Skip to content

Commit 59bc6ef

Browse files
authored
Merge pull request #6498 from blueyed/test_terminal_colors
test_terminal: improve color handling
2 parents 1971033 + d4d04e7 commit 59bc6ef

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

testing/test_terminal.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
import collections
55
import os
6+
import re
67
import sys
78
import textwrap
89
from io import StringIO
@@ -21,10 +22,14 @@
2122
from _pytest.terminal import TerminalReporter
2223

2324
DistInfo = collections.namedtuple("DistInfo", ["project_name", "version"])
24-
RED = r"\x1b\[31m"
25-
GREEN = r"\x1b\[32m"
26-
YELLOW = r"\x1b\[33m"
27-
RESET = r"\x1b\[0m"
25+
26+
COLORS = {
27+
"red": "\x1b[31m",
28+
"green": "\x1b[32m",
29+
"yellow": "\x1b[33m",
30+
"reset": "\x1b[0m",
31+
}
32+
RE_COLORS = {k: re.escape(v) for k, v in COLORS.items()}
2833

2934

3035
class Option:
@@ -1548,18 +1553,15 @@ def test_foo(i):
15481553
def test_foobar(i): raise ValueError()
15491554
""",
15501555
)
1551-
output = testdir.runpytest()
1552-
output.stdout.re_match_lines(
1556+
result = testdir.runpytest()
1557+
result.stdout.re_match_lines(
15531558
[
1554-
r"test_bar.py ({green}\.{reset}){{10}}{green} \s+ \[ 50%\]{reset}".format(
1555-
green=GREEN, reset=RESET
1556-
),
1557-
r"test_foo.py ({green}\.{reset}){{5}}{yellow} \s+ \[ 75%\]{reset}".format(
1558-
green=GREEN, reset=RESET, yellow=YELLOW
1559-
),
1560-
r"test_foobar.py ({red}F{reset}){{5}}{red} \s+ \[100%\]{reset}".format(
1561-
reset=RESET, red=RED
1562-
),
1559+
line.format(**RE_COLORS)
1560+
for line in [
1561+
r"test_bar.py ({green}\.{reset}){{10}}{green} \s+ \[ 50%\]{reset}",
1562+
r"test_foo.py ({green}\.{reset}){{5}}{yellow} \s+ \[ 75%\]{reset}",
1563+
r"test_foobar.py ({red}F{reset}){{5}}{red} \s+ \[100%\]{reset}",
1564+
]
15631565
]
15641566
)
15651567

0 commit comments

Comments
 (0)