Skip to content

Commit

Permalink
doctest: pytest_unconfigure: reset RUNNER_CLASS
Browse files Browse the repository at this point in the history
This is important when used with ``pytester``'s ``runpytest_inprocess``.

Since 07f20cc `pytest testing/acceptance_test.py -k test_doctest_id`
would fail, since the second run would not consider the exception to be
an instance of `doctest.DocTestFailure` anymore, since the module was
re-imported, and use another failure message then in the short test
summary info (and in the report itself):

> FAILED test_doctest_id.txt::test_doctest_id.txt - doctest.DocTestFailure: <Do...

while it should be:

> FAILED test_doctest_id.txt::test_doctest_id.txt
  • Loading branch information
blueyed committed Oct 23, 2019
1 parent 98fc937 commit 5e7b2ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
3 changes: 3 additions & 0 deletions changelog/6039.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The ``PytestDoctestRunner`` is properly invalidated when unconfiguring the doctest plugin.

This is important when used with ``pytester``'s ``runpytest_inprocess``.
6 changes: 6 additions & 0 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ def pytest_addoption(parser):
)


def pytest_unconfigure():
global RUNNER_CLASS

RUNNER_CLASS = None


def pytest_collect_file(path, parent):
config = parent.config
if path.ext == ".py":
Expand Down
25 changes: 15 additions & 10 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,16 +859,21 @@ def test_doctest_id(self, testdir):
4
""",
)
result = testdir.runpytest("-rf")
lines = result.stdout.str().splitlines()
for line in lines:
if line.startswith(("FAIL ", "FAILED ")):
_fail, _sep, testid = line.partition(" ")
break
result = testdir.runpytest(testid, "-rf")
result.stdout.fnmatch_lines(
["FAILED test_doctest_id.txt::test_doctest_id.txt", "*1 failed*"]
)
testid = "test_doctest_id.txt::test_doctest_id.txt"
expected_lines = [
"*= FAILURES =*",
"*_ ?doctest? test_doctest_id.txt _*",
"FAILED test_doctest_id.txt::test_doctest_id.txt",
"*= 1 failed in*",
]
result = testdir.runpytest(testid, "-rf", "--tb=short")
result.stdout.fnmatch_lines(expected_lines)

# Ensure that re-running it will still handle it as
# doctest.DocTestFailure, which was not the case before when
# re-importing doctest, but not creating a new RUNNER_CLASS.
result = testdir.runpytest(testid, "-rf", "--tb=short")
result.stdout.fnmatch_lines(expected_lines)

def test_core_backward_compatibility(self):
"""Test backward compatibility for get_plugin_manager function. See #787."""
Expand Down

0 comments on commit 5e7b2ae

Please sign in to comment.