From bfc3e48fd57f8ae889efd14b2e9b440b08208dcf Mon Sep 17 00:00:00 2001 From: Eric Siegerman Date: Thu, 2 Jul 2015 13:03:05 -0400 Subject: [PATCH] Factor out build_summary_stats_line(), and add tests --HG-- branch : esiegerman/summary_colors --- _pytest/terminal.py | 41 +++++++++++++++++-------------- testing/test_terminal.py | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 19 deletions(-) diff --git a/_pytest/terminal.py b/_pytest/terminal.py index 576893bbf..82da2045d 100644 --- a/_pytest/terminal.py +++ b/_pytest/terminal.py @@ -487,26 +487,9 @@ class TerminalReporter: def summary_stats(self): session_duration = time.time() - self._sessionstarttime - - 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) + (line, color) = build_summary_stats_line(self.stats) msg = "%s in %.2f seconds" % (line, session_duration) - - markup = {'bold': True} - if 'failed' in self.stats or 'error' in self.stats: - markup = {'red': True, 'bold': True} - else: - markup = {'green': True, 'bold': True} + markup = {color: True, 'bold': True} if self.verbosity >= 0: self.write_sep("=", msg, **markup) @@ -542,3 +525,23 @@ def flatten(l): else: 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) diff --git a/testing/test_terminal.py b/testing/test_terminal.py index fba3021e0..9390ac153 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -7,6 +7,7 @@ import pluggy import sys from _pytest.terminal import TerminalReporter, repr_pythonversion, getreportopt +from _pytest.terminal import build_summary_stats_line from _pytest import runner def basic_run_report(item): @@ -718,3 +719,55 @@ def test_terminal_summary(testdir): *==== hello ====* 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 +