2016-10-21 11 views
0

pytestを使用して別の関数から取得したテストケースを提供したいparametrizeを使用しようとしています。 私はこのpytestでテストがスキップされる

test_input = [] 
rarp_input1 = "" 
rarp_output1 = "" 
count =1 
def test_first_rarp(): 
    global test_input 
    config = ConfigParser.ConfigParser() 
    config.read(sys.argv[2]) 
    global rarp_input1 
    global rarp_output1 
    rarp_input1 = config.get('rarp', 'rarp_input1') 
    rarp_input1 =dpkt.ethernet.Ethernet(rarp_input1) 
    rarp_input2 = config.get('rarp','rarp_input2') 
    rarp_output1 = config.getint('rarp','rarp_output1') 
    rarp_output2 = config.get('rarp','rarp_output2') 
    dict_input = [] 
    dict_input.append(rarp_input1) 
    dict_output = [] 
    dict_output.append(rarp_output1) 
    global count 
    test_input.append((dict_input[0],count,dict_output[0])) 
    #assert test_input == [something something,someInt] 

@pytest.mark.parametrize("test_input1,test_input2,expected1",test_input) 
def test_mod_rarp(test_input1,test_input2,expected1): 
    global test_input 
    assert mod_rarp(test_input1,test_input2) == expected1 

を試してみましたが、第2のテストケースはスキップなっています。テストケースがスキップなっているのはなぜそれは

test_mod_rarp1.py::test_mod_rarp[test_input10-test_input20-expected10]

言いますか?私は関数も入力も間違っていないことを確認しました。次のコードは正常に動作しているため、

@pytest.mark.parametrize("test_input1,test_input2,expected1,[something something,someInt,someInt]) 
def test_mod_rarp(test_input1,test_input2,expected1): 
    assert mod_rarp(test_input1,test_input2) == expected1 

ここに実際の入力は入れていません。とにかくその正しい。また、configParserを使用して入力を取得しているconfigファイルもあります。 test_mod_rarp1.pyは私がこれをやっているpythonファイル名です。私は基本的に、ここで問題を引き起こしている場合には、他の関数から変数にアクセスすることができるかどうかを知りたいと思っています(私の例ではtest_input)。変数の範囲を変更するにはどうすればいいですか?

+0

なぜこのためにグローバルを使用していますか?テストケースを作成するための関数を書いておきたい場合は、それらを使って新しいリストを返す*とにかく、あなたはそれを呼び出すようには見えないので、 'mod_rarp'がどうなっているのか分からないそうするための明確な方法はありません。 – jonrsharpe

+0

私は、このモジュールをインポートする必要なしに他のモジュールでも変数を使用したいと思っていました。じゃ、はい。必要な変数を返すとprametrizeと一緒に使用すると問題なく動作します。どうもありがとう。 –

+0

しかし、私はまだ変数の範囲について興味があります。私がそれをグローバルにしたのに、なぜ私はパラメータでtest_inputを使用することができませんでしたか? –

答えて

0

パラメタリゼーションはコンパイル時に行われるため、実行時に生成されたデータをパラメータ化する場合はパラメタライズがスキップされます。

あなたがしようとしていることを達成する理想的な方法は、フィクスチャのパラメータ化を使用することです。パラメータは、generate_input()の実行前に起こったので

あなたのために物事をクリアする必要があります例えば、以下

import pytest 
input = [] 

def generate_input(): 
    global input 
    input = [10,20,30] 



@pytest.mark.parametrize("a", input) 
def test_1(a): 
    assert a < 25 

def generate_input2(): 
    return [10, 20, 30] 


@pytest.fixture(params=generate_input2()) 
def a(request): 
    return request.param 

def test_2(a): 
    assert a < 25 

OP

<SKIPPED:>pytest_suites/test_sample.py::test_1[a0] 


********** test_2[10] ********** 
<EXECUTING:>pytest_suites/test_sample.py::test_2[10] 
Collected Tests 
TEST::pytest_suites/test_sample.py::test_1[a0] 
TEST::pytest_suites/test_sample.py::test_2[10] 
TEST::pytest_suites/test_sample.py::test_2[20] 
TEST::pytest_suites/test_sample.py::test_2[30] 

test_1を参照してください、あなたはあなたのケースで同じロジックを適用できるがスキップされましたtest_2は必要に応じてパラメータ化されます

関連する問題