pytest xfail參數詳解


源碼使用說明
``` def xfail(self,condition
=None, reason=None, raises=None, run=True, strict=False): """mark the the test function as an expected failure if eval(self,condition) has a True value. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. See http://doc.pytest.org/en/latest/skipping.html """ ```

1. 直接使用@pytest.mark.xfail

@pytest.mark.xfail
def test_01():
    assert 1 == 1


@pytest.mark.xfail
def test_02():
    assert 1 == 2

運行結果為

 

 使用xfail標記用例預期失敗,如果用例運行成功則顯示Xpassed,失敗則顯示xfailed。xfail標記並不會影響用例的運行

2.strict參數 :設置strict=True以確保XPASS時,測試的記錄為失敗

同樣使用該代碼

@pytest.mark.xfail(strict=True)
def test_01():
    assert 1 == 1


@pytest.mark.xfail
def test_02():
    assert 1 == 2

運行結果

 

 xpass運行的結果現在顯示為failed

3,結合skipif 條件,條件為Ture運行xfail

@pytest.mark.xfail(1 >= 5, reason='test')
def test_01():
assert 1 == 1

@pytest.mark.xfail(1 >= 5, reason='test')
def test_02():
assert 1 == 5


@pytest.mark.xfail(1 <= 5, reason='test')
def test_03():
assert 1 == 1


@pytest.mark.xfail(1 <= 5, reason='test')
def test_04():
assert 1 == 2

運行結果

 

 用例01、02,因為條件為false所以不運行xfail,結果顯示為pass、fail

03、04條件為true,運行xfail,結果顯示為xpass、xfail

4raises參數

具體的說明測試失敗的原因。可以在raises參數中指定單個異常或異常組,如果測試失敗且沒有提到指定的異常,那么測試將被報告為常規失敗raises

@pytest.mark.xfail(raises=AssertionError)
def test_01():
    assert 1 == 2


@pytest.mark.xfail(raises=ValueError)
def test_02():
    if isinstance('1234', int) is not True:
        raise TypeError("傳入參數非整數")

運行結果:

 

 01測試用例,異常為指定異常,xfail運行,結果顯示xfailed

02測試用例,異常為非指定異常,xfail不運行,結果顯示failed



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM