【pytest】skip&Xfail

  • 一:skip跳过用例
    • pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能希望满足某些条件才执行某些测试用例,否则pytest会跳过运行该测试用例。实际常见场景:跳过非Windows平台上的仅Windows测试,或者跳过依赖于当前不可用的外部资源(例如数据库)的测试。
    • @pytest.mark.skip,跳过执行测试用例,有可选参数reason:跳过的原因,会在执行结果中打印
    • @pytest.mark.skipif(condition=(sys.platform=="win32"),reason='Windows系统不运行此用例'),注意:condition需要返回True才会跳过
import pytest,sys

class Test_class:
    @pytest.mark.skip
    def test_001(self):
        a = 'hello'
        b = 'hello world'
        assert a == b
        print('测试用例--001')

    def test_002(self):
        a = 1
        b = 2
        assert a+b == 3
        print('测试用例--002')

@pytest.mark.skipif(condition=(sys.platform=="win32"),reason='Windows系统不运行此用例')
def test_003():
    a = 'hello'
    b = 'hello world'
    assert a in b
    print('测试用例--003')
  • 运行结果:

  • 二:Xfail

    • 1.pytest.fail(reason=' '):在测试用例中调用,该方法之后的代码不再运行,结果中标记为xfail,如下所示
    • 2.@pytest.mark.xfail: --在函数或者方法中使用
      • 期望测试用例是失败的,但是不会影响测试用例的的执行;
      • 如果测试用例执行失败的则结果是xfail(不会额外显示出错误信息);
      • 如果测试用例执行成功的则结果是xpass;
pytest.mark.xfail(condition=None, *, reason=None, raises=None, run=True, strict=False)
参数
condition (bool or str) -- 将测试功能标记为xfail的条件 (True/False 或A condition string ). 如果是bool,还必须指定 reason (见 condition string )
reason (str) -- 测试函数标记为xfail的原因。
raises (Type[Exception]) -- 异常子类预期由测试函数引发;其他异常将使测试失败。
run (bool) -- 是否应实际执行测试功能。如果 False ,函数将始终Xfail且不会执行(如果函数是segfaulting,则很有用)。
strict (bool) --如果 False (默认)功能将在终端输出中显示为 xfailed 如果失败了, xpass 如果它通过。在这两种情况下,这不会导致整个测试套件失败。这对于标记 薄片状的 稍后要处理的测试(随机失败的测试)。如果 True ,功能将在终端输出中显示为 xfailed 如果它失败了,但是如果它意外地通过了,那么它将 fail 测试套件。这对于标记总是失败的函数特别有用,如果函数意外开始通过,则应该有明确的指示(例如,库的新版本修复了已知的错误)。
posted @ 2022-08-13 22:07  Tony_xiao  阅读(91)  评论(0编辑  收藏  举报