pytest跳过文件和目录skipif

  1. 跳过没有import导入成功的测试用例(Skipping on a missing import dependency)
pexpect = pytest.importorskip("pexpect")
  1. 以命令的方式或者条件下去标记跳过或标记失败的用例:
def test_function2():
    import slow_module

    if slow_module.slow_function():
        pytest.xfail("slow_module taking too long")

  1. 对xfail标记的用例也不要执行:
# 如果一个测试应该被标记为xfail,并且报告为这样,但不应该被执行,使用run参数为False:
@pytest.mark.xfail(run=False)
def test_function():
    ...
  1. 默认情况下,XFAIL和XPASS都不会让测试套件失败。你可以通过设置strict keyword-only参数为True来更改:
# 让意外通过的用例,也会标记失败
@pytest.mark.xfail(strict=True)
def test_function():
    ...
  1. 也可以忽略 xfail标记,就像xfail失效了一样,在命令行指定
pytest --runxfail
  1. 当使用参数化时,可以将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
  1. 显示xfailed、xpassed和跳过测试的额外信息:
# 命令行执行
pytest -rxXs
posted @ 2022-03-12 21:50  清风吹拂啊狂风肆虐  阅读(206)  评论(0编辑  收藏  举报