Home > Software design >  pytest markers are not working when running doctest with pytest runner
pytest markers are not working when running doctest with pytest runner

Time:07-30

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?

CodePudding user response:

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.

CodePudding user response:

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
  • Related