Fix warnings summary

- replace "tests with warnings" with just warnings: they a) might not
  come from a test in the first place, and b) a single test might have
  multiple warnings.
- fix usage of (unused) argument to `collapsed_location_report`

Co-authored-by: Ran Benita <ran@unusedvar.com>
This commit is contained in:
Daniel Hahler 2020-04-05 16:10:05 +02:00 committed by Ran Benita
parent 80e5098408
commit 8cae78a18b
6 changed files with 75 additions and 48 deletions

View File

@ -840,7 +840,7 @@ class TerminalReporter:
def collapsed_location_report(reports: List[WarningReport]):
locations = []
for w in warning_reports:
for w in reports:
location = w.get_location(self.config)
if location:
locations.append(location)
@ -852,16 +852,14 @@ class TerminalReporter:
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 ""
)
"{}: {} warning{}".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)
for message, message_reports in reports_grouped_by_message.items():
maybe_location = collapsed_location_report(message_reports)
if maybe_location:
self._tw.line(maybe_location)
lines = message.splitlines()

View File

@ -3,14 +3,19 @@ import warnings
import pytest
def func():
warnings.warn(UserWarning("foo"))
def func(msg):
warnings.warn(UserWarning(msg))
@pytest.mark.parametrize("i", range(5))
def test_foo(i):
func()
func("foo")
def test_bar():
func()
def test_foo_1():
func("foo")
@pytest.mark.parametrize("i", range(5))
def test_bar(i):
func("bar")

View File

@ -1,21 +0,0 @@
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()

View File

@ -0,0 +1,21 @@
import warnings
import pytest
def func(msg):
warnings.warn(UserWarning(msg))
@pytest.mark.parametrize("i", range(20))
def test_foo(i):
func("foo")
def test_foo_1():
func("foo")
@pytest.mark.parametrize("i", range(20))
def test_bar(i):
func("bar")

View File

@ -0,0 +1,5 @@
from test_1 import func
def test_2():
func("foo")

View File

@ -574,35 +574,54 @@ def test_group_warnings_by_message(testdir):
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"test_group_warnings_by_message.py::test_foo[0]",
"test_group_warnings_by_message.py::test_foo[1]",
"test_group_warnings_by_message.py::test_foo[2]",
"test_group_warnings_by_message.py::test_foo[3]",
"test_group_warnings_by_message.py::test_foo[4]",
"test_group_warnings_by_message.py::test_bar",
]
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
"test_group_warnings_by_message.py::test_foo[[]0[]]",
"test_group_warnings_by_message.py::test_foo[[]1[]]",
"test_group_warnings_by_message.py::test_foo[[]2[]]",
"test_group_warnings_by_message.py::test_foo[[]3[]]",
"test_group_warnings_by_message.py::test_foo[[]4[]]",
"test_group_warnings_by_message.py::test_foo_1",
" */test_group_warnings_by_message.py:*: UserWarning: foo",
" warnings.warn(UserWarning(msg))",
"",
"test_group_warnings_by_message.py::test_bar[[]0[]]",
"test_group_warnings_by_message.py::test_bar[[]1[]]",
"test_group_warnings_by_message.py::test_bar[[]2[]]",
"test_group_warnings_by_message.py::test_bar[[]3[]]",
"test_group_warnings_by_message.py::test_bar[[]4[]]",
" */test_group_warnings_by_message.py:*: UserWarning: bar",
" warnings.warn(UserWarning(msg))",
"",
"-- Docs: *",
"*= 11 passed, 11 warnings *",
],
consecutive=True,
)
warning_code = 'warnings.warn(UserWarning("foo"))'
assert warning_code in result.stdout.str()
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")
testdir.copy_example("warnings/test_group_warnings_by_message_summary")
testdir.syspathinsert()
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",
"test_1.py: 21 warnings",
"test_2.py: 1 warning",
" */test_1.py:7: UserWarning: foo",
" warnings.warn(UserWarning(msg))",
"",
"test_1.py: 20 warnings",
" */test_1.py:7: UserWarning: bar",
" warnings.warn(UserWarning(msg))",
"",
"-- Docs: *",
"*= 42 passed, 42 warnings *",
],
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):