Skip to content

Commit 6f8c4fa

Browse files
[7.1.x] JUnit XML: Escape error messages in setup/teardown (#10207)
Co-authored-by: holesch <[email protected]>
1 parent 121b692 commit 6f8c4fa

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ Seth Junot
307307
Shantanu Jain
308308
Shubham Adep
309309
Simon Gomizelj
310+
Simon Holesch
310311
Simon Kerr
311312
Skylar Downes
312313
Srinivas Reddy Thatiparthy

changelog/10190.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Invalid XML characters in setup or teardown error messages are now properly escaped for JUnit XML reports.

src/_pytest/junitxml.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def append_error(self, report: TestReport) -> None:
231231
msg = f'failed on teardown with "{reason}"'
232232
else:
233233
msg = f'failed on setup with "{reason}"'
234-
self._add_simple("error", msg, str(report.longrepr))
234+
self._add_simple("error", bin_xml_escape(msg), str(report.longrepr))
235235

236236
def append_skipped(self, report: TestReport) -> None:
237237
if hasattr(report, "wasxfail"):

testing/test_junitxml.py

+22
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,28 @@ def test_skip():
16251625
snode.assert_attr(message="1 <> 2")
16261626

16271627

1628+
def test_escaped_setup_teardown_error(
1629+
pytester: Pytester, run_and_parse: RunAndParse
1630+
) -> None:
1631+
pytester.makepyfile(
1632+
"""
1633+
import pytest
1634+
1635+
@pytest.fixture()
1636+
def my_setup():
1637+
raise Exception("error: \033[31mred\033[m")
1638+
1639+
def test_esc(my_setup):
1640+
pass
1641+
"""
1642+
)
1643+
_, dom = run_and_parse()
1644+
node = dom.find_first_by_tag("testcase")
1645+
snode = node.find_first_by_tag("error")
1646+
assert "#x1B[31mred#x1B[m" in snode["message"]
1647+
assert "#x1B[31mred#x1B[m" in snode.text
1648+
1649+
16281650
@parametrize_families
16291651
def test_logging_passing_tests_disabled_does_not_log_test_output(
16301652
pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str

0 commit comments

Comments
 (0)