diff --git a/AUTHORS b/AUTHORS index fdcd5b6e047..821a7d8f41e 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 00000000000..28457b9f537 --- /dev/null +++ b/changelog/6471.feature.rst @@ -0,0 +1,4 @@ +New command-line flags: + +* `--no-header`: disables the initial header, including platform, version, and plugins. +* `--no-summary`: disables the final test summary, including warnings. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 9c2665fb818..4168f31226b 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="store_true", + default=False, + dest="no_header", + help="disable header", + ) + group._addoption( + "--no-summary", + action="store_true", + default=False, + dest="no_summary", + help="disable summary", + ) group._addoption( "-q", "--quiet", @@ -351,6 +365,14 @@ def verbosity(self) -> int: def showheader(self) -> bool: return self.verbosity >= 0 + @property + def no_header(self) -> bool: + return bool(self.config.option.no_header) + + @property + def no_summary(self) -> bool: + return bool(self.config.option.no_summary) + @property def showfspath(self) -> bool: if self._showfspath is None: @@ -660,25 +682,26 @@ def pytest_sessionstart(self, session: "Session") -> None: 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 not self.no_header: + 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 @@ def pytest_sessionfinish( ExitCode.USAGE_ERROR, ExitCode.NO_TESTS_COLLECTED, ) - if exitstatus in summary_exit_codes: + 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 e8402079b6c..f1481dce5e4 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -698,6 +698,29 @@ def test_passes(): 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 @@ def test_header(self, testdir): 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(p1, "--no-summary") + result.stdout.no_fnmatch_line("*= FAILURES =*") + def test_showlocals(self, testdir): p1 = testdir.makepyfile( """ @@ -1483,6 +1536,21 @@ def test_failure(): 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")