pytest如何使用skip和skipif和xfail
- 你不想在window上运行:
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
class TestPosixCalls:
def test_function(self):
"will not be setup or run under 'win32' platform"
- python版本低于某一个特定版本就跳过:
import sys
@pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python3.9 or higher")
def test_function():
...
- 在module之间调用skipif(You can share skipif markers between modules. Consider this test module):
# content of test_mymodule.py
import mymodule
minversion = pytest.mark.skipif(
mymodule.__versioninfo__ < (1, 1), reason="at least mymodule-1.1 required"
)
@minversion
def test_function():
...
调用:
# test_myothermodule.py
from test_mymodule import minversion
@minversion
def test_anotherfunction():
...
tips: 对于较大的测试suite,可以使用文件进行标记
4.你也可以在class上skipif标记:
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
class TestPosixCalls:
def test_function(self):
"will not be setup or run under 'win32' platform"
- 跳过某一个module中所有测试函数:
# 如果你想跳过一个模块的所有测试函数,你可以使用全局pytestmark:
# test_module.py
pytestmark = pytest.mark.skipif(...)