源码使用说明
``` 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