Emit a warning for record_property when used with xunit2 (#5204)

Emit a warning for record_property when used with xunit2
This commit is contained in:
Bruno Oliveira 2019-05-05 21:33:31 -03:00 committed by GitHub
commit 4a2fdce62b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 24 deletions

View File

@ -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>`__.

View File

@ -281,6 +281,21 @@ class _NodeReporter(object):
self.to_xml = lambda: py.xml.raw(data) 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 @pytest.fixture
def record_property(request): def record_property(request):
"""Add an extra properties the calling test. """Add an extra properties the calling test.
@ -294,6 +309,7 @@ def record_property(request):
def test_function(record_property): def test_function(record_property):
record_property("example_key", 1) record_property("example_key", 1)
""" """
_warn_incompatibility_with_xunit2(request, "record_property")
def append_property(name, value): def append_property(name, value):
request.node.user_properties.append((name, value)) request.node.user_properties.append((name, value))
@ -307,27 +323,22 @@ def record_xml_attribute(request):
The fixture is callable with ``(name, value)``, with value being The fixture is callable with ``(name, value)``, with value being
automatically xml-encoded automatically xml-encoded
""" """
from _pytest.warning_types import PytestExperimentalApiWarning, PytestWarning from _pytest.warning_types import PytestExperimentalApiWarning
request.node.warn( request.node.warn(
PytestExperimentalApiWarning("record_xml_attribute is an experimental feature") PytestExperimentalApiWarning("record_xml_attribute is an experimental feature")
) )
_warn_incompatibility_with_xunit2(request, "record_xml_attribute")
# Declare noop # Declare noop
def add_attr_noop(name, value): def add_attr_noop(name, value):
pass pass
attr_func = add_attr_noop attr_func = add_attr_noop
xml = getattr(request.config, "_xml", None)
if xml is not None and xml.family != "xunit1": xml = getattr(request.config, "_xml", None)
request.node.warn( if xml is not None:
PytestWarning(
"record_xml_attribute is incompatible with junit_family: "
"%s (use: legacy|xunit1)" % xml.family
)
)
elif xml is not None:
node_reporter = xml.node_reporter(request.node.nodeid) node_reporter = xml.node_reporter(request.node.nodeid)
attr_func = node_reporter.add_attribute attr_func = node_reporter.add_attribute

View File

@ -993,6 +993,20 @@ def test_record_property_same_name(testdir):
pnodes[1].assert_attr(name="foo", value="baz") pnodes[1].assert_attr(name="foo", value="baz")
@pytest.mark.parametrize("fixture_name", ["record_property", "record_xml_attribute"])
def test_record_fixtures_without_junitxml(testdir, fixture_name):
testdir.makepyfile(
"""
def test_record({fixture_name}):
{fixture_name}("foo", "bar")
""".format(
fixture_name=fixture_name
)
)
result = testdir.runpytest()
assert result.ret == 0
@pytest.mark.filterwarnings("default") @pytest.mark.filterwarnings("default")
def test_record_attribute(testdir): def test_record_attribute(testdir):
testdir.makeini( testdir.makeini(
@ -1023,8 +1037,9 @@ def test_record_attribute(testdir):
@pytest.mark.filterwarnings("default") @pytest.mark.filterwarnings("default")
def test_record_attribute_xunit2(testdir): @pytest.mark.parametrize("fixture_name", ["record_xml_attribute", "record_property"])
"""Ensure record_xml_attribute drops values when outside of legacy family def test_record_fixtures_xunit2(testdir, fixture_name):
"""Ensure record_xml_attribute and record_property drop values when outside of legacy family
""" """
testdir.makeini( testdir.makeini(
""" """
@ -1037,21 +1052,28 @@ def test_record_attribute_xunit2(testdir):
import pytest import pytest
@pytest.fixture @pytest.fixture
def other(record_xml_attribute): def other({fixture_name}):
record_xml_attribute("bar", 1) {fixture_name}("bar", 1)
def test_record(record_xml_attribute, other): def test_record({fixture_name}, other):
record_xml_attribute("foo", "<1"); {fixture_name}("foo", "<1");
""" """.format(
fixture_name=fixture_name
)
) )
result, dom = runandparse(testdir, "-rw") result, dom = runandparse(testdir, "-rw")
result.stdout.fnmatch_lines( expected_lines = []
[ if fixture_name == "record_xml_attribute":
"*test_record_attribute_xunit2.py:6:*record_xml_attribute is an experimental feature", expected_lines.append(
"*test_record_attribute_xunit2.py:6:*record_xml_attribute is incompatible with " "*test_record_fixtures_xunit2.py:6:*record_xml_attribute is an experimental feature"
"junit_family: xunit2 (use: legacy|xunit1)", )
] 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): def test_random_report_log_xdist(testdir, monkeypatch):