From 5e7b2ae704da84db5e9d4a786ebe1efe1997f9ff Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 23 Oct 2019 00:21:29 +0200 Subject: [PATCH] doctest: pytest_unconfigure: reset RUNNER_CLASS This is important when used with ``pytester``'s ``runpytest_inprocess``. Since 07f20ccab `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: FAILED test_doctest_id.txt::test_doctest_id.txt --- changelog/6039.bugfix.rst | 3 +++ src/_pytest/doctest.py | 6 ++++++ testing/acceptance_test.py | 25 +++++++++++++++---------- 3 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 changelog/6039.bugfix.rst diff --git a/changelog/6039.bugfix.rst b/changelog/6039.bugfix.rst new file mode 100644 index 00000000000..b13a677c8a1 --- /dev/null +++ b/changelog/6039.bugfix.rst @@ -0,0 +1,3 @@ +The ``PytestDoctestRunner`` is properly invalidated when unconfiguring the doctest plugin. + +This is important when used with ``pytester``'s ``runpytest_inprocess``. diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index 1bd2642aeb0..4c17e035873 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -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": diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index a5187644d99..2bf56cb80f8 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -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."""