Skip to content
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/6906.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `--code-highlight` command line option to enable/disable code highlighting in terminal output.
3 changes: 2 additions & 1 deletion src/_pytest/_io/terminalwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def __init__(self, file: Optional[TextIO] = None) -> None:
self.hasmarkup = should_do_markup(file)
self._current_line = ""
self._terminal_width = None # type: Optional[int]
self.code_highlight = True

@property
def fullwidth(self) -> int:
Expand Down Expand Up @@ -180,7 +181,7 @@ def _write_source(self, lines: Sequence[str], indents: Sequence[str] = ()) -> No

def _highlight(self, source: str) -> str:
"""Highlight the given source code if we have markup support."""
if not self.hasmarkup:
if not self.hasmarkup or not self.code_highlight:
return source
try:
from pygments.formatters.terminal import TerminalFormatter
Expand Down
7 changes: 6 additions & 1 deletion src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,8 +1389,13 @@ def create_terminal_writer(
tw = TerminalWriter(file=file)
if config.option.color == "yes":
tw.hasmarkup = True
if config.option.color == "no":
elif config.option.color == "no":
tw.hasmarkup = False

if config.option.code_highlight == "yes":
tw.code_highlight = True
elif config.option.code_highlight == "no":
tw.code_highlight = False
return tw


Expand Down
6 changes: 6 additions & 0 deletions src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ def pytest_addoption(parser: Parser) -> None:
choices=["yes", "no", "auto"],
help="color terminal output (yes/no/auto).",
)
group._addoption(
"--code-highlight",
default="yes",
choices=["yes", "no"],
help="Whether code should be highlighted (only if --color is also enabled)",
)

parser.addini(
"console_output_style",
Expand Down
21 changes: 17 additions & 4 deletions testing/io/test_terminalwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,32 @@ def test_combining(self) -> None:


@pytest.mark.parametrize(
"has_markup, expected",
("has_markup", "code_highlight", "expected"),
[
pytest.param(
True, "{kw}assert{hl-reset} {number}0{hl-reset}\n", id="with markup"
True,
True,
"{kw}assert{hl-reset} {number}0{hl-reset}\n",
id="with markup and code_highlight",
),
pytest.param(
True, False, "assert 0\n", id="with markup but no code_highlight",
),
pytest.param(
False, True, "assert 0\n", id="without markup but with code_highlight",
),
pytest.param(
False, False, "assert 0\n", id="neither markup nor code_highlight",
),
pytest.param(False, "assert 0\n", id="no markup"),
],
)
def test_code_highlight(has_markup, expected, color_mapping):
def test_code_highlight(has_markup, code_highlight, expected, color_mapping):
f = io.StringIO()
tw = terminalwriter.TerminalWriter(f)
tw.hasmarkup = has_markup
tw.code_highlight = code_highlight
tw._write_source(["assert 0"])

assert f.getvalue().splitlines(keepends=True) == color_mapping.format([expected])

with pytest.raises(
Expand Down