Skip to content

Commit f6dfca7

Browse files
Merge pull request #4229 from nicoddemus/fix-warning-location
Show node that originated a warning in the warnings summary
2 parents 4a69104 + 2ad43ee commit f6dfca7

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

changelog/4221.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug where the warning summary at the end of the test session was not showing the test where the warning was originated.

src/_pytest/terminal.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,11 +725,10 @@ def summary_warnings(self):
725725
# legacy warnings show their location explicitly, while standard warnings look better without
726726
# it because the location is already formatted into the message
727727
warning_records = list(warning_records)
728-
is_legacy = warning_records[0].legacy
729-
if location and is_legacy:
728+
if location:
730729
self._tw.line(str(location))
731730
for w in warning_records:
732-
if is_legacy:
731+
if location:
733732
lines = w.message.splitlines()
734733
indented = "\n".join(" " + x for x in lines)
735734
message = indented.rstrip()

testing/test_warnings.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def test_normal_flow(testdir, pyfile_with_warnings):
4848
result.stdout.fnmatch_lines(
4949
[
5050
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
51+
"test_normal_flow.py::test_func",
5152
"*normal_flow_module.py:3: UserWarning: user warning",
5253
'* warnings.warn(UserWarning("user warning"))',
5354
"*normal_flow_module.py:4: RuntimeWarning: runtime warning",
@@ -369,8 +370,8 @@ def test_foo():
369370
result.stdout.fnmatch_lines(
370371
[
371372
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
372-
"*collection_warnings.py:3: UserWarning: collection warning",
373-
' warnings.warn(UserWarning("collection warning"))',
373+
" *collection_warnings.py:3: UserWarning: collection warning",
374+
' warnings.warn(UserWarning("collection warning"))',
374375
"* 1 passed, 1 warnings*",
375376
]
376377
)

0 commit comments

Comments
 (0)