Skip to content

Commit 2ce9c6d

Browse files
authored
Merge pull request #14540 from minbang930/fix-14533-doctest-module-fixtures
Fix module fixtures with --doctest-modules
2 parents b8d5cb2 + ef55156 commit 2ce9c6d

8 files changed

Lines changed: 78 additions & 7 deletions

File tree

‎AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ Mike Fiedler (miketheman)
334334
Mike Hoyle (hoylemd)
335335
Mike Lundy
336336
Milan Lesnek
337+
minbang930
337338
Miro Hrončok
338339
Mulat Mekonen
339340
mrbean-bremen

‎changelog/14533.breaking.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
When using :option:`--doctest-modules`, autouse fixtures with ``module``, ``package`` or ``session`` scope that are defined inline in Python test modules (not plugins or conftests) will now possibly execute twice.
2+
3+
If this is undesirable, move the fixture definition to a ``conftest.py`` file if possible.
4+
5+
Technical explanation for those interested:
6+
When using `--doctest-modules`, pytest possibly collects Python modules twice, once as :class:`pytest.Module` and once as a ``DoctestModule`` (depending on the configuration).
7+
Due to improvements in pytest's fixture implementation, if e.g. the ``DoctestModule`` collects a fixture, it is now visible to it only, and not to the ``Module``.
8+
This means that both need to register the fixtures independently.

‎doc/en/how-to/doctest.rst‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ By default, pytest will collect ``test*.txt`` files looking for doctest directiv
4242
can pass additional globs using the :option:`--doctest-glob` option (multi-allowed).
4343

4444
In addition to text files, you can also execute doctests directly from docstrings of your classes
45-
and functions, including from test modules:
45+
and functions, including from test modules, using the :option:`--doctest-modules` option:
4646

4747
.. code-block:: python
4848
@@ -224,6 +224,9 @@ unless explicitly configured by :confval:`python_files`.
224224
Also, the :ref:`usefixtures <usefixtures>` mark and fixtures marked as :ref:`autouse <autouse>` are supported
225225
when executing text doctest files.
226226

227+
Python doctest modules are collected independently from Python test files.
228+
Fixture scope is not shared between the two.
229+
227230
Doctests do not support fixtures that depend on parametrization, because doctest
228231
collection does not perform the same test generation as normal test functions.
229232
This includes parametrized autouse fixtures. If you need to run doctests against

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ testpaths = [
377377
]
378378
norecursedirs = [
379379
"testing/example_scripts",
380+
"testing/plugins_integration",
380381
".*",
381382
"build",
382383
"dist",

‎src/_pytest/doctest.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,7 @@ def _from_module(self, module, object):
552552
else:
553553
raise
554554

555-
# While doctests currently don't support fixtures directly, we still
556-
# need to pick up autouse fixtures.
555+
# doctests supports fixtures via `getfixture` and autouse.
557556
self.session._fixturemanager.parsefactories(self)
558557

559558
# Uses internal doctest module parsing mechanism.

‎src/_pytest/fixtures.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,6 @@ def __init__(self, session: Session) -> None:
17071707
# TODO: The order of the FixtureDefs list of each arg is significant,
17081708
# explain.
17091709
self._arg2fixturedefs: Final[dict[str, list[FixtureDef[Any]]]] = {}
1710-
self._holderobjseen: Final[set[object]] = set()
17111710
# A mapping from a node to a list of autouse fixture names it defines.
17121711
# The Session entry holds global usefixtures from config.
17131712
self._node_autousenames: Final[dict[nodes.Node, list[str]]] = {
@@ -2070,16 +2069,13 @@ def parsefactories(
20702069
assert isinstance(node_or_obj, nodes.Node)
20712070
holderobj = cast(object, node_or_obj.obj) # type: ignore[attr-defined]
20722071
effective_node = node_or_obj
2073-
if holderobj in self._holderobjseen:
2074-
return
20752072

20762073
# Avoid accessing `@property` (and other descriptors) when iterating fixtures.
20772074
if not safe_isclass(holderobj) and not isinstance(holderobj, types.ModuleType):
20782075
holderobj_tp: object = type(holderobj)
20792076
else:
20802077
holderobj_tp = holderobj
20812078

2082-
self._holderobjseen.add(holderobj)
20832079
for name in dir(holderobj):
20842080
# The attribute can be an arbitrary descriptor, so the attribute
20852081
# access below can raise. safe_getattr() ignores such exceptions.

‎testing/test_doctest.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,37 @@ def test_doctestmodule_with_fixtures(self, pytester: Pytester):
600600
reprec = pytester.inline_run(p, "--doctest-modules")
601601
reprec.assertoutcome(passed=1)
602602

603+
def test_module_fixture_available_to_normal_test_with_doctestmodules(
604+
self, pytester: Pytester
605+
) -> None:
606+
"""Regression test for #14533.
607+
608+
Module-level fixtures collected with ``--doctest-modules`` are available
609+
both to normal tests and doctests in the same file.
610+
"""
611+
pytester.makepyfile(
612+
"""
613+
import pytest
614+
615+
@pytest.fixture
616+
def fix():
617+
return "fix"
618+
619+
def test(fix):
620+
assert fix == "fix"
621+
622+
def func():
623+
'''My function.
624+
625+
>>> getfixture("fix")
626+
'fix'
627+
'''
628+
"""
629+
)
630+
631+
result = pytester.runpytest("--doctest-modules")
632+
result.assert_outcomes(passed=2)
633+
603634
def test_doctestmodule_three_tests(self, pytester: Pytester):
604635
p = pytester.makepyfile(
605636
"""
@@ -1302,6 +1333,37 @@ def bar():
13021333
result = pytester.runpytest("--doctest-modules")
13031334
result.stdout.fnmatch_lines(["*2 passed*"])
13041335

1336+
def test_doctest_and_python_fixtures_not_shared(self, pytester: Pytester) -> None:
1337+
"""Fixture scopes are not shared between doctest and python modules.
1338+
1339+
This test is not meant as a hard behavioral test -- sharing scope is
1340+
also an acceptable behavior (see #14533). But this test ensures and
1341+
behavior change is done knowingly.
1342+
"""
1343+
pytester.makepyfile(
1344+
r"""
1345+
import pytest
1346+
1347+
@pytest.fixture(scope="session", autouse=True)
1348+
def auto():
1349+
with open("out", "a", encoding="utf-8") as f:
1350+
f.write("RUN\n")
1351+
1352+
def test():
1353+
pass
1354+
1355+
def func():
1356+
'''My function.
1357+
1358+
>>> 1 + 1
1359+
2
1360+
'''
1361+
"""
1362+
)
1363+
result = pytester.runpytest("--doctest-modules")
1364+
result.assert_outcomes(passed=2)
1365+
assert Path("out").read_text("utf-8").split() == ["RUN"] * 2
1366+
13051367
@pytest.mark.parametrize("scope", SCOPES)
13061368
@pytest.mark.parametrize("enable_doctest", [True, False])
13071369
def test_fixture_scopes(self, pytester, scope, enable_doctest):

‎tox.ini‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ description =
182182
pip_pre=true
183183
changedir = testing/plugins_integration
184184
deps = -rtesting/plugins_integration/requirements.txt
185+
allowlist_externals = pip
185186
setenv =
186187
PYTHONPATH=.
187188
commands =

0 commit comments

Comments
 (0)