Skip to content

Commit 6c0b6c2

Browse files
authored
Merge pull request #11941 from bluetech/doctest-parsefactories
doctest: fix autouse fixtures possibly not getting picked up
2 parents 4c894f2 + 9cd14b4 commit 6c0b6c2

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

Diff for: changelog/11929.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a regression in pytest 8.0.0 whereby autouse fixtures defined in a module get ignored by the doctests in the module.

Diff for: src/_pytest/doctest.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
from _pytest.outcomes import OutcomeException
4141
from _pytest.outcomes import skip
4242
from _pytest.pathlib import fnmatch_ex
43-
from _pytest.pathlib import import_path
4443
from _pytest.python import Module
4544
from _pytest.python_api import approx
4645
from _pytest.warning_types import PytestWarning
@@ -107,7 +106,7 @@ def pytest_addoption(parser: Parser) -> None:
107106
"--doctest-ignore-import-errors",
108107
action="store_true",
109108
default=False,
110-
help="Ignore doctest ImportErrors",
109+
help="Ignore doctest collection errors",
111110
dest="doctest_ignore_import_errors",
112111
)
113112
group.addoption(
@@ -561,17 +560,17 @@ def _from_module(self, module, object):
561560
pass
562561

563562
try:
564-
module = import_path(
565-
self.path,
566-
root=self.config.rootpath,
567-
mode=self.config.getoption("importmode"),
568-
)
569-
except ImportError:
563+
module = self.obj
564+
except Collector.CollectError:
570565
if self.config.getvalue("doctest_ignore_import_errors"):
571566
skip("unable to import module %r" % self.path)
572567
else:
573568
raise
574569

570+
# While doctests currently don't support fixtures directly, we still
571+
# need to pick up autouse fixtures.
572+
self.session._fixturemanager.parsefactories(self)
573+
575574
# Uses internal doctest module parsing mechanism.
576575
finder = MockAwareDocTestFinder()
577576
optionflags = get_optionflags(self.config)

Diff for: testing/test_doctest.py

+32
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,38 @@ def auto(request):
13761376
str(result.stdout.no_fnmatch_line("*FAILURES*"))
13771377
result.stdout.fnmatch_lines(["*=== 1 passed in *"])
13781378

1379+
@pytest.mark.parametrize("scope", [*SCOPES, "package"])
1380+
def test_auto_use_defined_in_same_module(
1381+
self, pytester: Pytester, scope: str
1382+
) -> None:
1383+
"""Autouse fixtures defined in the same module as the doctest get picked
1384+
up properly.
1385+
1386+
Regression test for #11929.
1387+
"""
1388+
pytester.makepyfile(
1389+
f"""
1390+
import pytest
1391+
1392+
AUTO = "the fixture did not run"
1393+
1394+
@pytest.fixture(autouse=True, scope="{scope}")
1395+
def auto(request):
1396+
global AUTO
1397+
AUTO = "the fixture ran"
1398+
1399+
def my_doctest():
1400+
'''My doctest.
1401+
1402+
>>> my_doctest()
1403+
'the fixture ran'
1404+
'''
1405+
return AUTO
1406+
"""
1407+
)
1408+
result = pytester.runpytest("--doctest-modules")
1409+
result.assert_outcomes(passed=1)
1410+
13791411

13801412
class TestDoctestNamespaceFixture:
13811413
SCOPES = ["module", "session", "class", "function"]

0 commit comments

Comments
 (0)