Fix user_properties not saved to XML if fixture errors during teardown · pytest-dev/pytest@917ce9a (original) (raw)

4 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -170,6 +170,7 @@ Ian Lesperance
170 170 Ilya Konstantinov
171 171 Ionuț Turturică
172 172 Isaac Virshup
173 +Israel Fruchter
173 174 Itxaso Aizpurua
174 175 Iwan Briquemont
175 176 Jaap Broekhuizen
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 +Fixed bug where `user_properties` where not being saved in the JUnit XML file if a fixture failed during teardown.
Original file line number Diff line number Diff line change
@@ -502,6 +502,10 @@ def finalize(self, report: TestReport) -> None:
502 502 # Local hack to handle xdist report order.
503 503 workernode = getattr(report, "node", None)
504 504 reporter = self.node_reporters.pop((nodeid, workernode))
505 +
506 +for propname, propvalue in report.user_properties:
507 +reporter.add_property(propname, str(propvalue))
508 +
505 509 if reporter is not None:
506 510 reporter.finalize()
507 511
@@ -599,9 +603,6 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
599 603 reporter = self._opentestcase(report)
600 604 reporter.write_captured_output(report)
601 605
602 -for propname, propvalue in report.user_properties:
603 -reporter.add_property(propname, str(propvalue))
604 -
605 606 self.finalize(report)
606 607 report_wid = getattr(report, "worker_id", None)
607 608 report_ii = getattr(report, "item_index", None)
Original file line number Diff line number Diff line change
@@ -1228,6 +1228,36 @@ def test_record(record_property, other):
1228 1228 result.stdout.fnmatch_lines(["*= 1 passed in *"])
1229 1229
1230 1230
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 +
1231 1261 def test_record_property_same_name(
1232 1262 pytester: Pytester, run_and_parse: RunAndParse
1233 1263 ) -> None: