2016-04-06 28 views
2

我想參數化測試場景,所以我不必爲像xUnit樣式測試中的每個場景做單獨的情況。pytest說夾具沒找到,當我不使用一個

這裏是pytest的例子,我試圖複製我自己的用例。

from datetime import datetime, timedelta 

testdata = [ 
    (datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)), 
    (datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)), 
] 


@pytest.mark.parametrize("a,b,expected", testdata) 
def test_timedistance_v0(a, b, expected): 
    diff = a - b 
    assert diff == expected 

上面的工作很好,但是當我嘗試修改它以供我使用,如下所示。我得到一個錯誤,說「轉儲」夾具找不到。我不知道發生了什麼事。這是我第一次使用pytest,所以也許我沒有得到一些東西。

dump1 = load_dump(pathtodump1,pathtodump2) # load_dump can take multiple params 
dump2 = load_dump(pathtodump3) 

scenarios = [(dump1,{'prod_str':None}), 
      (dump2,{'prod_str':"123"})] 

@pytest.mark.paramaterize("dump,expected_meta", scenarios) 
def test_metadump_profiles(dump, expected_meta): 
     meta = dump.meta 
     assert meta.prod_str == expected_meta['prod_str'] 

這是我從pytest得到的錯誤。我還應該提到,當我調試測試從不運行時,它在參數裝飾器中的某處失敗。

========================================================================= ERRORS ========================================================================== 
________________________________________________________ ERROR at setup of test_metadump_profiles _________________________________________________________ 
file /x/x/x-x/x/x/x/x/x/x/test_x.py, line 81 
    @pytest.mark.paramaterize("dump,expect_meta", scenarios) 
    def test_metadump_profiles(dump, expect_meta): 
     fixture 'dump' not found 
     available fixtures: capfd, capsys, recwarn, tmpdir_factory, tmpdir, monkeypatch, pytestconfig, record_xml_property, cache 
     use 'py.test --fixtures [testpath]' for help on them. 


. 

回答

5
@pytest.mark.paramaterize 

這是無效的,pytest拋出一個明確的錯誤是沒有意義的。這是由拼寫錯誤引起的...

@pytest.mark.parametrize 

是有效的拼寫。最愚蠢的錯誤,我把我的頭髮拉出來。 參見github issue on this topic for more info.