question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pytest colored output (with --color=yes) makes the test output log unreadable (by humans)

See original GitHub issue

VS Code version

1.62.3

Extension version

v2021.11.1422169775

OS type

macOS

OS version

12.0

Python distribution

Other

Python version

3.9

Language server

Pylance

Expected behaviour

I expect the output log in “Python Test Log” to not display the control characters, for instance by stripping them, that is obtaining the following output:

============================= test session starts ==============================
platform darwin -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-0.13.1
rootdir: /Users/ogrisel/code/scikit-learn, configfile: setup.cfg
plugins: anyio-3.3.0, xdist-2.3.0, timeout-1.4.2, forked-1.3.0
collected 1 item

sklearn/impute/tests/test_base.py .                                      [100%]

- generated xml file: /var/folders/_y/lfnx34p13w3_sr2k12bjb05w0000gn/T/tmp-17017Jlww71GJ2jF6.xml -
============================== 1 passed in 0.07s ===============================

Alternatively, colors could be rendered in the log as they are when running the tests in a shell terminal:

image

Actual behaviour

The “Python Test Log” displays the color control characters such as  without rendering them:

============================= test session starts ==============================
platform darwin -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-0.13.1
rootdir: /Users/ogrisel/code/debugpytestvscode, configfile: setup.cfg
plugins: anyio-3.3.0, xdist-2.3.0, timeout-1.4.2, forked-1.3.0
collected 1 item

test_debug_vscode.py .                                                   [100%]

- generated xml file: /var/folders/_y/lfnx34p13w3_sr2k12bjb05w0000gn/T/tmp-24919mvRdR0hh0QGh.xml -
============================== 1 passed in 0.01s ===============================

Steps to reproduce

Create a new test Python test project, configure a Python interpreter with pytest installed and add a setup.cfg file containing:

[aliases]
test = pytest

[tool:pytest]
addopts = --color=yes

and a test file with a single test function, for instance a file named test_debug_vscode.py with the following content:

def test_donothing_but_pass():
    assert True

Then:

Shift-Ctrl-P > Python: Configure Tests > pytest > Use existing config file setup.cfg

Then launch the test by clicking on the green > icon in the VS Code UI and open the “Python Test Log” output view.

Logs

The Python log itself has no problem:

> ~/mambaforge/envs/dev/bin/python -m pytest --rootdir ~/code/debugpytestvscode --override-ini junit_family=xunit1 --junit-xml=/var/folders/_y/lfnx34p13w3_sr2k12bjb05w0000gn/T/tmp-24919pLfLJqAh0u2i.xml
cwd: ~/code/debugpytestvscode

Code of Conduct

  • I agree to follow this project’s Code of Conduct

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:9
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
karthiknadigcommented, Dec 6, 2021

@ogrisel I see, thanks for clarifying.

0reactions
ogriselcommented, Dec 6, 2021

Currently we parse the pytest output, that causes this problem.

Parsing is not the problem: the test runner can complete successfully with green markers everywhere. It’s just the display in the “Python Test Log” output view that is unreadable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I force pytest to write color output? - Stack Overflow
As of 2.5.0 py.test has the option --color=yes. As of 2.7.0, it should be also possible to do: export PYTEST_ADDOPTS="--color=yes".
Read more >
Managing pytest's output — pytest documentation
The -v flag controls the verbosity of pytest output in various aspects: test session progress, assertion details when tests fail, fixtures details with ......
Read more >
pytest-tui - PyPI
Test results are categorized in the same way Pytest does it: ... As with the TUIs, the HTML page retains the original pytest...
Read more >
How to Bring Color Back Into tox4 and pytest - Jürgen Gmach
ini , to this line commands = pytest {tty:--color=yes} {posargs} . This forces pytest to produce colorful output, even when invoked without a ......
Read more >
pytest Documentation - Read the Docs
you can run the test module and get the custom output defined in the ... One of the things that makes pytest's fixture...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found