Skip to content

Defer assertion-diff highlighting so JUnit XML stays free of ANSI - #14881

Open
arronKler wants to merge 2 commits into
pytest-dev:mainfrom
arronKler:fix/12365-junitxml-ansi
Open

Defer assertion-diff highlighting so JUnit XML stays free of ANSI#14881
arronKler wants to merge 2 commits into
pytest-dev:mainfrom
arronKler:fix/12365-junitxml-ansi

Conversation

@arronKler

Copy link
Copy Markdown
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits.
  • Add text like closes #XYZW to the PR description and/or commits.
  • Create a new changelog file in the changelog directory.
  • Add yourself to AUTHORS in alphabetical order.

Fixes #12365.

Problem

With Pygments installed, verbose assertion diffs (-v/-vv) bake SGR escape sequences into the explanation strings at comparison time. --junitxml then writes those codes through bin_xml_escape(), which turns ESC into #x1B and leaves fragments like #x1B[94m1#x1B[39;49;00m in both the failure message attribute and the failure body.

A previous attempt (#14605) stripped ANSI from TerminalRepr.__str__. That was the wrong layer: it also removed escape sequences that belong to the values under test (for example assert render() == "\x1b[31mred\x1b[0m"), and it never touched reprcrash.message.

Solution

Do not apply Pygments when building the explanation. Wrap highlighted spans in private markers, keep the public AssertionError message plain, and resolve the markers only when writing to a color terminal. JUnit XML and other plain-text consumers see the original source, so pytest-generated colors disappear from the report while user-provided escape sequences remain visible.

Verbose assertion explanations were highlighted at comparison time, so
JUnit XML serialized the raw ANSI codes (as #x1B...) whenever color and
-v/-vv were enabled. Mark highlight spans instead and resolve them only
when writing to a color terminal. The public exception message and XML
report stay plain, and escape sequences that belong to the values under
test are left intact.

Fixes pytest-dev#12365
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ASCII color chars leaking into xml report file when using verbose flag

1 participant