Skip to content

summarize warning summaries if the number of locations is high #6834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/6834.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Excess warning summaries are now collapsed per file to ensure readable display of warning summaries.
30 changes: 23 additions & 7 deletions src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,16 +825,32 @@ def summary_warnings(self):
for wr in warning_reports:
reports_grouped_by_message.setdefault(wr.message, []).append(wr)

title = "warnings summary (final)" if final else "warnings summary"
self.write_sep("=", title, yellow=True, bold=False)
for message, warning_reports in reports_grouped_by_message.items():
has_any_location = False
def collapsed_location_report(reports: List[WarningReport]):
locations = []
for w in warning_reports:
location = w.get_location(self.config)
if location:
self._tw.line(str(location))
has_any_location = True
if has_any_location:
locations.append(location)

if len(locations) < 10:
return "\n".join(map(str, locations))

counts_by_filename = collections.Counter(
str(loc).split("::", 1)[0] for loc in locations
)
return "\n".join(
"{0}: {1} test{2} with warning{2}".format(
k, v, "s" if v > 1 else ""
)
for k, v in counts_by_filename.items()
)

title = "warnings summary (final)" if final else "warnings summary"
self.write_sep("=", title, yellow=True, bold=False)
for message, warning_reports in reports_grouped_by_message.items():
maybe_location = collapsed_location_report(warning_reports)
if maybe_location:
self._tw.line(maybe_location)
lines = message.splitlines()
indented = "\n".join(" " + x for x in lines)
message = indented.rstrip()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import warnings

import pytest


def func():
warnings.warn(UserWarning("foo"))


@pytest.fixture(params=range(20), autouse=True)
def repeat_hack(request):
return request.param


@pytest.mark.parametrize("i", range(5))
def test_foo(i):
func()


def test_bar():
func()
18 changes: 18 additions & 0 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,24 @@ def test_group_warnings_by_message(testdir):
assert result.stdout.str().count(warning_code) == 1


@pytest.mark.filterwarnings("ignore::pytest.PytestExperimentalApiWarning")
@pytest.mark.filterwarnings("always")
def test_group_warnings_by_message_summary(testdir):
testdir.copy_example("warnings/test_group_warnings_by_message_summary.py")
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
"test_group_warnings_by_message_summary.py: 120 tests with warnings",
"*test_group_warnings_by_message_summary.py:7: UserWarning: foo",
],
consecutive=True,
)
warning_code = 'warnings.warn(UserWarning("foo"))'
assert warning_code in result.stdout.str()
assert result.stdout.str().count(warning_code) == 1


def test_pytest_configure_warning(testdir, recwarn):
"""Issue 5115."""
testdir.makeconftest(
Expand Down