pytest如何使用skip和skipif和xfail

  1. 你不想在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"
  1. python版本低于某一个特定版本就跳过:
import sys

@pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python3.9 or higher")
def test_function():
    ...
  1. 在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"
  1. 跳过某一个module中所有测试函数:
# 如果你想跳过一个模块的所有测试函数,你可以使用全局pytestmark:
# test_module.py
pytestmark = pytest.mark.skipif(...)
posted @ 2022-03-12 08:07  清风吹拂啊狂风肆虐  阅读(55)  评论(0编辑  收藏  举报