Skip to content

Commit 2c37585

Browse files
authored
Merge pull request #7258 from piotrhm/issue_6471
2 parents f551cab + 357f9b6 commit 2c37585

File tree

3 files changed

+121
-20
lines changed

3 files changed

+121
-20
lines changed

changelog/6471.feature.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
New command-line flags:
2+
3+
* `--no-header`: disables the initial header, including platform, version, and plugins.
4+
* `--no-summary`: disables the final test summary, including warnings.

src/_pytest/terminal.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ def pytest_addoption(parser: Parser) -> None:
117117
dest="verbose",
118118
help="increase verbosity.",
119119
)
120+
group._addoption(
121+
"--no-header",
122+
action="store_true",
123+
default=False,
124+
dest="no_header",
125+
help="disable header",
126+
)
127+
group._addoption(
128+
"--no-summary",
129+
action="store_true",
130+
default=False,
131+
dest="no_summary",
132+
help="disable summary",
133+
)
120134
group._addoption(
121135
"-q",
122136
"--quiet",
@@ -354,6 +368,14 @@ def verbosity(self) -> int:
354368
def showheader(self) -> bool:
355369
return self.verbosity >= 0
356370

371+
@property
372+
def no_header(self) -> bool:
373+
return bool(self.config.option.no_header)
374+
375+
@property
376+
def no_summary(self) -> bool:
377+
return bool(self.config.option.no_summary)
378+
357379
@property
358380
def showfspath(self) -> bool:
359381
if self._showfspath is None:
@@ -663,25 +685,26 @@ def pytest_sessionstart(self, session: "Session") -> None:
663685
return
664686
self.write_sep("=", "test session starts", bold=True)
665687
verinfo = platform.python_version()
666-
msg = "platform {} -- Python {}".format(sys.platform, verinfo)
667-
pypy_version_info = getattr(sys, "pypy_version_info", None)
668-
if pypy_version_info:
669-
verinfo = ".".join(map(str, pypy_version_info[:3]))
670-
msg += "[pypy-{}-{}]".format(verinfo, pypy_version_info[3])
671-
msg += ", pytest-{}, py-{}, pluggy-{}".format(
672-
pytest.__version__, py.__version__, pluggy.__version__
673-
)
674-
if (
675-
self.verbosity > 0
676-
or self.config.option.debug
677-
or getattr(self.config.option, "pastebin", None)
678-
):
679-
msg += " -- " + str(sys.executable)
680-
self.write_line(msg)
681-
lines = self.config.hook.pytest_report_header(
682-
config=self.config, startdir=self.startdir
683-
)
684-
self._write_report_lines_from_hooks(lines)
688+
if not self.no_header:
689+
msg = "platform {} -- Python {}".format(sys.platform, verinfo)
690+
pypy_version_info = getattr(sys, "pypy_version_info", None)
691+
if pypy_version_info:
692+
verinfo = ".".join(map(str, pypy_version_info[:3]))
693+
msg += "[pypy-{}-{}]".format(verinfo, pypy_version_info[3])
694+
msg += ", pytest-{}, py-{}, pluggy-{}".format(
695+
pytest.__version__, py.__version__, pluggy.__version__
696+
)
697+
if (
698+
self.verbosity > 0
699+
or self.config.option.debug
700+
or getattr(self.config.option, "pastebin", None)
701+
):
702+
msg += " -- " + str(sys.executable)
703+
self.write_line(msg)
704+
lines = self.config.hook.pytest_report_header(
705+
config=self.config, startdir=self.startdir
706+
)
707+
self._write_report_lines_from_hooks(lines)
685708

686709
def _write_report_lines_from_hooks(
687710
self, lines: List[Union[str, List[str]]]
@@ -778,7 +801,7 @@ def pytest_sessionfinish(
778801
ExitCode.USAGE_ERROR,
779802
ExitCode.NO_TESTS_COLLECTED,
780803
)
781-
if exitstatus in summary_exit_codes:
804+
if exitstatus in summary_exit_codes and not self.no_summary:
782805
self.config.hook.pytest_terminal_summary(
783806
terminalreporter=self, exitstatus=exitstatus, config=self.config
784807
)

testing/test_terminal.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,29 @@ def test_passes():
698698
if request.config.pluginmanager.list_plugin_distinfo():
699699
result.stdout.fnmatch_lines(["plugins: *"])
700700

701+
def test_no_header_trailer_info(self, testdir, request):
702+
testdir.monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD")
703+
testdir.makepyfile(
704+
"""
705+
def test_passes():
706+
pass
707+
"""
708+
)
709+
result = testdir.runpytest("--no-header")
710+
verinfo = ".".join(map(str, sys.version_info[:3]))
711+
result.stdout.no_fnmatch_line(
712+
"platform %s -- Python %s*pytest-%s*py-%s*pluggy-%s"
713+
% (
714+
sys.platform,
715+
verinfo,
716+
pytest.__version__,
717+
py.__version__,
718+
pluggy.__version__,
719+
)
720+
)
721+
if request.config.pluginmanager.list_plugin_distinfo():
722+
result.stdout.no_fnmatch_line("plugins: *")
723+
701724
def test_header(self, testdir):
702725
testdir.tmpdir.join("tests").ensure_dir()
703726
testdir.tmpdir.join("gui").ensure_dir()
@@ -727,6 +750,36 @@ def test_header(self, testdir):
727750
result = testdir.runpytest("tests")
728751
result.stdout.fnmatch_lines(["rootdir: *test_header0, configfile: tox.ini"])
729752

753+
def test_no_header(self, testdir):
754+
testdir.tmpdir.join("tests").ensure_dir()
755+
testdir.tmpdir.join("gui").ensure_dir()
756+
757+
# with testpaths option, and not passing anything in the command-line
758+
testdir.makeini(
759+
"""
760+
[pytest]
761+
testpaths = tests gui
762+
"""
763+
)
764+
result = testdir.runpytest("--no-header")
765+
result.stdout.no_fnmatch_line(
766+
"rootdir: *test_header0, inifile: tox.ini, testpaths: tests, gui"
767+
)
768+
769+
# with testpaths option, passing directory in command-line: do not show testpaths then
770+
result = testdir.runpytest("tests", "--no-header")
771+
result.stdout.no_fnmatch_line("rootdir: *test_header0, inifile: tox.ini")
772+
773+
def test_no_summary(self, testdir):
774+
p1 = testdir.makepyfile(
775+
"""
776+
def test_no_summary():
777+
assert false
778+
"""
779+
)
780+
result = testdir.runpytest(p1, "--no-summary")
781+
result.stdout.no_fnmatch_line("*= FAILURES =*")
782+
730783
def test_showlocals(self, testdir):
731784
p1 = testdir.makepyfile(
732785
"""
@@ -1483,6 +1536,21 @@ def test_failure():
14831536
assert stdout.count("=== warnings summary ") == 1
14841537

14851538

1539+
@pytest.mark.filterwarnings("default")
1540+
def test_terminal_no_summary_warnings_header_once(testdir):
1541+
testdir.makepyfile(
1542+
"""
1543+
def test_failure():
1544+
import warnings
1545+
warnings.warn("warning_from_" + "test")
1546+
assert 0
1547+
"""
1548+
)
1549+
result = testdir.runpytest("--no-summary")
1550+
result.stdout.no_fnmatch_line("*= warnings summary =*")
1551+
result.stdout.no_fnmatch_line("*= short test summary info =*")
1552+
1553+
14861554
@pytest.fixture(scope="session")
14871555
def tr() -> TerminalReporter:
14881556
config = _pytest.config._prepareconfig()
@@ -2130,6 +2198,12 @@ def test_collecterror(testdir):
21302198
)
21312199

21322200

2201+
def test_no_summary_collecterror(testdir):
2202+
p1 = testdir.makepyfile("raise SyntaxError()")
2203+
result = testdir.runpytest("-ra", "--no-summary", str(p1))
2204+
result.stdout.no_fnmatch_line("*= ERRORS =*")
2205+
2206+
21332207
def test_via_exec(testdir: Testdir) -> None:
21342208
p1 = testdir.makepyfile("exec('def test_via_exec(): pass')")
21352209
result = testdir.runpytest(str(p1), "-vv")

0 commit comments

Comments
 (0)