pytest学习总结2.13 - skip、xfail使用全集

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

2.13 skip和xfail处理不能成功的测试用例

  标记不能在某些平台上运行或您希望失败的测试函数,以便pytest可以相应地处理它们,并呈现测试会话的摘要,同时保持测试套件的绿色,跳过意味着您只有在满足某些条件时才期望测试通过,否则pytest应该完全跳过运行测试。

  常见的例子是跳过非windows平台上的仅windows测试 xfail意味着您期望测试由于某种原因而失败。还有一个常见的例子是对尚未实现的特性或尚未修复的错误的测试。

  当测试通过,尽管预期失败(用pytest.mark.xfail标记)时,它是xpass,将在测试摘要中报告。

2.13.1 跳过测试用例

1. 装饰器标记跳过,并传递跳过原因:

import pytest
@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
    ...
复制代码

2. pytest.skip 强制性跳过

def test_function():
    if not valid_config():
        pytest.skip("unsupported configuration")
复制代码

3. 参数化中跳过用例:

import pytest
@pytest.mark.parametrize("i", range(50))
def test_num(i):
    if i in (17, 25):
        pytest.skip("跳过用例")
复制代码

4. 跳过平台是win的所有用例

import sys
import pytest
    if not sys.platform.startswith("win"):
    pytest.skip("skipping windows-only tests", allow_module_level=True)
复制代码

5. 有条件的跳过用例

如果您希望有条件地跳过某些内容,下面是一个标记在Python3.10之前的解释器上运行时要跳过的测试函数的例子:

import sys
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher˓→")
def test_function():
    ...
复制代码

6. 基于某些条件跳过某些测试用例

在模块(py文件)之间共享 skipif 标记,基于某些条件,跳过模块中的某些测试:

# 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():
    pass
复制代码
# test_myothermodule.py
from test_mymodule import minversion

@minversion
def test_anotherfunction():
    pass
复制代码

7. 跳过一个类,在类上使用 skipif 标记

import pytest, sys
@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"
复制代码

8. 跳过一个模块的所有测试函数,您可以使用全局测试标记

import pytest
# test_module.py
pytestmark = pytest.mark.skipif(...)
复制代码

9. 跳过文件或目录:直接pytest后面跟着对应目录的用例就可以了

2.13.2 将测试用例标记为失败

1. 使用xfail标记来指示您希望测试失败:

@pytest.mark.xfail
def test_function():
    ...
复制代码

2. 运行测试过程中设置失败

在pytest.xfail()调用之后没有执行其他代码。这是因为它是通过引发一个已知的异常而在内部实现的。

import pytest
@pytest.mark.parametrize("i", range(50))
def test_num(i):
    if i in (17, 25):
        pytest.xfail("预期失败")
复制代码

3. 在某个条件、原因下失败

@pytest.mark.xfail(sys.platform == "win32", reason="bug in a 3rd party library")
def test_function():
    ...
复制代码

4. 禁止运行标记的用例

@pytest.mark.xfail(run=False)
def test_function():
    ...
复制代码

5. 设置strict使xfail的测试用例运行结果为失败

import pytest
@pytest.mark.xfail(strict=True)
def test_function():
    pass
复制代码
[pytest]
xfail_strict=true
复制代码

6. 相关代码示例

import pytest
xfail = pytest.mark.xfail
@xfail
def test_hello():
    assert 0
@xfail(run=False)
def test_hello2():
    assert 0
@xfail("hasattr(os, 'sep')")
def test_hello3():
    assert 0
@xfail(reason="bug 110")
def test_hello4():
    assert 0
@xfail('pytest.__version__[0] != "17"')
def test_hello5():
    assert 0
def test_hello6():
    pytest.xfail("reason")
@xfail(raises=IndexError)
def test_hello7():
    x = []
    x[1] = 1
复制代码
C:\Users\mc\Desktop\python基础>pytest -rx test_module.py
======================================================================= test session starts ========================================================================
platform win32 -- Python 3.9.6, pytest-7.1.1, pluggy-0.13.1
rootdir: C:\Users\mc\Desktop\python基础
collected 7 items                                               
test_module.py xxxxxxx                               [100%]

===================================================================== short test summary info ======================================================================
XFAIL test_module.py::test_hello
XFAIL test_module.py::test_hello2
  reason: [NOTRUN]
XFAIL test_module.py::test_hello3
  condition: hasattr(os, 'sep')
XFAIL test_module.py::test_hello4
  bug 110
XFAIL test_module.py::test_hello5
  condition: pytest.__version__[0] != "17"
XFAIL test_module.py::test_hello6
  reason: reason
XFAIL test_module.py::test_hello7
======================================================================== 7 xfailed in 0.19s ========================================================================
复制代码

2.13.3 使用参数化跳过/退出

import sys
import pytest
@pytest.mark.parametrize(
    ("n", "expected"),
    [
        (1, 2),
        pytest.param(1, 0, marks=pytest.mark.xfail),
        pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")),
        (2, 3),
        (3, 4),
        (4, 5),
        pytest.param(10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k˓→")),
    ],
)
def test_increment(n, expected):
    assert n + 1 == expected
复制代码
C:\Users\mc\Desktop\python>pytest -vs test_module.py
======================================================================= test session starts ========================================================================
platform win32 -- Python 3.9.6, pytest-7.1.1, pluggy-0.13.1 -- c:\python39\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\mc\Desktop\python
collected 7 items

test_module.py::test_increment[1-2] PASSED
test_module.py::test_increment[1-0] XFAIL
test_module.py::test_increment[1-3] XFAIL (some bug)
test_module.py::test_increment[2-3] PASSED
test_module.py::test_increment[3-4] PASSED
test_module.py::test_increment[4-5] PASSED
test_module.py::test_increment[10-11] SKIPPED (py2k˓→)
============================================================= 4 passed, 1 skipped, 2 xfailed in 0.12s ==============================================================
复制代码

猜你喜欢

转载自juejin.im/post/7085359646977835022