语法:
@pytest.mark.xfail(condition,reason,strict)
condition默认为true,表示启动这个装饰器
reason在不符合期望时展示
strict是为了解决这样一种情况:当一个被标记为 xfail 的测试意外通过了(也就是 Bug 被修复了,或者功能终于开发完了),pytest 默认会把它标记为 XPASS(Unexpected Pass)。默认情况下,XPASS 不会导致整个测试流程崩溃,你的 CI/CD 依然是绿灯通过的。这就会导致一个问题:开发把 Bug 修好了,但没人记得去删掉测试代码上的 @pytest.mark.xfail 标记。 久而久之,代码里全是废弃的标记。strict=True 就是为了解决这个问题的。它规定:如果一个预期会失败的测试居然通过了,那就强制让它报错!
测试代码:
import pytest@pytest.mark.xfail(strict=True, reason='原因1')
def test_xfail1():assert False@pytest.mark.xfail(strict=True, reason='原因2')
def test_xfail2():assert True@pytest.mark.xfail(strict=False, reason='原因3')
def test_xfail3():assert False@pytest.mark.xfail(strict=False, reason='原因4')
def test_xfail4():assert Truedef test_xfail5():assert Falsedef test_xfail6():assert True@pytest.mark.skip
def test_xfail7():assert True
结果:
Results (0.20s):1 passed1 xpassed2 failed- test_xfail.py:7 test_xfail2- test_xfail.py:19 test_xfail52 xfailed1 skipped
生成allure报告:
pytest .\test_xfail.py --alluredir=../xml --clean-alluredir //生成xml
allure generate ../xml -o ../html -c // 生成html工程
allure open ../html //启动工程
通过的用例:

失败的用例:

跳过的用例

结论:
注解未开启:成功是passed,失败是failed
注解开启,严格模式未开启:成功是xpassed,失败是xfailed
严格模式开启:失败是xfailed,成功是failed
