简体   繁体   English

使用 pytest 跑步者运行 doctest 时,pytest 标记不起作用

[英]pytest markers are not working when running doctest with pytest runner

Trying to run doctest tests using pytest runner and wanted to use markers functionality to group them and run selectively.尝试使用 pytest 运行程序运行 doctest 测试,并希望使用标记功能对它们进行分组并有选择地运行。

Whole module where doctests are declared is marked as following:声明 doctest 的整个模块标记如下:

pytestmark = pytest.mark.mymark

pytest sort of picks up this marker. pytest 有点拿起这个标记。 I can see that because if I don't declare this marker name in pytest.ini, it gives me a warning.我可以看到,因为如果我不在 pytest.ini 中声明这个标记名称,它会给我一个警告。 The problem is that pytest doesn't want to run this test when I'm applying marker filter:问题是 pytest 在我应用标记过滤器时不想运行此测试:

> pytest -m mymark
collected 4 items / 4 deselected / 0 selected                   

Any ideas how to make this work?任何想法如何使这项工作?

Creating conftest.py file with the following code fixes the problem:使用以下代码创建 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]

Likely a bug in pytest, so this is a temporary, non-generic solution that just gives you an idea how to address your particular use case.可能是 pytest 中的一个错误,所以这是一个临时的、非通用的解决方案,它只是让您了解如何解决您的特定用例。

I would recommend using pytest.ini to declare all fixture to avoid warning.我建议使用pytest.ini声明所有夹具以避免警告。

Create a pytest.ini file at src level and declare all your markers instead of doctest.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM