- 跳过没有import导入成功的测试用例(Skipping on a missing import dependency)
pexpect = pytest.importorskip("pexpect")
- 以命令的方式或者条件下去标记跳过或标记失败的用例:
def test_function2():
import slow_module
if slow_module.slow_function():
pytest.xfail("slow_module taking too long")
- 对xfail标记的用例也不要执行:
# 如果一个测试应该被标记为xfail,并且报告为这样,但不应该被执行,使用run参数为False:
@pytest.mark.xfail(run=False)
def test_function():
...
- 默认情况下,XFAIL和XPASS都不会让测试套件失败。你可以通过设置strict keyword-only参数为True来更改:
# 让意外通过的用例,也会标记失败
@pytest.mark.xfail(strict=True)
def test_function():
...
- 也可以忽略 xfail标记,就像xfail失效了一样,在命令行指定
pytest --runxfail
- 当使用参数化时,可以将skip和xfail这样的标记应用到单个测试实例中:
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
- 显示xfailed、xpassed和跳过测试的额外信息:
# 命令行执行
pytest -rxXs