我有一些测试用适当的标记标记。如果我运行pytest,则默认情况下它们会运行,但是我想默认情况下跳过它们。我知道的唯一选择是在pytest调用时明确说“不是标记”,但是我希望它们默认情况下不运行,除非在命令行中明确询问了标记。
答案 0 :(得分:1)
对Control skipping of tests according to command line option中的示例进行了少许修改:
# conftest.py
import pytest
def pytest_collection_modifyitems(config, items):
keywordexpr = config.option.keyword
markexpr = config.option.markexpr
if keywordexpr or markexpr:
return # let pytest handle this
skip_mymarker = pytest.mark.skip(reason='mymarker not selected')
for item in items:
if 'mymarker' in item.keywords:
item.add_marker(skip_mymarker)
示例测试:
import pytest
def test_not_marked():
pass
@pytest.mark.mymarker
def test_marked():
pass
使用标记运行测试:
$ pytest -v -k mymarker
...
collected 2 items / 1 deselected / 1 selected
test_spam.py::test_marked PASSED
...
或者:
$ pytest -v -m mymarker
...
collected 2 items / 1 deselected / 1 selected
test_spam.py::test_marked PASSED
...
没有标记:
$ pytest -v
...
collected 2 items
test_spam.py::test_not_marked PASSED
test_spam.py::test_marked SKIPPED
...