Skip to content

Commit bd37035

Browse files
summarize warning summaries if the number of locations is high
1 parent f77d606 commit bd37035

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-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
@@ -825,16 +825,32 @@ def summary_warnings(self):
825825
for wr in warning_reports:
826826
reports_grouped_by_message.setdefault(wr.message, []).append(wr)
827827

828-
title = "warnings summary (final)" if final else "warnings summary"
829-
self.write_sep("=", title, yellow=True, bold=False)
830-
for message, warning_reports in reports_grouped_by_message.items():
831-
has_any_location = False
828+
def collapsed_location_report(reports: List[WarningReport]):
829+
locations = []
832830
for w in warning_reports:
833831
location = w.get_location(self.config)
834832
if location:
835-
self._tw.line(str(location))
836-
has_any_location = True
837-
if has_any_location:
833+
locations.append(location)
834+
835+
if len(locations) < 10:
836+
return "\n".join(map(str, locations))
837+
838+
counts_by_filename = collections.Counter(
839+
str(loc).split("::", 1)[0] for loc in locations
840+
)
841+
return "\n".join(
842+
"{0}: {1} test{2} with warning{2}".format(
843+
k, v, "s" if v > 1 else ""
844+
)
845+
for k, v in counts_by_filename.items()
846+
)
847+
848+
title = "warnings summary (final)" if final else "warnings summary"
849+
self.write_sep("=", title, yellow=True, bold=False)
850+
for message, warning_reports in reports_grouped_by_message.items():
851+
maybe_location = collapsed_location_report(warning_reports)
852+
if maybe_location:
853+
self._tw.line(maybe_location)
838854
lines = message.splitlines()
839855
indented = "\n".join(" " + x for x in lines)
840856
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,19 @@ 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+
["test_group_warnings_by_message_summary.py: 120 tests with warnings"]
594+
)
595+
warning_code = 'warnings.warn(UserWarning("foo"))'
596+
assert warning_code in result.stdout.str()
597+
assert result.stdout.str().count(warning_code) == 1
598+
599+
587600
def test_pytest_configure_warning(testdir, recwarn):
588601
"""Issue 5115."""
589602
testdir.makeconftest(

0 commit comments

Comments
 (0)