Skip to content

Commit 878bb14

Browse files
committed
Multiple colors with terminal summary_stats
Ref: #5060
1 parent 0f11a7a commit 878bb14

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

changelog/5061.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use multiple colors with terminal summary statistics.

src/_pytest/terminal.py

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -860,15 +860,41 @@ def _outrep_summary(self, rep):
860860
self._tw.line(content)
861861

862862
def summary_stats(self):
863-
session_duration = time.time() - self._sessionstarttime
864-
(line, color) = build_summary_stats_line(self.stats)
865-
msg = "{} in {}".format(line, format_session_duration(session_duration))
866-
markup = {color: True, "bold": True}
863+
if self.verbosity < -1:
864+
return
867865

868-
if self.verbosity >= 0:
869-
self.write_sep("=", msg, **markup)
870-
if self.verbosity == -1:
871-
self.write_line(msg, **markup)
866+
session_duration = time.time() - self._sessionstarttime
867+
(parts, main_color) = build_summary_stats_line(self.stats)
868+
line_parts = []
869+
870+
display_sep = self.verbosity >= 0
871+
if display_sep:
872+
fullwidth = self._tw.fullwidth
873+
for text, markup in parts:
874+
with_markup = self._tw.markup(text, **markup)
875+
if display_sep:
876+
fullwidth += len(with_markup) - len(text)
877+
line_parts.append(with_markup)
878+
msg = ", ".join(line_parts)
879+
880+
main_markup = {main_color: True}
881+
duration = " in {}".format(format_session_duration(session_duration))
882+
duration_with_markup = self._tw.markup(duration, **main_markup)
883+
if display_sep:
884+
fullwidth += len(duration_with_markup) - len(duration)
885+
msg += duration_with_markup
886+
887+
if display_sep:
888+
markup_for_end_sep = self._tw.markup("", **main_markup)
889+
if markup_for_end_sep.endswith("\x1b[0m"):
890+
markup_for_end_sep = markup_for_end_sep[:-4]
891+
fullwidth += len(markup_for_end_sep)
892+
msg += markup_for_end_sep
893+
894+
if display_sep:
895+
self.write_sep("=", msg, fullwidth=fullwidth, **main_markup)
896+
else:
897+
self.write_line(msg, **main_markup)
872898

873899
def short_test_summary(self):
874900
if not self.reportchars:
@@ -1007,6 +1033,15 @@ def _folded_skips(skipped):
10071033
return values
10081034

10091035

1036+
_color_for_type = {
1037+
"failed": "red",
1038+
"error": "red",
1039+
"warnings": "yellow",
1040+
"passed": "green",
1041+
}
1042+
_color_for_type_default = "yellow"
1043+
1044+
10101045
def build_summary_stats_line(stats):
10111046
known_types = (
10121047
"failed passed skipped deselected xfailed xpassed warnings error".split()
@@ -1017,30 +1052,32 @@ def build_summary_stats_line(stats):
10171052
if found_type: # setup/teardown reports have an empty key, ignore them
10181053
known_types.append(found_type)
10191054
unknown_type_seen = True
1055+
1056+
# main color
1057+
if "failed" in stats or "error" in stats:
1058+
main_color = "red"
1059+
elif "warnings" in stats or unknown_type_seen:
1060+
main_color = "yellow"
1061+
elif "passed" in stats:
1062+
main_color = "green"
1063+
else:
1064+
main_color = "yellow"
1065+
10201066
parts = []
10211067
for key in known_types:
10221068
reports = stats.get(key, None)
10231069
if reports:
10241070
count = sum(
10251071
1 for rep in reports if getattr(rep, "count_towards_summary", True)
10261072
)
1027-
parts.append("%d %s" % (count, key))
1028-
1029-
if parts:
1030-
line = ", ".join(parts)
1031-
else:
1032-
line = "no tests ran"
1073+
color = _color_for_type.get(key, _color_for_type_default)
1074+
markup = {color: True, "bold": color == main_color}
1075+
parts.append(("%d %s" % (count, key), markup))
10331076

1034-
if "failed" in stats or "error" in stats:
1035-
color = "red"
1036-
elif "warnings" in stats or unknown_type_seen:
1037-
color = "yellow"
1038-
elif "passed" in stats:
1039-
color = "green"
1040-
else:
1041-
color = "yellow"
1077+
if not parts:
1078+
parts = [("no tests ran", {_color_for_type_default: True})]
10421079

1043-
return line, color
1080+
return parts, main_color
10441081

10451082

10461083
def _plugin_nameversions(plugininfo):

0 commit comments

Comments
 (0)