Skip to content

Commit

Permalink
doctest: fix autouse fixtures possibly not getting picked up
Browse files Browse the repository at this point in the history
Fix #11929.
  • Loading branch information
bluetech committed Feb 6, 2024
1 parent 6e5008f commit f81c216
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
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.
4 changes: 4 additions & 0 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,10 @@ def _from_module(self, module, object):
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

0 comments on commit f81c216

Please sign in to comment.