Skip to content

Commit 23b91d1

Browse files
authored
[8.0.x] Merge pull request #11941 from bluetech/doctest-parsefactories (#11948)
doctest: fix autouse fixtures possibly not getting picked up (cherry picked from commit 6c0b6c2)
1 parent b188f4d commit 23b91d1

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-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

+8-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(
@@ -568,16 +567,17 @@ def _from_module(self, module, object):
568567
)
569568
else:
570569
try:
571-
module = import_path(
572-
self.path,
573-
root=self.config.rootpath,
574-
mode=self.config.getoption("importmode"),
575-
)
576-
except ImportError:
570+
module = self.obj
571+
except Collector.CollectError:
577572
if self.config.getvalue("doctest_ignore_import_errors"):
578573
skip("unable to import module %r" % self.path)
579574
else:
580575
raise
576+
577+
# While doctests currently don't support fixtures directly, we still
578+
# need to pick up autouse fixtures.
579+
self.session._fixturemanager.parsefactories(self)
580+
581581
# Uses internal doctest module parsing mechanism.
582582
finder = MockAwareDocTestFinder()
583583
optionflags = get_optionflags(self.config)

Diff for: testing/test_doctest.py

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

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

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

0 commit comments

Comments
 (0)