diff --git a/changelog/6801.bugfix.rst b/changelog/6801.bugfix.rst new file mode 100644 index 000000000..0523f304e --- /dev/null +++ b/changelog/6801.bugfix.rst @@ -0,0 +1 @@ +Do not display empty lines inbetween traceback for unexpected exceptions with doctests. diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index 477ee8fd7..021084778 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -313,7 +313,10 @@ class DoctestItem(pytest.Item): else: inner_excinfo = ExceptionInfo(failure.exc_info) lines += ["UNEXPECTED EXCEPTION: %s" % repr(inner_excinfo.value)] - lines += traceback.format_exception(*failure.exc_info) + lines += [ + x.strip("\n") + for x in traceback.format_exception(*failure.exc_info) + ] reprlocation_lines.append((reprlocation, lines)) return ReprFailDoctest(reprlocation_lines) else: diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 8ce834d5b..c9defec5d 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -179,11 +179,21 @@ class TestDoctests: result = testdir.runpytest("--doctest-modules") result.stdout.fnmatch_lines( [ - "*unexpected_exception*", - "*>>> i = 0*", - "*>>> 0 / i*", - "*UNEXPECTED*ZeroDivision*", - ] + "test_doctest_unexpected_exception.txt F *", + "", + "*= FAILURES =*", + "*_ [[]doctest[]] test_doctest_unexpected_exception.txt _*", + "001 >>> i = 0", + "002 >>> 0 / i", + "UNEXPECTED EXCEPTION: ZeroDivisionError*", + "Traceback (most recent call last):", + ' File "*/doctest.py", line *, in __run', + " *", + ' File "", line 1, in ', + "ZeroDivisionError: division by zero", + "*/test_doctest_unexpected_exception.txt:2: UnexpectedException", + ], + consecutive=True, ) def test_doctest_outcomes(self, testdir):