Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape skip reason in junitxml #11842

Merged
merged 8 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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