Skip to content

Color percentage indicator #6107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/6097.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The "[XXX%]" indicator in the test summary is colored according to the final (new) multi-colored line's main color.
21 changes: 16 additions & 5 deletions src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from typing import Mapping
from typing import Optional
from typing import Set
from typing import Tuple

import attr
import pluggy
Expand Down Expand Up @@ -458,18 +459,20 @@ def pytest_runtest_logfinish(self, nodeid):
else:
progress_length = len(" [100%]")

main_color, _ = _get_main_color(self.stats)

self._progress_nodeids_reported.add(nodeid)
is_last_item = (
len(self._progress_nodeids_reported) == self._session.testscollected
)
if is_last_item:
self._write_progress_information_filling_space()
self._write_progress_information_filling_space(color=main_color)
else:
w = self._width_of_current_line
past_edge = w + progress_length + 1 >= self._screen_width
if past_edge:
msg = self._get_progress_information_message()
self._tw.write(msg + "\n", cyan=True)
self._tw.write(msg + "\n", **{main_color: True})

def _get_progress_information_message(self):
collected = self._session.testscollected
Expand All @@ -486,11 +489,13 @@ def _get_progress_information_message(self):
return " [{:3d}%]".format(progress)
return " [100%]"

def _write_progress_information_filling_space(self):
def _write_progress_information_filling_space(self, color=None):
if not color:
color, _ = _get_main_color(self.stats)
msg = self._get_progress_information_message()
w = self._width_of_current_line
fill = self._tw.fullwidth - w - 1
self.write(msg.rjust(fill), cyan=True)
self.write(msg.rjust(fill), **{color: True})

@property
def _width_of_current_line(self):
Expand Down Expand Up @@ -1075,7 +1080,7 @@ def _make_plural(count, noun):
return count, noun + "s" if count != 1 else noun


def build_summary_stats_line(stats):
def _get_main_color(stats) -> Tuple[str, List[str]]:
known_types = (
"failed passed skipped deselected xfailed xpassed warnings error".split()
)
Expand All @@ -1096,6 +1101,12 @@ def build_summary_stats_line(stats):
else:
main_color = "yellow"

return main_color, known_types


def build_summary_stats_line(stats):
main_color, known_types = _get_main_color(stats)

parts = []
for key in known_types:
reports = stats.get(key, None)
Expand Down
45 changes: 43 additions & 2 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
from _pytest.terminal import TerminalReporter

DistInfo = collections.namedtuple("DistInfo", ["project_name", "version"])
RED = r"\x1b\[31m"
GREEN = r"\x1b\[32m"
YELLOW = r"\x1b\[33m"
RESET = r"\x1b\[0m"


class Option:
Expand Down Expand Up @@ -1494,6 +1498,43 @@ def test_normal(self, many_tests_files, testdir):
]
)

def test_colored_progress(self, testdir, monkeypatch):
monkeypatch.setenv("PY_COLORS", "1")
testdir.makepyfile(
test_bar="""
import pytest
@pytest.mark.parametrize('i', range(10))
def test_bar(i): pass
""",
test_foo="""
import pytest
import warnings
@pytest.mark.parametrize('i', range(5))
def test_foo(i):
warnings.warn(DeprecationWarning("collection"))
pass
""",
test_foobar="""
import pytest
@pytest.mark.parametrize('i', range(5))
def test_foobar(i): raise ValueError()
""",
)
output = testdir.runpytest()
output.stdout.re_match_lines(
[
r"test_bar.py ({green}\.{reset}){{10}}{green} \s+ \[ 50%\]{reset}".format(
green=GREEN, reset=RESET
),
r"test_foo.py ({green}\.{reset}){{5}}{yellow} \s+ \[ 75%\]{reset}".format(
green=GREEN, reset=RESET, yellow=YELLOW
),
r"test_foobar.py ({red}F{reset}){{5}}{red} \s+ \[100%\]{reset}".format(
reset=RESET, red=RED
),
]
)

def test_count(self, many_tests_files, testdir):
testdir.makeini(
"""
Expand Down Expand Up @@ -1781,13 +1822,13 @@ def test_collecterror(testdir):
result = testdir.runpytest("-ra", str(p1))
result.stdout.fnmatch_lines(
[
"collected 0 items / 1 errors",
"collected 0 items / 1 error",
"*= ERRORS =*",
"*_ ERROR collecting test_collecterror.py _*",
"E SyntaxError: *",
"*= short test summary info =*",
"ERROR test_collecterror.py",
"*! Interrupted: 1 errors during collection !*",
"*! Interrupted: 1 error during collection !*",
"*= 1 error in *",
]
)

This comment was marked as resolved.