2016-03-02 93 views
1

我有一個示例代碼,其中我有兩個一參數化測試功能斷言:pytest參數化夾具斷言

@pytest.mark.parametrize("test_input,expected", [ 
    ("3", 8), 
    ("2", 6), 
]) 
def test_eval(test_input, expected): 
    assert test_input == expected # first assert 
    assert test_input + 2 ==expected # second assert 

所以我想輸出是(僞碼):

assertion error 3==8 
assertion error 5==8 
assertion error 2==6 
assertion error 4==6 

對所有組合執行測試時,是否有辦法達到第二個斷言,即使第一個斷言失敗?

作爲替代,我想知道有沒有把這個變成類例如類似於這樣的方式:

@pytest.mark.parametrize("test_input,expected", [ 
    ("3", 8), 
    ("2", 6), 
]) 
class TestFunc(object): 
    def test_f1(test_input, expected): 
    assert test_input==expected 
    def test_f2(test_input, expected): 
    assert test_input+2==expected 

我要得到相同的輸出作爲前一種情況:

assertion error 3==8 
assertion error 5==8 
assertion error 2==6 
assertion error 4==6 

回答

0

pytest-expect插件這樣做的事情。

您在課堂上使用@pytest.mark.parametrize的方式是開箱即用的,您只是忘了self

另一種可能性是簡單地寫兩個測試和共享參數化:

eval_parametrize = pytest.mark.parametrize("test_input, expected", [ 
    ("3", 8), 
    ("2", 6), 
]) 

@eval_parametrize 
def test_f1(test_input, expected): 
    assert test_input == expected 

@eval_parametrize 
def test_f2(test_input, expected): 
    assert test_input + 2 == expected 
+0

我想是沒有用的插件(對不起,我沒有提及),但無論如何希望的解決方案是一個很好的解決方案,我想想,謝謝 – lcadc17