JUnit XML: Escape error messages in setup/teardown (#10190)

Co-authored-by: Holesch, Simon (BSH) <simon.holesch@bshg.com>
This commit is contained in:
holesch 2022-08-12 11:11:03 +02:00 committed by GitHub
parent 433efaeaa9
commit cc0092b9d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 1 deletions

View File

@ -313,6 +313,7 @@ Seth Junot
Shantanu Jain
Shubham Adep
Simon Gomizelj
Simon Holesch
Simon Kerr
Skylar Downes
Srinivas Reddy Thatiparthy

View File

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

View File

@ -231,7 +231,7 @@ class _NodeReporter:
msg = f'failed on teardown with "{reason}"'
else:
msg = f'failed on setup with "{reason}"'
self._add_simple("error", msg, str(report.longrepr))
self._add_simple("error", bin_xml_escape(msg), str(report.longrepr))
def append_skipped(self, report: TestReport) -> None:
if hasattr(report, "wasxfail"):

View File

@ -1625,6 +1625,28 @@ def test_escaped_skipreason_issue3533(
snode.assert_attr(message="1 <> 2")
def test_escaped_setup_teardown_error(
pytester: Pytester, run_and_parse: RunAndParse
) -> None:
pytester.makepyfile(
"""
import pytest
@pytest.fixture()
def my_setup():
raise Exception("error: \033[31mred\033[m")
def test_esc(my_setup):
pass
"""
)
_, dom = run_and_parse()
node = dom.find_first_by_tag("testcase")
snode = node.find_first_by_tag("error")
assert "#x1B[31mred#x1B[m" in snode["message"]
assert "#x1B[31mred#x1B[m" in snode.text
@parametrize_families
def test_logging_passing_tests_disabled_does_not_log_test_output(
pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str