Skip to content

Commit

Permalink
Refactor into TerminalReporter.short_test_summary
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Apr 7, 2019
1 parent c70ecd4 commit b8fe24f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 103 deletions.
196 changes: 95 additions & 101 deletions src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ def pytest_terminal_summary(self):
self.summary_failures()
self.summary_warnings()
yield
self.short_test_summary()
self.summary_passes()
# Display any extra warnings from teardown here (if any).
self.summary_warnings()
Expand Down Expand Up @@ -873,58 +874,101 @@ def summary_stats(self):
if self.verbosity == -1:
self.write_line(msg, **markup)

def short_test_summary(self):
if not self.reportchars:
return

def show_simple(lines, stat):
failed = self.stats.get(stat)
if failed:
config = self.config
for rep in failed:
verbose_word = _get_report_str(config, rep)
pos = _get_pos(config, rep)
lines.append("%s %s" % (verbose_word, pos))

def show_xfailed(lines):
xfailed = self.stats.get("xfailed")
if xfailed:
config = self.config
for rep in xfailed:
verbose_word = _get_report_str(config, rep)
pos = _get_pos(config, rep)
lines.append("%s %s" % (verbose_word, pos))
reason = rep.wasxfail
if reason:
lines.append(" " + str(reason))

def show_xpassed(lines):
xpassed = self.stats.get("xpassed")
if xpassed:
config = self.config
for rep in xpassed:
verbose_word = _get_report_str(config, rep)
pos = _get_pos(config, rep)
reason = rep.wasxfail
lines.append("%s %s %s" % (verbose_word, pos, reason))

def show_skipped(lines):
skipped = self.stats.get("skipped", [])
if skipped:
fskips = _folded_skips(skipped)
if fskips:
verbose_word = _get_report_str(self.config, report=skipped[0])
for num, fspath, lineno, reason in fskips:
if reason.startswith("Skipped: "):
reason = reason[9:]
if lineno is not None:
lines.append(
"%s [%d] %s:%d: %s"
% (verbose_word, num, fspath, lineno + 1, reason)
)
else:
lines.append(
"%s [%d] %s: %s" % (verbose_word, num, fspath, reason)
)

def shower(stat):
def show_(lines):
return show_simple(lines, stat)

return show_

def _get_report_str(config, report):
_category, _short, verbose = config.hook.pytest_report_teststatus(
report=report, config=config
)
return verbose

def _get_pos(config, rep):
nodeid = config.cwd_relative_nodeid(rep.nodeid)
return nodeid

REPORTCHAR_ACTIONS = {
"x": show_xfailed,
"X": show_xpassed,
"f": shower("failed"),
"F": shower("failed"),
"s": show_skipped,
"S": show_skipped,
"p": shower("passed"),
"E": shower("error"),
}

lines = []
for char in self.reportchars:
if char == "w": # XXX: https://github.com/pytest-dev/pytest/issues/5066
continue
action = REPORTCHAR_ACTIONS[char]
action(lines)

if lines:
self.write_sep("=", "short test summary info")
for line in lines:
self.write_line(line)

def pytest_terminal_summary(terminalreporter):
tr = terminalreporter
if not tr.reportchars:
return

lines = []
for char in tr.reportchars:
action = REPORTCHAR_ACTIONS.get(char, lambda tr, lines: None)
action(terminalreporter, lines)

if lines:
tr._tw.sep("=", "short test summary info")
for line in lines:
tr._tw.line(line)


def show_simple(terminalreporter, lines, stat):
failed = terminalreporter.stats.get(stat)
if failed:
config = terminalreporter.config
for rep in failed:
verbose_word = _get_report_str(config, rep)
pos = _get_pos(config, rep)
lines.append("%s %s" % (verbose_word, pos))


def show_xfailed(terminalreporter, lines):
xfailed = terminalreporter.stats.get("xfailed")
if xfailed:
config = terminalreporter.config
for rep in xfailed:
verbose_word = _get_report_str(config, rep)
pos = _get_pos(config, rep)
lines.append("%s %s" % (verbose_word, pos))
reason = rep.wasxfail
if reason:
lines.append(" " + str(reason))


def show_xpassed(terminalreporter, lines):
xpassed = terminalreporter.stats.get("xpassed")
if xpassed:
config = terminalreporter.config
for rep in xpassed:
verbose_word = _get_report_str(config, rep)
pos = _get_pos(config, rep)
reason = rep.wasxfail
lines.append("%s %s %s" % (verbose_word, pos, reason))


def folded_skips(skipped):

def _folded_skips(skipped):
d = {}
for event in skipped:
key = event.longrepr
Expand All @@ -946,56 +990,6 @@ def folded_skips(skipped):
return values


def show_skipped(terminalreporter, lines):
tr = terminalreporter
skipped = tr.stats.get("skipped", [])
if skipped:
fskips = folded_skips(skipped)
if fskips:
verbose_word = _get_report_str(terminalreporter.config, report=skipped[0])
for num, fspath, lineno, reason in fskips:
if reason.startswith("Skipped: "):
reason = reason[9:]
if lineno is not None:
lines.append(
"%s [%d] %s:%d: %s"
% (verbose_word, num, fspath, lineno + 1, reason)
)
else:
lines.append("%s [%d] %s: %s" % (verbose_word, num, fspath, reason))


def shower(stat):
def show_(terminalreporter, lines):
return show_simple(terminalreporter, lines, stat)

return show_


def _get_report_str(config, report):
_category, _short, verbose = config.hook.pytest_report_teststatus(
report=report, config=config
)
return verbose


def _get_pos(config, rep):
nodeid = config.cwd_relative_nodeid(rep.nodeid)
return nodeid


REPORTCHAR_ACTIONS = {
"x": show_xfailed,
"X": show_xpassed,
"f": shower("failed"),
"F": shower("failed"),
"s": show_skipped,
"S": show_skipped,
"p": shower("passed"),
"E": shower("error"),
}


def build_summary_stats_line(stats):
known_types = (
"failed passed skipped deselected xfailed xpassed warnings error".split()
Expand Down
4 changes: 2 additions & 2 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import pytest
from _pytest.main import EXIT_NOTESTSCOLLECTED
from _pytest.reports import BaseReport
from _pytest.terminal import _folded_skips
from _pytest.terminal import _plugin_nameversions
from _pytest.terminal import build_summary_stats_line
from _pytest.terminal import folded_skips
from _pytest.terminal import getreportopt
from _pytest.terminal import TerminalReporter

Expand Down Expand Up @@ -1552,7 +1552,7 @@ class X(object):
ev3.longrepr = longrepr
ev3.skipped = True

values = folded_skips([ev1, ev2, ev3])
values = _folded_skips([ev1, ev2, ev3])
assert len(values) == 1
num, fspath, lineno, reason = values[0]
assert num == 3
Expand Down

0 comments on commit b8fe24f

Please sign in to comment.