Merge pull request #6834 from RonnyPfannschmidt/fix-6833-summarize-warning-item-locations
summarize warning summaries if the number of locations is high
This commit is contained in:
commit
194b52145b
|
@ -0,0 +1 @@
|
|||
Excess warning summaries are now collapsed per file to ensure readable display of warning summaries.
|
|
@ -837,16 +837,32 @@ class TerminalReporter:
|
|||
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()
|
||||
|
|
|
@ -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()
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue