@pytest.mark.xfail:
期望测试用例是失败的,但是不会影响测试用例的的执行;
如果测试用例执行失败的则结果是xfail(不会额外显示出错误信息);
如果测试用例执行成功的则结果是xpass;
import pytest class TestClass(): @pytest.mark.xfail def test_one(self): print("test_one方法执行") assert 1==2 def test_two(self): print("test_two方法执行") assert ‘o‘ in ‘love‘ def test_three(self): print("test_three方法执行") assert 3-2==1
执行结果:
C:\Users\cale\checkapi\test>pytest test_gy.py -v
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\ipharmacare\python37\python.exe
cachedir: .pytest_cache
metadata: {‘Python‘: ‘3.7.3‘, ‘Platform‘: ‘Windows-10-10.0.17134-SP0‘, ‘Packages‘: {‘pytest‘: ‘5.2.1‘, ‘py‘: ‘1.8.0‘, ‘pluggy‘: ‘0.13.0‘}, ‘Plugins‘: {‘html‘: ‘2.0.0‘, ‘metadata‘: ‘1.8.0‘, ‘rerunfailures‘: ‘7.0‘}}
rootdir: C:\Users\cale\checkapi\test
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 3 items
test_gy.py::TestClass::test_one XFAIL [ 33%]
test_gy.py::TestClass::test_two PASSED [ 66%]
test_gy.py::TestClass::test_three PASSED [100%]
======================================================================================================= 2 passed, 1 xfailed in 0.31s =======================================================================================================
pytest.fail(reason=‘ ‘):
在测试用例中调用,该方法之后的代码不再运行,结果中标记为xfail
import pytest class TestClass(): def test_one(self): print("test_one方法执行") pytest.xfail(reason=‘该功能尚未完善‘) assert 1==1 def test_two(self): print("test_two方法执行") assert ‘o‘ in ‘love‘ def test_three(self): print("test_three方法执行") assert 3-2==1
运行结果:
C:\Users\cale\checkapi\test>pytest test_gy.py -v
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\ipharmacare\python37\python.exe
cachedir: .pytest_cache
metadata: {‘Python‘: ‘3.7.3‘, ‘Platform‘: ‘Windows-10-10.0.17134-SP0‘, ‘Packages‘: {‘pytest‘: ‘5.2.1‘, ‘py‘: ‘1.8.0‘, ‘pluggy‘: ‘0.13.0‘}, ‘Plugins‘: {‘html‘: ‘2.0.0‘, ‘metadata‘: ‘1.8.0‘, ‘rerunfailures‘: ‘7.0‘}}
rootdir: C:\Users\cale\checkapi\test
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 3 items
test_gy.py::TestClass::test_one XFAIL [ 33%]
test_gy.py::TestClass::test_two PASSED [ 66%]
test_gy.py::TestClass::test_three PASSED [100%]
======================================================================================================= 2 passed, 1 xfailed in 0.35s =======================================================================================================
原文:https://www.cnblogs.com/pipile/p/12655094.html