From 0b70300ba4c00f2fdab1415b33ac6b035418e648 Mon Sep 17 00:00:00 2001 From: piotrhm Date: Mon, 25 May 2020 14:18:57 +0200 Subject: [PATCH 1/7] Added requested modifications --- AUTHORS | 1 + changelog/6471.feature.rst | 1 + src/_pytest/terminal.py | 63 ++++++++++++++++++++++++++------------ 3 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 changelog/6471.feature.rst diff --git a/AUTHORS b/AUTHORS index fdcd5b6e0..821a7d8f4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -227,6 +227,7 @@ Pedro Algarvio Philipp Loose Pieter Mulder Piotr Banaszkiewicz +Piotr Helm Prashant Anand Pulkit Goyal Punyashloka Biswal diff --git a/changelog/6471.feature.rst b/changelog/6471.feature.rst new file mode 100644 index 000000000..bc2d7564e --- /dev/null +++ b/changelog/6471.feature.rst @@ -0,0 +1 @@ +New flags --no-header and --no-summary added. The first one disables header, the second one disables summary. \ No newline at end of file diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 9c2665fb8..2a98e4ceb 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -115,6 +115,20 @@ def pytest_addoption(parser: Parser) -> None: dest="verbose", help="increase verbosity.", ) + group._addoption( + "--no-header", + action="count", + default=0, + dest="no_header", + help="disable header", + ) + group._addoption( + "--no-summary", + action="count", + default=0, + dest="no_summary", + help="disable summary", + ) group._addoption( "-q", "--quiet", @@ -351,6 +365,14 @@ class TerminalReporter: def showheader(self) -> bool: return self.verbosity >= 0 + @property + def no_header(self) -> bool: + return self.config.option.no_header + + @property + def no_summary(self) -> bool: + return self.config.option.no_summary + @property def showfspath(self) -> bool: if self._showfspath is None: @@ -660,25 +682,26 @@ class TerminalReporter: return self.write_sep("=", "test session starts", bold=True) verinfo = platform.python_version() - msg = "platform {} -- Python {}".format(sys.platform, verinfo) - pypy_version_info = getattr(sys, "pypy_version_info", None) - if pypy_version_info: - verinfo = ".".join(map(str, pypy_version_info[:3])) - msg += "[pypy-{}-{}]".format(verinfo, pypy_version_info[3]) - msg += ", pytest-{}, py-{}, pluggy-{}".format( - pytest.__version__, py.__version__, pluggy.__version__ - ) - if ( - self.verbosity > 0 - or self.config.option.debug - or getattr(self.config.option, "pastebin", None) - ): - msg += " -- " + str(sys.executable) - self.write_line(msg) - lines = self.config.hook.pytest_report_header( - config=self.config, startdir=self.startdir - ) - self._write_report_lines_from_hooks(lines) + if self.no_header == 0: + msg = "platform {} -- Python {}".format(sys.platform, verinfo) + pypy_version_info = getattr(sys, "pypy_version_info", None) + if pypy_version_info: + verinfo = ".".join(map(str, pypy_version_info[:3])) + msg += "[pypy-{}-{}]".format(verinfo, pypy_version_info[3]) + msg += ", pytest-{}, py-{}, pluggy-{}".format( + pytest.__version__, py.__version__, pluggy.__version__ + ) + if ( + self.verbosity > 0 + or self.config.option.debug + or getattr(self.config.option, "pastebin", None) + ): + msg += " -- " + str(sys.executable) + self.write_line(msg) + lines = self.config.hook.pytest_report_header( + config=self.config, startdir=self.startdir + ) + self._write_report_lines_from_hooks(lines) def _write_report_lines_from_hooks( self, lines: List[Union[str, List[str]]] @@ -775,7 +798,7 @@ class TerminalReporter: ExitCode.USAGE_ERROR, ExitCode.NO_TESTS_COLLECTED, ) - if exitstatus in summary_exit_codes: + if exitstatus in summary_exit_codes and self.no_summary == 0: self.config.hook.pytest_terminal_summary( terminalreporter=self, exitstatus=exitstatus, config=self.config ) From 51fb11c1d1436fb438cfe4d014b34c46fc342b70 Mon Sep 17 00:00:00 2001 From: piotrhm Date: Mon, 25 May 2020 19:29:18 +0200 Subject: [PATCH 2/7] Added tests --- changelog/6471.feature.rst | 2 +- src/_pytest/terminal.py | 12 +++---- testing/test_terminal.py | 74 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/changelog/6471.feature.rst b/changelog/6471.feature.rst index bc2d7564e..ebc9a208d 100644 --- a/changelog/6471.feature.rst +++ b/changelog/6471.feature.rst @@ -1 +1 @@ -New flags --no-header and --no-summary added. The first one disables header, the second one disables summary. \ No newline at end of file +New flags --no-header and --no-summary added. The first one disables header, the second one disables summary. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 2a98e4ceb..6a4a609b4 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -117,15 +117,15 @@ def pytest_addoption(parser: Parser) -> None: ) group._addoption( "--no-header", - action="count", - default=0, + action="store_true", + default=False, dest="no_header", help="disable header", ) group._addoption( "--no-summary", - action="count", - default=0, + action="store_true", + default=False, dest="no_summary", help="disable summary", ) @@ -682,7 +682,7 @@ class TerminalReporter: return self.write_sep("=", "test session starts", bold=True) verinfo = platform.python_version() - if self.no_header == 0: + if not self.no_header: msg = "platform {} -- Python {}".format(sys.platform, verinfo) pypy_version_info = getattr(sys, "pypy_version_info", None) if pypy_version_info: @@ -798,7 +798,7 @@ class TerminalReporter: ExitCode.USAGE_ERROR, ExitCode.NO_TESTS_COLLECTED, ) - if exitstatus in summary_exit_codes and self.no_summary == 0: + if exitstatus in summary_exit_codes and not self.no_summary: self.config.hook.pytest_terminal_summary( terminalreporter=self, exitstatus=exitstatus, config=self.config ) diff --git a/testing/test_terminal.py b/testing/test_terminal.py index e8402079b..8b8c246ba 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -698,6 +698,29 @@ class TestTerminalFunctional: if request.config.pluginmanager.list_plugin_distinfo(): result.stdout.fnmatch_lines(["plugins: *"]) + def test_no_header_trailer_info(self, testdir, request): + testdir.monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD") + testdir.makepyfile( + """ + def test_passes(): + pass + """ + ) + result = testdir.runpytest("--no-header") + verinfo = ".".join(map(str, sys.version_info[:3])) + result.stdout.no_fnmatch_line( + "platform %s -- Python %s*pytest-%s*py-%s*pluggy-%s" + % ( + sys.platform, + verinfo, + pytest.__version__, + py.__version__, + pluggy.__version__, + ) + ) + if request.config.pluginmanager.list_plugin_distinfo(): + result.stdout.no_fnmatch_line("plugins: *") + def test_header(self, testdir): testdir.tmpdir.join("tests").ensure_dir() testdir.tmpdir.join("gui").ensure_dir() @@ -727,6 +750,36 @@ class TestTerminalFunctional: result = testdir.runpytest("tests") result.stdout.fnmatch_lines(["rootdir: *test_header0, configfile: tox.ini"]) + def test_no_header(self, testdir): + testdir.tmpdir.join("tests").ensure_dir() + testdir.tmpdir.join("gui").ensure_dir() + + # with testpaths option, and not passing anything in the command-line + testdir.makeini( + """ + [pytest] + testpaths = tests gui + """ + ) + result = testdir.runpytest("--no-header") + result.stdout.no_fnmatch_line( + "rootdir: *test_header0, inifile: tox.ini, testpaths: tests, gui" + ) + + # with testpaths option, passing directory in command-line: do not show testpaths then + result = testdir.runpytest("tests", "--no-header") + result.stdout.no_fnmatch_line("rootdir: *test_header0, inifile: tox.ini") + + def test_no_summary(self, testdir): + p1 = testdir.makepyfile( + """ + def test_no_summary(): + assert false + """ + ) + result = testdir.runpytest("--no-summary") + result.stdout.no_fnmatch_line("*= FAILURES =*") + def test_showlocals(self, testdir): p1 = testdir.makepyfile( """ @@ -1483,6 +1536,21 @@ def test_terminal_summary_warnings_header_once(testdir): assert stdout.count("=== warnings summary ") == 1 +@pytest.mark.filterwarnings("default") +def test_terminal_no_summary_warnings_header_once(testdir): + testdir.makepyfile( + """ + def test_failure(): + import warnings + warnings.warn("warning_from_" + "test") + assert 0 + """ + ) + result = testdir.runpytest("--no-summary") + result.stdout.no_fnmatch_line("*= warnings summary =*") + result.stdout.no_fnmatch_line("*= short test summary info =*") + + @pytest.fixture(scope="session") def tr() -> TerminalReporter: config = _pytest.config._prepareconfig() @@ -2130,6 +2198,12 @@ def test_collecterror(testdir): ) +def test_no_summary_collecterror(testdir): + p1 = testdir.makepyfile("raise SyntaxError()") + result = testdir.runpytest("-ra", "--no-summary", str(p1)) + result.stdout.no_fnmatch_line("*= ERRORS =*") + + def test_via_exec(testdir: Testdir) -> None: p1 = testdir.makepyfile("exec('def test_via_exec(): pass')") result = testdir.runpytest(str(p1), "-vv") From 5e0e12d69b2494135e35ef3dcba9434daa932914 Mon Sep 17 00:00:00 2001 From: piotrhm Date: Mon, 25 May 2020 19:36:57 +0200 Subject: [PATCH 3/7] Fixed linting --- testing/test_terminal.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 8b8c246ba..5b6c7109a 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -709,14 +709,14 @@ class TestTerminalFunctional: result = testdir.runpytest("--no-header") verinfo = ".".join(map(str, sys.version_info[:3])) result.stdout.no_fnmatch_line( - "platform %s -- Python %s*pytest-%s*py-%s*pluggy-%s" - % ( - sys.platform, - verinfo, - pytest.__version__, - py.__version__, - pluggy.__version__, - ) + "platform %s -- Python %s*pytest-%s*py-%s*pluggy-%s" + % ( + sys.platform, + verinfo, + pytest.__version__, + py.__version__, + pluggy.__version__, + ) ) if request.config.pluginmanager.list_plugin_distinfo(): result.stdout.no_fnmatch_line("plugins: *") From 2be1c61eb3a0c202df4ca9ee0d764b5bdaad2001 Mon Sep 17 00:00:00 2001 From: piotrhm Date: Mon, 25 May 2020 20:07:10 +0200 Subject: [PATCH 4/7] Fixed linting 2 --- testing/test_terminal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 5b6c7109a..47243335b 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -779,7 +779,7 @@ class TestTerminalFunctional: ) result = testdir.runpytest("--no-summary") result.stdout.no_fnmatch_line("*= FAILURES =*") - + def test_showlocals(self, testdir): p1 = testdir.makepyfile( """ From df562533ffc467dda8da94c1d87f0722851223eb Mon Sep 17 00:00:00 2001 From: piotrhm Date: Wed, 27 May 2020 12:38:21 +0200 Subject: [PATCH 5/7] Fixed test --- testing/test_terminal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 47243335b..f1481dce5 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -777,7 +777,7 @@ class TestTerminalFunctional: assert false """ ) - result = testdir.runpytest("--no-summary") + result = testdir.runpytest(p1, "--no-summary") result.stdout.no_fnmatch_line("*= FAILURES =*") def test_showlocals(self, testdir): From d5a8bf7c6cfed4950b758a5539fb229497b7dca8 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 8 Jun 2020 22:13:29 -0300 Subject: [PATCH 6/7] Improve CHANGELOG --- changelog/6471.feature.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/changelog/6471.feature.rst b/changelog/6471.feature.rst index ebc9a208d..28457b9f5 100644 --- a/changelog/6471.feature.rst +++ b/changelog/6471.feature.rst @@ -1 +1,4 @@ -New flags --no-header and --no-summary added. The first one disables header, the second one disables summary. +New command-line flags: + +* `--no-header`: disables the initial header, including platform, version, and plugins. +* `--no-summary`: disables the final test summary, including warnings. From 357f9b6e839d6f7021904b28d974933aeb0f219b Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 8 Jun 2020 22:23:28 -0300 Subject: [PATCH 7/7] Add type annotations --- src/_pytest/terminal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 6a4a609b4..4168f3122 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -367,11 +367,11 @@ class TerminalReporter: @property def no_header(self) -> bool: - return self.config.option.no_header + return bool(self.config.option.no_header) @property def no_summary(self) -> bool: - return self.config.option.no_summary + return bool(self.config.option.no_summary) @property def showfspath(self) -> bool: