Skip to content

Issue 6471 - Introduce new option to skip header and summary #7258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ Pedro Algarvio
Philipp Loose
Pieter Mulder
Piotr Banaszkiewicz
Piotr Helm
Prashant Anand
Pulkit Goyal
Punyashloka Biswal
Expand Down
4 changes: 4 additions & 0 deletions changelog/6471.feature.rst
Original file line number Diff line number Diff line change
@@ -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.
63 changes: 43 additions & 20 deletions src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]]]
Expand Down Expand Up @@ -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
)
Expand Down
74 changes: 74 additions & 0 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
"""
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down