pytest icon indicating copy to clipboard operation
pytest copied to clipboard

The colors of the logs are not displayed using --capture=tee-sys

Open nck974 opened this issue 2 years ago • 6 comments

  • [x] a detailed description of the bug or problem you are having:

When I use --capture=tee-sys instead of -s the logs are not colored anymore. Example: image

The source of the issue is that I want to capture to logs to put them in an HTML report, but I also want to see live what is going on in the test as it takes a long time to execute and I need to check the progress. Not having the colors makes it harder to read.

  • [x] output of pip list from the virtual environment you are using
colorama==0.4.6
exceptiongroup==1.1.2
iniconfig==2.0.0     
loguru==0.7.0        
packaging==23.1      
pluggy==1.2.0        
pytest==7.4.0        
tomli==2.0.1
win32-setctime==1.1.0
  • [x] pytest and operating system versions
Windows 10:
Python 3.10.10
  • [x] minimal example if possible:
"""
My test module
"""
from loguru import logger


def test_color():
    """
    My test
    """
    logger.info("My colored log")

nck974 avatar Aug 01 '23 10:08 nck974

Hi there, I have a course assignment where I need to contribute to an open-source project and am just wondering if I can help with this issue! Are there any tips on how I can address this problem?

Thanks!

rowanhnwl avatar Nov 27 '23 19:11 rowanhnwl

Hey @rowanhnwl, sure you can contribute. You can check the doc on contributing. Then I would start by identifying the part of the code where the color originate from (searching for the string "color" in the codebase for example) and once I identified the part where color is handled adding breakpoint and using a debugger to see what happens when --capture=tee-sys is used.

Pierre-Sassoulas avatar Nov 27 '23 19:11 Pierre-Sassoulas

Great, thanks @Pierre-Sassoulas. Might be a stupid question, but I'm just wondering how I can run my fork to actually see any changes that I make - I've been looking through the docs but I can't seem to find anything.

rowanhnwl avatar Nov 27 '23 21:11 rowanhnwl

I'm not sure I understood the question but maybe you want to use git diff origin/main (see the change compared to your main branch), here's some documentation related to creating a pull request on github !https://docs.github.com/en/pull-requests. The git doc from gitlab could also be of use https://docs.gitlab.com/ee/tutorials/make_first_git_commit/

Pierre-Sassoulas avatar Nov 27 '23 21:11 Pierre-Sassoulas

Hi! I have a similar course assignment to rowan, and have been looking into this issue.

It seems like the problem comes from tee-sys piping the command output through pytest, which then displays it to the user. Most command-line programs automatically disable coloured output when being piped, which is what's happening here with loguru. In this particular case the solution would be to configure loguru with colorize=True to force colored output through the pipe.

The most direct solution to adding colored output to tee-sys seems to be having pytest emulate a terminal while handling output, which would cause programs like loguru to produce coloured output by default. However I think this could be more trouble than it would be worth, and would like input on this potential solution.

Edit: I've also come across some potential options for coloured output involving the script utility on linux, which can run a command in a terminal session and capture the output (including colours). However, the behaviour is extremely platform-dependent, as different versions of the script command have different arguments, and relies on an external utility being installed that cannot be directly interacted with from within python. Unfortunately this is also probably not viable

107zxz avatar May 24 '24 03:05 107zxz