Skip to content

Commit 4fe69d7

Browse files
committed
WIP: multiple colors with terminal summary_stats
Ref: pytest-dev#5060
1 parent b549438 commit 4fe69d7

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

src/_pytest/terminal.py

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -864,14 +864,40 @@ def _outrep_summary(self, rep):
864864

865865
def summary_stats(self):
866866
session_duration = time.time() - self._sessionstarttime
867-
(line, color) = build_summary_stats_line(self.stats)
868-
msg = "%s in %.2f seconds" % (line, session_duration)
869-
markup = {color: True, "bold": True}
867+
(parts, main_color) = build_summary_stats_line(self.stats)
868+
line_parts = []
869+
fullwidth = self._tw.fullwidth
870+
for text, markup in parts:
871+
with_markup = self._tw.markup(text, **markup)
872+
fullwidth += len(with_markup) - len(text)
873+
line_parts.append(with_markup)
874+
msg = ", ".join(line_parts)
875+
876+
main_markup = {main_color: True, "bold": True}
877+
duration = " in %.2f seconds" % session_duration
878+
duration_with_markup = self._tw.markup(duration, **main_markup)
879+
fullwidth += len(duration_with_markup) - len(duration)
880+
msg += duration_with_markup
881+
882+
markup_for_end_sep = self._tw.markup("", **main_markup)
883+
if markup_for_end_sep.endswith("\x1b[0m"):
884+
markup_for_end_sep = markup_for_end_sep[:-4]
885+
fullwidth += len(markup_for_end_sep)
886+
msg += markup_for_end_sep
870887

871888
if self.verbosity >= 0:
872-
self.write_sep("=", msg, **markup)
889+
self.write_sep("=", msg, fullwidth=fullwidth, **main_markup)
873890
if self.verbosity == -1:
874-
self.write_line(msg, **markup)
891+
self.write_line(msg, **main_markup)
892+
893+
894+
_color_for_type = {
895+
"failed": "red",
896+
"error": "red",
897+
"warnings": "yellow",
898+
"passed": "green",
899+
}
900+
_color_for_type_default = "yellow"
875901

876902

877903
def build_summary_stats_line(stats):
@@ -884,30 +910,32 @@ def build_summary_stats_line(stats):
884910
if found_type: # setup/teardown reports have an empty key, ignore them
885911
known_types.append(found_type)
886912
unknown_type_seen = True
913+
914+
# main color
915+
if "failed" in stats or "error" in stats:
916+
main_color = "red"
917+
elif "warnings" in stats or unknown_type_seen:
918+
main_color = "yellow"
919+
elif "passed" in stats:
920+
main_color = "green"
921+
else:
922+
main_color = "yellow"
923+
887924
parts = []
888925
for key in known_types:
889926
reports = stats.get(key, None)
890927
if reports:
891928
count = sum(
892929
1 for rep in reports if getattr(rep, "count_towards_summary", True)
893930
)
894-
parts.append("%d %s" % (count, key))
895-
896-
if parts:
897-
line = ", ".join(parts)
898-
else:
899-
line = "no tests ran"
931+
color = _color_for_type.get(key, _color_for_type_default)
932+
markup = {color: True, "bold": color == main_color}
933+
parts.append(("%d %s" % (count, key), markup))
900934

901-
if "failed" in stats or "error" in stats:
902-
color = "red"
903-
elif "warnings" in stats or unknown_type_seen:
904-
color = "yellow"
905-
elif "passed" in stats:
906-
color = "green"
907-
else:
908-
color = "yellow"
935+
if not parts:
936+
parts = [("no tests ran", {_color_for_type_default: True})]
909937

910-
return line, color
938+
return parts, main_color
911939

912940

913941
def _plugin_nameversions(plugininfo):

0 commit comments

Comments
 (0)