作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的 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/
我是一名优秀的程序员,十分优秀!