Skip to content

Commit

Permalink
Emit a warning for record_property when used with xunit2
Browse files Browse the repository at this point in the history
"property" elements cannot be children of "testsuite" according to the schema, so it is incompatible with xunit2

Related to #5202
  • Loading branch information
nicoddemus committed May 3, 2019
1 parent 3f5622c commit 4430042
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
4 changes: 4 additions & 0 deletions changelog/5202.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``record_property`` now emits a ``PytestWarning`` when used with ``junit_family=xunit2``: the fixture generates
``property`` tags as children of ``testcase``, which is not permitted according to the most
`recent schema <https://github.com/jenkinsci/xunit-plugin/blob/master/
src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd>`__.
31 changes: 21 additions & 10 deletions src/_pytest/junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,21 @@ def finalize(self):
self.to_xml = lambda: py.xml.raw(data)


def _warn_incompatibility_with_xunit2(request, fixture_name):
"""Emits a PytestWarning about the given fixture being incompatible with newer xunit revisions"""
from _pytest.warning_types import PytestWarning

xml = getattr(request.config, "_xml", None)
if xml is not None and xml.family not in ("xunit1", "legacy"):
request.node.warn(
PytestWarning(
"{fixture_name} is incompatible with junit_family '{family}' (use 'legacy' or 'xunit1')".format(
fixture_name=fixture_name, family=xml.family
)
)
)


@pytest.fixture
def record_property(request):
"""Add an extra properties the calling test.
Expand All @@ -294,6 +309,7 @@ def record_property(request):
def test_function(record_property):
record_property("example_key", 1)
"""
_warn_incompatibility_with_xunit2(request, "record_property")

def append_property(name, value):
request.node.user_properties.append((name, value))
Expand All @@ -307,27 +323,22 @@ def record_xml_attribute(request):
The fixture is callable with ``(name, value)``, with value being
automatically xml-encoded
"""
from _pytest.warning_types import PytestExperimentalApiWarning, PytestWarning
from _pytest.warning_types import PytestExperimentalApiWarning

request.node.warn(
PytestExperimentalApiWarning("record_xml_attribute is an experimental feature")
)

_warn_incompatibility_with_xunit2(request, "record_xml_attribute")

# Declare noop
def add_attr_noop(name, value):
pass

attr_func = add_attr_noop
xml = getattr(request.config, "_xml", None)

if xml is not None and xml.family != "xunit1":
request.node.warn(
PytestWarning(
"record_xml_attribute is incompatible with junit_family: "
"%s (use: legacy|xunit1)" % xml.family
)
)
elif xml is not None:
xml = getattr(request.config, "_xml", None)
if xml is not None:
node_reporter = xml.node_reporter(request.node.nodeid)
attr_func = node_reporter.add_attribute

Expand Down
36 changes: 22 additions & 14 deletions testing/test_junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,8 +1023,9 @@ def test_record(record_xml_attribute, other):


@pytest.mark.filterwarnings("default")
def test_record_attribute_xunit2(testdir):
"""Ensure record_xml_attribute drops values when outside of legacy family
@pytest.mark.parametrize("fixture_name", ["record_xml_attribute", "record_property"])
def test_record_fixtures_xunit2(testdir, fixture_name):
"""Ensure record_xml_attribute and record_property drop values when outside of legacy family
"""
testdir.makeini(
"""
Expand All @@ -1037,21 +1038,28 @@ def test_record_attribute_xunit2(testdir):
import pytest
@pytest.fixture
def other(record_xml_attribute):
record_xml_attribute("bar", 1)
def test_record(record_xml_attribute, other):
record_xml_attribute("foo", "<1");
"""
def other({fixture_name}):
{fixture_name}("bar", 1)
def test_record({fixture_name}, other):
{fixture_name}("foo", "<1");
""".format(
fixture_name=fixture_name
)
)

result, dom = runandparse(testdir, "-rw")
result.stdout.fnmatch_lines(
[
"*test_record_attribute_xunit2.py:6:*record_xml_attribute is an experimental feature",
"*test_record_attribute_xunit2.py:6:*record_xml_attribute is incompatible with "
"junit_family: xunit2 (use: legacy|xunit1)",
]
)
expected_lines = []
if fixture_name == "record_xml_attribute":
expected_lines.append(
"*test_record_fixtures_xunit2.py:6:*record_xml_attribute is an experimental feature"
)
expected_lines = [
"*test_record_fixtures_xunit2.py:6:*{fixture_name} is incompatible "
"with junit_family 'xunit2' (use 'legacy' or 'xunit1')".format(
fixture_name=fixture_name
)
]
result.stdout.fnmatch_lines(expected_lines)


def test_random_report_log_xdist(testdir, monkeypatch):
Expand Down

0 comments on commit 4430042

Please sign in to comment.