简体   繁体   中英

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.

Whole module where doctests are declared is marked as following:

pytestmark = pytest.mark.mymark

pytest sort of picks up this marker. I can see that because if I don't declare this marker name in pytest.ini, it gives me a warning. The problem is that pytest doesn't want to run this test when I'm applying marker filter:

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

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.

I would recommend using pytest.ini to declare all fixture to avoid warning.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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