Skip to content

Commit 194b521

Browse files
Merge pull request #6834 from RonnyPfannschmidt/fix-6833-summarize-warning-item-locations
summarize warning summaries if the number of locations is high
2 parents 15e1dd0 + 23c43a3 commit 194b521

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

changelog/6834.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Excess warning summaries are now collapsed per file to ensure readable display of warning summaries.

src/_pytest/terminal.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -837,16 +837,32 @@ def summary_warnings(self):
837837
for wr in warning_reports:
838838
reports_grouped_by_message.setdefault(wr.message, []).append(wr)
839839

840-
title = "warnings summary (final)" if final else "warnings summary"
841-
self.write_sep("=", title, yellow=True, bold=False)
842-
for message, warning_reports in reports_grouped_by_message.items():
843-
has_any_location = False
840+
def collapsed_location_report(reports: List[WarningReport]):
841+
locations = []
844842
for w in warning_reports:
845843
location = w.get_location(self.config)
846844
if location:
847-
self._tw.line(str(location))
848-
has_any_location = True
849-
if has_any_location:
845+
locations.append(location)
846+
847+
if len(locations) < 10:
848+
return "\n".join(map(str, locations))
849+
850+
counts_by_filename = collections.Counter(
851+
str(loc).split("::", 1)[0] for loc in locations
852+
)
853+
return "\n".join(
854+
"{0}: {1} test{2} with warning{2}".format(
855+
k, v, "s" if v > 1 else ""
856+
)
857+
for k, v in counts_by_filename.items()
858+
)
859+
860+
title = "warnings summary (final)" if final else "warnings summary"
861+
self.write_sep("=", title, yellow=True, bold=False)
862+
for message, warning_reports in reports_grouped_by_message.items():
863+
maybe_location = collapsed_location_report(warning_reports)
864+
if maybe_location:
865+
self._tw.line(maybe_location)
850866
lines = message.splitlines()
851867
indented = "\n".join(" " + x for x in lines)
852868
message = indented.rstrip()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import warnings
2+
3+
import pytest
4+
5+
6+
def func():
7+
warnings.warn(UserWarning("foo"))
8+
9+
10+
@pytest.fixture(params=range(20), autouse=True)
11+
def repeat_hack(request):
12+
return request.param
13+
14+
15+
@pytest.mark.parametrize("i", range(5))
16+
def test_foo(i):
17+
func()
18+
19+
20+
def test_bar():
21+
func()

testing/test_warnings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,24 @@ def test_group_warnings_by_message(testdir):
584584
assert result.stdout.str().count(warning_code) == 1
585585

586586

587+
@pytest.mark.filterwarnings("ignore::pytest.PytestExperimentalApiWarning")
588+
@pytest.mark.filterwarnings("always")
589+
def test_group_warnings_by_message_summary(testdir):
590+
testdir.copy_example("warnings/test_group_warnings_by_message_summary.py")
591+
result = testdir.runpytest()
592+
result.stdout.fnmatch_lines(
593+
[
594+
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
595+
"test_group_warnings_by_message_summary.py: 120 tests with warnings",
596+
"*test_group_warnings_by_message_summary.py:7: UserWarning: foo",
597+
],
598+
consecutive=True,
599+
)
600+
warning_code = 'warnings.warn(UserWarning("foo"))'
601+
assert warning_code in result.stdout.str()
602+
assert result.stdout.str().count(warning_code) == 1
603+
604+
587605
def test_pytest_configure_warning(testdir, recwarn):
588606
"""Issue 5115."""
589607
testdir.makeconftest(

0 commit comments

Comments
 (0)