pytest-dev / pytest-dev/pytest

PY_COLORS=1 causes invalid escape codes in --junitxml

Open
#2,560 13 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plugin: junitxml type: bug
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Issue: the combination of PY_COLORS=1 and --junitxml causes some weird escaping in the generated xml. Specifically, it seems to partially escape the ANSI escape codes -- converting the ESC into #, but not doing anything else.

Running with just --colors=yes doesn't do this to the generated XML. (This issue really only shows up in my CI environment, where getting things fixed is potentially slow, and could "break" builds for people.) Since it seems like PY_COLORS=1 should mean the same thing as --color=yes I think this is a bug.

This occurs for me with pytest versions 2.8 - 3.1. Pip list for my env with 3.1:

$ pip list
appdirs (1.4.3)
packaging (16.8)
pip (9.0.1)
py (1.4.34)
pyparsing (2.2.0)
pytest (3.1.2)
setuptools (34.3.3)
six (1.10.0)
wheel (0.29.0)

Example:

$ cat test.py
def test_example():
    assert False

############### the bug
$ PY_COLORS=1 pytest --junitxml=junit-env.xml test.py
=============================== test session starts ===============================
platform darwin -- Python 3.5.3, pytest-3.0.6, py-1.4.31, pluggy-0.4.0
rootdir: /Users/bwm/findable/virtualenvs/tmp-203699af9fd9e3ac, inifile:
plugins: cov-2.4.0
collected 1 items

test.py F

 generated xml file: /Users/bwm/findable/virtualenvs/tmp-203699af9fd9e3ac/junit-env.xml
==================================== FAILURES =====================================
__________________________________ test_example ___________________________________

    def test_example():
>       assert False
E       assert False

test.py:2: AssertionError
============================ 1 failed in 0.03 seconds =============================

$ cat junit-env.xml
<?xml version="1.0" encoding="utf-8"?><testsuite errors="0" failures="1" name="pytest" skips="0" tests="1" time="0.028"><testcase classname="test" file="test.py" line="0" name="test_example" time="0.0013718605041503906"><failure message="assert False">#x1B[1m    def test_example():#x1B[0m
#x1B[1m&gt;       assert False#x1B[0m
#x1B[1m#x1B[31mE       assert False#x1B[0m

#x1B[1m#x1B[31mtest.py#x1B[0m:2: AssertionError</failure></testcase></testsuite>%
############## desired behavior
$ pytest --color=yes --junitxml=junit-flag.xml test.py
=============================== test session starts ===============================
platform darwin -- Python 3.5.3, pytest-3.0.6, py-1.4.31, pluggy-0.4.0
rootdir: /Users/bwm/findable/virtualenvs/tmp-203699af9fd9e3ac, inifile:
plugins: cov-2.4.0
collected 1 items

test.py F

 generated xml file: /Users/bwm/findable/virtualenvs/tmp-203699af9fd9e3ac/junit-flag.xml
==================================== FAILURES =====================================
__________________________________ test_example ___________________________________

    def test_example():
>       assert False
E       assert False

test.py:2: AssertionError
============================ 1 failed in 0.03 seconds =============================

$ cat junit-flag.xml
<?xml version="1.0" encoding="utf-8"?><testsuite errors="0" failures="1" name="pytest" skips="0" tests="1" time="0.027"><testcase classname="test" file="test.py" line="0" name="test_example" time="0.0012619495391845703"><failure message="assert False">def test_example():
&gt;       assert False
E       assert False

test.py:2: AssertionError</failure></testcase></testsuite>%

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided test.py using PY_COLORS=1 pytest --junitxml and compare it with --color=yes. Start at pytest's --junitxml and color-handling entry points, then verify that the generated junit-env.xml contains valid XML text without #x1B escape artifacts while preserving the failure output.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.