python - 如何在pytest中只运行未标记的测试

标签 python pytest

我的 python 测试代码中有几个标记:

@pytest.mark.slowtest
@pytest.mark.webtest
@pytest.mark.stagingtest

我能够使用标记有选择地运行测试,例如 pytest -m slowtest

如何在不求助于 pytest -m "not (slowtest or webtest or stagingtest)" 的情况下运行未标记的测试?

如您所想,我们将来可能会使用其他标记...

最佳答案

我发现另一个有用的选项是使用 pytest.ini 添加默认选项。我们明确地写下要跳过的标记。它需要在 pytest.ini 中对跳过的标记列表进行主线化。换句话说,不是 100% OP 要求的,但这是我在寻找默认标记时发现的最好的 SO 问题。希望这可以帮助其他人。

来自docs :


addopts

    Add the specified OPTS to the set of command line arguments as if they had been specified by the user. Example: if you have this ini file content:

    # content of pytest.ini
    [pytest]
    addopts = --maxfail=2 -rf  # exit after 2 failures, report fail info

    issuing pytest test_hello.py actually means:

    pytest --maxfail=2 -rf test_hello.py

    Default is to add no options.

我添加到我的 pytest.ini 中的内容

[pytest]
addopts = -m "not slow"
markers =
    slow: marks tests as slow (deselect with '-m "not slow"')

使用它

﮸$ grep 'mark.slow' tests/*.py | wc -l
3

$ pytest -s
======================================================= test session starts ========================================================
platform linux -- Python 3.8.2, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /tests, configfile: pytest.ini
collected 66 items / 3 deselected / 63 selected                                                                                    

../tests/test_app.py ...............................................................

================================================= 63 passed, 3 deselected in 8.70s =================================================

关于python - 如何在pytest中只运行未标记的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39846230/

相关文章:

python - python : best way to improve a function with a error message

python - Django Web 应用程序中的 SMTP 问题

python - pytest:使用 re.escape() 断言转义字符失败

python - 如何使用mockito模拟os.path.join

python - 如何向管理表单添加自定义功能?

Python 3 : Crash with free(): corrupted unsorted chunks while reading file

python - 使用 pytest-qt 测试 PyQt5 应用程序时如何显示 GUI?

python - 如何标记一个函数不是 pytest 的测试?

python - Pytest - 测试解析器错误 : Unrecognised arguments

Python 临时文件 : broken or am I doing it wrong?