Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doctest: fix autouse fixtures possibly not getting picked up #11941

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/11929.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a regression in pytest 8.0.0 whereby autouse fixtures defined in a module get ignored by the doctests in the module.
15 changes: 7 additions & 8 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from _pytest.outcomes import OutcomeException
from _pytest.outcomes import skip
from _pytest.pathlib import fnmatch_ex
from _pytest.pathlib import import_path
from _pytest.python import Module
from _pytest.python_api import approx
from _pytest.warning_types import PytestWarning
Expand Down Expand Up @@ -107,7 +106,7 @@
"--doctest-ignore-import-errors",
action="store_true",
default=False,
help="Ignore doctest ImportErrors",
help="Ignore doctest collection errors",
dest="doctest_ignore_import_errors",
)
group.addoption(
Expand Down Expand Up @@ -561,17 +560,17 @@
pass

try:
module = import_path(
self.path,
root=self.config.rootpath,
mode=self.config.getoption("importmode"),
)
except ImportError:
module = self.obj
except Collector.CollectError:

Check warning on line 564 in src/_pytest/doctest.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/doctest.py#L564

Added line #L564 was not covered by tests
if self.config.getvalue("doctest_ignore_import_errors"):
skip("unable to import module %r" % self.path)
else:
raise

# While doctests currently don't support fixtures directly, we still
# need to pick up autouse fixtures.
self.session._fixturemanager.parsefactories(self)

# Uses internal doctest module parsing mechanism.
finder = MockAwareDocTestFinder()
optionflags = get_optionflags(self.config)
Expand Down
32 changes: 32 additions & 0 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,38 @@ def auto(request):
str(result.stdout.no_fnmatch_line("*FAILURES*"))
result.stdout.fnmatch_lines(["*=== 1 passed in *"])

@pytest.mark.parametrize("scope", [*SCOPES, "package"])
def test_auto_use_defined_in_same_module(
self, pytester: Pytester, scope: str
) -> None:
"""Autouse fixtures defined in the same module as the doctest get picked
up properly.

Regression test for #11929.
"""
pytester.makepyfile(
f"""
import pytest

AUTO = "the fixture did not run"

@pytest.fixture(autouse=True, scope="{scope}")
def auto(request):
global AUTO
AUTO = "the fixture ran"

def my_doctest():
'''My doctest.

>>> my_doctest()
'the fixture ran'
'''
return AUTO
"""
)
result = pytester.runpytest("--doctest-modules")
result.assert_outcomes(passed=1)


class TestDoctestNamespaceFixture:
SCOPES = ["module", "session", "class", "function"]
Expand Down