Skip to content

Commit 4a2fdce

Browse files
authored
Emit a warning for record_property when used with xunit2 (#5204)
Emit a warning for record_property when used with xunit2
2 parents f5c1f3d + 8f23e19 commit 4a2fdce

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

changelog/5202.trivial.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``record_property`` now emits a ``PytestWarning`` when used with ``junit_family=xunit2``: the fixture generates
2+
``property`` tags as children of ``testcase``, which is not permitted according to the most
3+
`recent schema <https://github.com/jenkinsci/xunit-plugin/blob/master/
4+
src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd>`__.

src/_pytest/junitxml.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,21 @@ def finalize(self):
281281
self.to_xml = lambda: py.xml.raw(data)
282282

283283

284+
def _warn_incompatibility_with_xunit2(request, fixture_name):
285+
"""Emits a PytestWarning about the given fixture being incompatible with newer xunit revisions"""
286+
from _pytest.warning_types import PytestWarning
287+
288+
xml = getattr(request.config, "_xml", None)
289+
if xml is not None and xml.family not in ("xunit1", "legacy"):
290+
request.node.warn(
291+
PytestWarning(
292+
"{fixture_name} is incompatible with junit_family '{family}' (use 'legacy' or 'xunit1')".format(
293+
fixture_name=fixture_name, family=xml.family
294+
)
295+
)
296+
)
297+
298+
284299
@pytest.fixture
285300
def record_property(request):
286301
"""Add an extra properties the calling test.
@@ -294,6 +309,7 @@ def record_property(request):
294309
def test_function(record_property):
295310
record_property("example_key", 1)
296311
"""
312+
_warn_incompatibility_with_xunit2(request, "record_property")
297313

298314
def append_property(name, value):
299315
request.node.user_properties.append((name, value))
@@ -307,27 +323,22 @@ def record_xml_attribute(request):
307323
The fixture is callable with ``(name, value)``, with value being
308324
automatically xml-encoded
309325
"""
310-
from _pytest.warning_types import PytestExperimentalApiWarning, PytestWarning
326+
from _pytest.warning_types import PytestExperimentalApiWarning
311327

312328
request.node.warn(
313329
PytestExperimentalApiWarning("record_xml_attribute is an experimental feature")
314330
)
315331

332+
_warn_incompatibility_with_xunit2(request, "record_xml_attribute")
333+
316334
# Declare noop
317335
def add_attr_noop(name, value):
318336
pass
319337

320338
attr_func = add_attr_noop
321-
xml = getattr(request.config, "_xml", None)
322339

323-
if xml is not None and xml.family != "xunit1":
324-
request.node.warn(
325-
PytestWarning(
326-
"record_xml_attribute is incompatible with junit_family: "
327-
"%s (use: legacy|xunit1)" % xml.family
328-
)
329-
)
330-
elif xml is not None:
340+
xml = getattr(request.config, "_xml", None)
341+
if xml is not None:
331342
node_reporter = xml.node_reporter(request.node.nodeid)
332343
attr_func = node_reporter.add_attribute
333344

testing/test_junitxml.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,20 @@ def test_record_with_same_name(record_property):
993993
pnodes[1].assert_attr(name="foo", value="baz")
994994

995995

996+
@pytest.mark.parametrize("fixture_name", ["record_property", "record_xml_attribute"])
997+
def test_record_fixtures_without_junitxml(testdir, fixture_name):
998+
testdir.makepyfile(
999+
"""
1000+
def test_record({fixture_name}):
1001+
{fixture_name}("foo", "bar")
1002+
""".format(
1003+
fixture_name=fixture_name
1004+
)
1005+
)
1006+
result = testdir.runpytest()
1007+
assert result.ret == 0
1008+
1009+
9961010
@pytest.mark.filterwarnings("default")
9971011
def test_record_attribute(testdir):
9981012
testdir.makeini(
@@ -1023,8 +1037,9 @@ def test_record(record_xml_attribute, other):
10231037

10241038

10251039
@pytest.mark.filterwarnings("default")
1026-
def test_record_attribute_xunit2(testdir):
1027-
"""Ensure record_xml_attribute drops values when outside of legacy family
1040+
@pytest.mark.parametrize("fixture_name", ["record_xml_attribute", "record_property"])
1041+
def test_record_fixtures_xunit2(testdir, fixture_name):
1042+
"""Ensure record_xml_attribute and record_property drop values when outside of legacy family
10281043
"""
10291044
testdir.makeini(
10301045
"""
@@ -1037,21 +1052,28 @@ def test_record_attribute_xunit2(testdir):
10371052
import pytest
10381053
10391054
@pytest.fixture
1040-
def other(record_xml_attribute):
1041-
record_xml_attribute("bar", 1)
1042-
def test_record(record_xml_attribute, other):
1043-
record_xml_attribute("foo", "<1");
1044-
"""
1055+
def other({fixture_name}):
1056+
{fixture_name}("bar", 1)
1057+
def test_record({fixture_name}, other):
1058+
{fixture_name}("foo", "<1");
1059+
""".format(
1060+
fixture_name=fixture_name
1061+
)
10451062
)
10461063

10471064
result, dom = runandparse(testdir, "-rw")
1048-
result.stdout.fnmatch_lines(
1049-
[
1050-
"*test_record_attribute_xunit2.py:6:*record_xml_attribute is an experimental feature",
1051-
"*test_record_attribute_xunit2.py:6:*record_xml_attribute is incompatible with "
1052-
"junit_family: xunit2 (use: legacy|xunit1)",
1053-
]
1054-
)
1065+
expected_lines = []
1066+
if fixture_name == "record_xml_attribute":
1067+
expected_lines.append(
1068+
"*test_record_fixtures_xunit2.py:6:*record_xml_attribute is an experimental feature"
1069+
)
1070+
expected_lines = [
1071+
"*test_record_fixtures_xunit2.py:6:*{fixture_name} is incompatible "
1072+
"with junit_family 'xunit2' (use 'legacy' or 'xunit1')".format(
1073+
fixture_name=fixture_name
1074+
)
1075+
]
1076+
result.stdout.fnmatch_lines(expected_lines)
10551077

10561078

10571079
def test_random_report_log_xdist(testdir, monkeypatch):

0 commit comments

Comments
 (0)