gpt4 book ai didi

python - 如何确保只有在 pytest 中明确要求时才运行带有标记的测试?

转载 作者:太空宇宙 更新时间:2023-11-04 01:57:46 24 4
gpt4 key购买 nike

我用适当的标记标记了一些测试。如果我运行 pytest,默认情况下它们会运行,但我想默认跳过它们。我知道的唯一选择是在调用 pytest 时明确说“不是标记”,但我希望它们不会默认运行,除非在命令行明确询问标记。

最佳答案

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
...

关于python - 如何确保只有在 pytest 中明确要求时才运行带有标记的测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56374588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com