Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit df5e1b6

Browse files
committedAug 30, 2023
[junit] move properties handling to finalize()
seem like not all the cases of calling `finalize` were adding the properties into the test case element, which leads to some test cases missing the expected properties. Fixes: pytest-dev#11367
1 parent afb8d66 commit df5e1b6

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed
 

‎AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Ian Lesperance
170170
Ilya Konstantinov
171171
Ionuț Turturică
172172
Isaac Virshup
173+
Israel Fruchter
173174
Itxaso Aizpurua
174175
Iwan Briquemont
175176
Jaap Broekhuizen

‎changelog/11367.bugfix.rst

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[junit] move properties handling to :func:`finalize()`
2+
3+
seem like not all the cases of calling finalize were adding the properties into the test case element,
4+
which leads to some test cases missing the expected properties.
5+

‎src/_pytest/junitxml.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@ def finalize(self, report: TestReport) -> None:
502502
# Local hack to handle xdist report order.
503503
workernode = getattr(report, "node", None)
504504
reporter = self.node_reporters.pop((nodeid, workernode))
505+
506+
for propname, propvalue in report.user_properties:
507+
reporter.add_property(propname, str(propvalue))
508+
505509
if reporter is not None:
506510
reporter.finalize()
507511

@@ -599,9 +603,6 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
599603
reporter = self._opentestcase(report)
600604
reporter.write_captured_output(report)
601605

602-
for propname, propvalue in report.user_properties:
603-
reporter.add_property(propname, str(propvalue))
604-
605606
self.finalize(report)
606607
report_wid = getattr(report, "worker_id", None)
607608
report_ii = getattr(report, "item_index", None)

‎testing/test_junitxml.py

+30
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,36 @@ def test_record(record_property, other):
12281228
result.stdout.fnmatch_lines(["*= 1 passed in *"])
12291229

12301230

1231+
def test_record_property_on_test_and_teardown_failure(
1232+
pytester: Pytester, run_and_parse: RunAndParse
1233+
) -> None:
1234+
pytester.makepyfile(
1235+
"""
1236+
import pytest
1237+
1238+
@pytest.fixture
1239+
def other(record_property):
1240+
record_property("bar", 1)
1241+
yield
1242+
assert 0
1243+
1244+
def test_record(record_property, other):
1245+
record_property("foo", "<1")
1246+
assert 0
1247+
"""
1248+
)
1249+
result, dom = run_and_parse()
1250+
node = dom.find_first_by_tag("testsuite")
1251+
tnodes = node.find_by_tag("testcase")
1252+
for tnode in tnodes:
1253+
psnode = tnode.find_first_by_tag("properties")
1254+
assert psnode, f"testcase didn't had expected properties:\n{tnode}"
1255+
pnodes = psnode.find_by_tag("property")
1256+
pnodes[0].assert_attr(name="bar", value="1")
1257+
pnodes[1].assert_attr(name="foo", value="<1")
1258+
result.stdout.fnmatch_lines(["*= 1 failed, 1 error *"])
1259+
1260+
12311261
def test_record_property_same_name(
12321262
pytester: Pytester, run_and_parse: RunAndParse
12331263
) -> None:

0 commit comments

Comments
 (0)
Please sign in to comment.