Skip to content

Commit

Permalink
[8.0.x] Escape skip reason in junitxml (#11845)
Browse files Browse the repository at this point in the history
Co-authored-by: clee2000 <44682903+clee2000@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and clee2000 authored Jan 19, 2024
1 parent 747072a commit 3b41c65
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/11842.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Properly escape the ``reason`` of a :ref:`skip <pytest.mark.skip ref>` mark when writing JUnit XML files.
4 changes: 3 additions & 1 deletion src/_pytest/junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ def append_skipped(self, report: TestReport) -> None:
skipreason = skipreason[9:]
details = f"{filename}:{lineno}: {skipreason}"

skipped = ET.Element("skipped", type="pytest.skip", message=skipreason)
skipped = ET.Element(
"skipped", type="pytest.skip", message=bin_xml_escape(skipreason)
)
skipped.text = bin_xml_escape(details)
self.append(skipped)
self.write_captured_output(report)
Expand Down
17 changes: 17 additions & 0 deletions testing/test_junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,23 @@ def test_skip():
snode.assert_attr(message="1 <> 2")


def test_bin_escaped_skipreason(pytester: Pytester, run_and_parse: RunAndParse) -> None:
"""Escape special characters from mark.skip reason (#11842)."""
pytester.makepyfile(
"""
import pytest
@pytest.mark.skip("\33[31;1mred\33[0m")
def test_skip():
pass
"""
)
_, dom = run_and_parse()
node = dom.find_first_by_tag("testcase")
snode = node.find_first_by_tag("skipped")
assert "#x1B[31;1mred#x1B[0m" in snode.text
snode.assert_attr(message="#x1B[31;1mred#x1B[0m")


def test_escaped_setup_teardown_error(
pytester: Pytester, run_and_parse: RunAndParse
) -> None:
Expand Down

0 comments on commit 3b41c65

Please sign in to comment.