Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ Pierre Sassoulas
Pieter Mulder
Piotr Banaszkiewicz
Piotr Helm
Poorva Barve
Poulami Sau
Prakhar Gurunani
Praneeth Kodumagulla
Expand Down
1 change: 1 addition & 0 deletions changelog/9924.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed doctest option flags leaking between docstrings after a failure, skip, or expected failure.
11 changes: 8 additions & 3 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,14 @@ def runtest(self) -> None:
_check_all_skipped(self.dtest)
self._disable_output_capturing_for_darwin()
failures: list[doctest.DocTestFailure] = []
# Type ignored because we change the type of `out` from what
# doctest expects.
self.runner.run(self.dtest, out=failures) # type: ignore[arg-type]
optionflags = self.runner.optionflags
try:
# Type ignored because we change the type of `out` from what
# doctest expects.
self.runner.run(self.dtest, out=failures) # type: ignore[arg-type]
finally:
# Our runner can raise before doctest restores its option flags.
self.runner.optionflags = optionflags
if failures:
raise MultipleDoctestFailures(failures)

Expand Down
82 changes: 82 additions & 0 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,35 @@ def test_doctest_outcomes(self, pytester: Pytester):
]
)

@pytest.mark.parametrize("outcome", ["skip", "xfail"])
@pytest.mark.parametrize("continue_on_failure", [False, True])
def test_optionflags_restored_after_outcome(
self, pytester: Pytester, outcome: str, continue_on_failure: bool
) -> None:
pytester.makepyfile(
f"""
import pytest

def first():
'''
>>> pytest.{outcome}("reason") # doctest: -ELLIPSIS
'''

def second():
'''
>>> print("foobar")
foo...
'''
"""
)
args = ["--doctest-modules"]
if continue_on_failure:
args.append("--doctest-continue-on-failure")
result = pytester.runpytest(*args)
result.assert_outcomes(
passed=1, skipped=int(outcome == "skip"), xfailed=int(outcome == "xfail")
)

def test_docstring_partial_context_around_error(self, pytester: Pytester):
"""Test that we show some context before the actual line of a failing
doctest.
Expand Down Expand Up @@ -679,6 +708,59 @@ def nice_meth(self):
reprec = pytester.inline_run(p, "--doctest-modules")
reprec.assertoutcome(failed=1, passed=1)

@pytest.mark.parametrize("continue_on_failure", [False, True])
@pytest.mark.parametrize("first_example", ["0.", "1 / 0"])
@pytest.mark.parametrize(
"optionflags, directive, second_example, second_output, passed",
[
pytest.param("ELLIPSIS", "+NUMBER", "1.", "0.", 0, id="enable-number"),
pytest.param(
"", "+ELLIPSIS", 'print("foobar")', "foo...", 0, id="enable-ellipsis"
),
pytest.param(
"ELLIPSIS",
"-ELLIPSIS",
'print("foobar")',
"foo...",
1,
id="disable-ellipsis",
),
],
)
def test_optionflags_do_not_leak_between_docstrings(
self,
pytester: Pytester,
continue_on_failure: bool,
first_example: str,
optionflags: str,
directive: str,
second_example: str,
second_output: str,
passed: int,
) -> None:
"""A failing docstring must not change another's comparison options (#9924)."""
pytester.makeini(f"[pytest]\ndoctest_optionflags = {optionflags}")
pytester.makepyfile(
f"""
def first():
'''
>>> {first_example} # doctest: {directive}
2.
'''

def second():
'''
>>> {second_example}
{second_output}
'''
"""
)
args = ["--doctest-modules"]
if continue_on_failure:
args.append("--doctest-continue-on-failure")
result = pytester.runpytest(*args)
result.assert_outcomes(failed=2 - passed, passed=passed)

def test_ignored_whitespace(self, pytester: Pytester):
pytester.makeini(
"""
Expand Down