pytest xfail的使用

1.pytest.fail(reason=' '):

测试用例中调用,该方法之后的代码不再运行结果中标记为xfail,如下所示:

1 class TestPytest():
2     def test01(self):
3         print('test01')
4         pytest.xfail(reason='标记成xfail不执行,后面的代码不执行')
5         assert 1 == 1
6 if __name__ == '__main__':
7     pytest.main(['-vs', 'test_xfail.py'])
1 ============================= 1 xfailed in 0.06s ==============================
2 
3 Process finished with exit code 0
4 test01
5 XFAIL (标记成xfail不执行,后面的代码不执行)
6 self = <testcase.pytest.test.test_xfail.TestPytest object at 0x000002090B02B608>

可以看出,test01函数运行时,控制台中打印了“test01”,最后的结果标记为xfail.

2.@pytest.mark.xfail:  --在函数或者方法中使用

期望测试用例是失败的,但是不会影响测试用例的的执行;

如果测试用例执行失败的则结果是xfail(不会额外显示出错误信息);

如果测试用例执行成功的则结果是xpass;

 1 class TestPytest():
 2 
 3     @pytest.mark.xfail
 4     def test02(self):
 5         assert 's' in 'an'
 6         print('test02')
 7 
 8     @pytest.mark.xfail
 9     def test03(self):
10         assert 's' in 'this'
11         print('test03')
12 
13 
14 if __name__ == '__main__':
15     pytest.main(['-vs', 'test_xfail.py'])
1 test_xfail.py::TestPytest::test02 XFAIL
2 test_xfail.py::TestPytest::test03 test03
3 XPASS
4 
5 ======================== 1 xfailed, 1 xpassed in 0.08s ========================
6 
7 Process finished with exit code 0

运行结果分析:test02 标记为xfail (因为断言是失败的),test03 标记为xpass(断言是成功的)且打印了‘test03’.

posted @ 2022-04-27 14:06  别摸我的马甲线  阅读(270)  评论(0)    收藏  举报