Factor out build_summary_stats_line(), and add tests

--HG--
branch : esiegerman/summary_colors
This commit is contained in:
Eric Siegerman 2015-07-02 13:03:05 -04:00
parent 76497c2542
commit bfc3e48fd5
2 changed files with 75 additions and 19 deletions

View File

@ -487,26 +487,9 @@ class TerminalReporter:
def summary_stats(self): def summary_stats(self):
session_duration = time.time() - self._sessionstarttime session_duration = time.time() - self._sessionstarttime
(line, color) = build_summary_stats_line(self.stats)
keys = ("failed passed skipped deselected "
"xfailed xpassed warnings").split()
for key in self.stats.keys():
if key not in keys:
keys.append(key)
parts = []
for key in keys:
if key: # setup/teardown reports have an empty key, ignore them
val = self.stats.get(key, None)
if val:
parts.append("%d %s" % (len(val), key))
line = ", ".join(parts)
msg = "%s in %.2f seconds" % (line, session_duration) msg = "%s in %.2f seconds" % (line, session_duration)
markup = {color: True, 'bold': True}
markup = {'bold': True}
if 'failed' in self.stats or 'error' in self.stats:
markup = {'red': True, 'bold': True}
else:
markup = {'green': True, 'bold': True}
if self.verbosity >= 0: if self.verbosity >= 0:
self.write_sep("=", msg, **markup) self.write_sep("=", msg, **markup)
@ -542,3 +525,23 @@ def flatten(l):
else: else:
yield x yield x
def build_summary_stats_line(stats):
keys = ("failed passed skipped deselected "
"xfailed xpassed warnings").split()
for key in stats.keys():
if key not in keys:
keys.append(key)
parts = []
for key in keys:
if key: # setup/teardown reports have an empty key, ignore them
val = stats.get(key, None)
if val:
parts.append("%d %s" % (len(val), key))
line = ", ".join(parts)
if 'failed' in stats or 'error' in stats:
color = 'red'
else:
color = 'green'
return (line, color)

View File

@ -7,6 +7,7 @@ import pluggy
import sys import sys
from _pytest.terminal import TerminalReporter, repr_pythonversion, getreportopt from _pytest.terminal import TerminalReporter, repr_pythonversion, getreportopt
from _pytest.terminal import build_summary_stats_line
from _pytest import runner from _pytest import runner
def basic_run_report(item): def basic_run_report(item):
@ -718,3 +719,55 @@ def test_terminal_summary(testdir):
*==== hello ====* *==== hello ====*
world world
""") """)
@pytest.mark.parametrize("exp_color, exp_line, stats_arg", [
# The method under test only cares about the length of each
# dict value, not the actual contents, so tuples of anything
# suffice
("red", "1 failed", {"failed": (1,)}),
("red", "1 failed, 1 passed", {"failed": (1,), "passed": (1,)}),
("red", "1 error", {"error": (1,)}),
("red", "1 passed, 1 error", {"error": (1,), "passed": (1,)}),
# (a status that's not known to the code)
("green", "1 weird", {"weird": (1,)}),
("green", "1 passed, 1 weird", {"weird": (1,), "passed": (1,)}),
("green", "1 warnings", {"warnings": (1,)}),
("green", "1 passed, 1 warnings", {"warnings": (1,), "passed": (1,)}),
("green", "5 passed", {"passed": (1,2,3,4,5)}),
("green", "1 skipped", {"skipped": (1,)}),
("green", "1 passed, 1 skipped", {"skipped": (1,), "passed": (1,)}),
("green", "1 deselected", {"deselected": (1,)}),
("green", "1 passed, 1 deselected", {"deselected": (1,), "passed": (1,)}),
("green", "1 xfailed", {"xfailed": (1,)}),
("green", "1 passed, 1 xfailed", {"xfailed": (1,), "passed": (1,)}),
("green", "1 xpassed", {"xpassed": (1,)}),
("green", "1 passed, 1 xpassed", {"xpassed": (1,), "passed": (1,)}),
# No tests were found at all
("green", "", {}),
# A couple more complex combinations
("red", "1 failed, 2 passed, 3 xfailed",
{"passed": (1,2), "failed": (1,), "xfailed": (1,2,3)}),
("green", "1 passed, 2 skipped, 3 deselected, 2 xfailed",
{"passed": (1,),
"skipped": (1,2),
"deselected": (1,2,3),
"xfailed": (1,2)}),
])
def test_summary_stats(exp_line, exp_color, stats_arg):
(line, color) = build_summary_stats_line(stats_arg)
assert line == exp_line
assert color == exp_color