[英]pytest markers are not working when running doctest with pytest runner
尝试使用 pytest 运行程序运行 doctest 测试,并希望使用标记功能对它们进行分组并有选择地运行。
声明 doctest 的整个模块标记如下:
pytestmark = pytest.mark.mymark
pytest 有点拿起这个标记。 我可以看到,因为如果我不在 pytest.ini 中声明这个标记名称,它会给我一个警告。 问题是 pytest 在我应用标记过滤器时不想运行此测试:
> pytest -m mymark
collected 4 items / 4 deselected / 0 selected
任何想法如何使这项工作?
使用以下代码创建 conftest.py 文件可以解决该问题:
from _pytest.doctest import DoctestItem, DoctestModule
# pylint: disable=unused-argument
def pytest_collection_modifyitems(config, items):
"""pytest hook which gets called after collection is completed"""
for item in items:
if(isinstance(item, DoctestItem)
and isinstance(item.parent, DoctestModule)
and hasattr(item.parent.module, 'pytestmark')):
item.own_markers = [item.parent.module.pytestmark.mark]
可能是 pytest 中的一个错误,所以这是一个临时的、非通用的解决方案,它只是让您了解如何解决您的特定用例。
我建议使用pytest.ini
声明所有夹具以避免警告。
在src
级别创建 pytest.ini 文件并声明所有标记而不是 doctest。
[pytest]
addopts = -v --html=report.html --self-contained-html --junitxml="report.xml" --
markers =
marker_name: description
sanity: run tests in the directory/module marked as sanity
regression: run tests in the directory/module marked as regression
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.