From be3b8fc9c1417243b9567bd31992c0c42f0bf898 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 23 Nov 2018 22:39:05 +0100 Subject: [PATCH] Fix warnings summary header appearing twice Ref: https://github.com/pytest-dev/pytest/pull/4450#discussion_r236017645 Ref: https://github.com/pytest-dev/pytest/pull/4399 --- doc/en/warnings.rst | 4 ---- doc/en/writing_plugins.rst | 2 -- src/_pytest/terminal.py | 2 ++ testing/test_terminal.py | 29 ++++++++++++++++++++++++++++- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/doc/en/warnings.rst b/doc/en/warnings.rst index 1317f020f..182aae4fa 100644 --- a/doc/en/warnings.rst +++ b/doc/en/warnings.rst @@ -33,8 +33,6 @@ Running pytest now produces this output:: $REGENDOC_TMPDIR/test_show_warnings.py:4: UserWarning: api v1, should use functions from v2 warnings.warn(UserWarning("api v1, should use functions from v2")) - -- Docs: https://docs.pytest.org/en/latest/warnings.html - ========================= warnings summary (final) ========================= -- Docs: https://docs.pytest.org/en/latest/warnings.html =================== 1 passed, 1 warnings in 0.12 seconds =================== @@ -358,8 +356,6 @@ defines an ``__init__`` constructor, as this prevents the class from being insta $REGENDOC_TMPDIR/test_pytest_warnings.py:1: PytestWarning: cannot collect test class 'Test' because it has a __init__ constructor class Test: - -- Docs: https://docs.pytest.org/en/latest/warnings.html - ========================= warnings summary (final) ========================= -- Docs: https://docs.pytest.org/en/latest/warnings.html 1 warnings in 0.12 seconds diff --git a/doc/en/writing_plugins.rst b/doc/en/writing_plugins.rst index e35577bf8..bee3d6102 100644 --- a/doc/en/writing_plugins.rst +++ b/doc/en/writing_plugins.rst @@ -422,8 +422,6 @@ additionally it is possible to copy examples for an example folder before runnin $REGENDOC_TMPDIR/test_example.py:4: PytestExperimentalApiWarning: testdir.copy_example is an experimental api that may change over time testdir.copy_example("test_example.py") - -- Docs: https://docs.pytest.org/en/latest/warnings.html - ========================= warnings summary (final) ========================= -- Docs: https://docs.pytest.org/en/latest/warnings.html =================== 2 passed, 1 warnings in 0.12 seconds =================== diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 3c9abac8a..6f3893653 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -734,6 +734,8 @@ class TerminalReporter(object): else: warnings = all_warnings self._already_displayed_warnings = len(warnings) + if not warnings: + return grouped = itertools.groupby( warnings, key=lambda wr: wr.get_location(self.config) diff --git a/testing/test_terminal.py b/testing/test_terminal.py index f1bfa81f2..86ec1cd07 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -1094,7 +1094,34 @@ def test_terminal_summary_warnings_are_displayed(testdir): ] ) assert "None" not in result.stdout.str() - assert result.stdout.str().count("warning_from_test") == 1 + stdout = result.stdout.str() + assert stdout.count("warning_from_test") == 1 + assert stdout.count("=== warnings summary ") == 2 + + +@pytest.mark.filterwarnings("default") +def test_terminal_summary_warnings_header_once(testdir): + testdir.makepyfile( + """ + def test_failure(): + import warnings + warnings.warn("warning_from_" + "test") + assert 0 + """ + ) + result = testdir.runpytest("-ra") + result.stdout.fnmatch_lines( + [ + "*= warnings summary =*", + "*warning_from_test*", + "*= short test summary info =*", + "*== 1 failed, 1 warnings in *", + ] + ) + assert "None" not in result.stdout.str() + stdout = result.stdout.str() + assert stdout.count("warning_from_test") == 1 + assert stdout.count("=== warnings summary ") == 1 @pytest.mark.parametrize(