Skip to content

Commit ac3a42b

Browse files
authored
doctest: strip newlines with unexpected exceptions (#6801)
1 parent be23aeb commit ac3a42b

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

changelog/6801.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Do not display empty lines inbetween traceback for unexpected exceptions with doctests.

src/_pytest/doctest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,10 @@ def repr_failure(self, excinfo):
313313
else:
314314
inner_excinfo = ExceptionInfo(failure.exc_info)
315315
lines += ["UNEXPECTED EXCEPTION: %s" % repr(inner_excinfo.value)]
316-
lines += traceback.format_exception(*failure.exc_info)
316+
lines += [
317+
x.strip("\n")
318+
for x in traceback.format_exception(*failure.exc_info)
319+
]
317320
reprlocation_lines.append((reprlocation, lines))
318321
return ReprFailDoctest(reprlocation_lines)
319322
else:

testing/test_doctest.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,21 @@ def test_doctest_unexpected_exception(self, testdir):
179179
result = testdir.runpytest("--doctest-modules")
180180
result.stdout.fnmatch_lines(
181181
[
182-
"*unexpected_exception*",
183-
"*>>> i = 0*",
184-
"*>>> 0 / i*",
185-
"*UNEXPECTED*ZeroDivision*",
186-
]
182+
"test_doctest_unexpected_exception.txt F *",
183+
"",
184+
"*= FAILURES =*",
185+
"*_ [[]doctest[]] test_doctest_unexpected_exception.txt _*",
186+
"001 >>> i = 0",
187+
"002 >>> 0 / i",
188+
"UNEXPECTED EXCEPTION: ZeroDivisionError*",
189+
"Traceback (most recent call last):",
190+
' File "*/doctest.py", line *, in __run',
191+
" *",
192+
' File "<doctest test_doctest_unexpected_exception.txt[1]>", line 1, in <module>',
193+
"ZeroDivisionError: division by zero",
194+
"*/test_doctest_unexpected_exception.txt:2: UnexpectedException",
195+
],
196+
consecutive=True,
187197
)
188198

189199
def test_doctest_outcomes(self, testdir):

0 commit comments

Comments
 (0)