diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 52c04a49c..27467b6c7 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -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() diff --git a/testing/example_scripts/warnings/test_group_warnings_by_message.py b/testing/example_scripts/warnings/test_group_warnings_by_message.py index c736135b7..6985caa44 100644 --- a/testing/example_scripts/warnings/test_group_warnings_by_message.py +++ b/testing/example_scripts/warnings/test_group_warnings_by_message.py @@ -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") diff --git a/testing/example_scripts/warnings/test_group_warnings_by_message_summary.py b/testing/example_scripts/warnings/test_group_warnings_by_message_summary.py deleted file mode 100644 index 4f7df3d6d..000000000 --- a/testing/example_scripts/warnings/test_group_warnings_by_message_summary.py +++ /dev/null @@ -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() diff --git a/testing/example_scripts/warnings/test_group_warnings_by_message_summary/test_1.py b/testing/example_scripts/warnings/test_group_warnings_by_message_summary/test_1.py new file mode 100644 index 000000000..b8c11cb71 --- /dev/null +++ b/testing/example_scripts/warnings/test_group_warnings_by_message_summary/test_1.py @@ -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") diff --git a/testing/example_scripts/warnings/test_group_warnings_by_message_summary/test_2.py b/testing/example_scripts/warnings/test_group_warnings_by_message_summary/test_2.py new file mode 100644 index 000000000..636d04a55 --- /dev/null +++ b/testing/example_scripts/warnings/test_group_warnings_by_message_summary/test_2.py @@ -0,0 +1,5 @@ +from test_1 import func + + +def test_2(): + func("foo") diff --git a/testing/test_warnings.py b/testing/test_warnings.py index bf7fe51c6..51d1286b4 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -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):