Change family behavior:

* "legacy" is no longer a copy of "xunit1"
* Attempts to use "legacy" will redirect to "xunit1"
* record_xml_attribute is not compatible outside of legacy family
* Replace call to method/override raw() with to_xml()
This commit is contained in:
Joseph Hunkeler 2019-01-14 13:14:44 -05:00
parent 8937e39afd
commit aaa7d36bc9
No known key found for this signature in database
GPG Key ID: 5C1D2774D4689768
1 changed files with 26 additions and 19 deletions

View File

@ -84,9 +84,6 @@ families["_base_legacy"] = {"testcase": ["file", "line", "url"]}
families["xunit1"] = families["_base"].copy() families["xunit1"] = families["_base"].copy()
merge_family(families["xunit1"], families["_base_legacy"]) merge_family(families["xunit1"], families["_base_legacy"])
# Alias "legacy" to xUnit 1.x
families["legacy"] = families["xunit1"]
# xUnit 2.x uses strict base attributes # xUnit 2.x uses strict base attributes
families["xunit2"] = families["_base"] families["xunit2"] = families["_base"]
@ -145,7 +142,7 @@ class _NodeReporter(object):
self.attrs.update(existing_attrs) # restore any user-defined attributes self.attrs.update(existing_attrs) # restore any user-defined attributes
# Preserve legacy testcase behavior # Preserve legacy testcase behavior
if self.family == "legacy": if self.family == "xunit1":
return return
# Purge attributes not permitted by this test family # Purge attributes not permitted by this test family
@ -275,7 +272,7 @@ class _NodeReporter(object):
def finalize(self): def finalize(self):
data = self.to_xml().unicode(indent=0) data = self.to_xml().unicode(indent=0)
self.__dict__.clear() self.__dict__.clear()
self.raw = lambda: py.xml.raw(data) self.to_xml = lambda: py.xml.raw(data)
@pytest.fixture @pytest.fixture
@ -307,16 +304,26 @@ def record_xml_attribute(request):
from _pytest.warning_types import PytestWarning from _pytest.warning_types import PytestWarning
request.node.warn(PytestWarning("record_xml_attribute is an experimental feature")) request.node.warn(PytestWarning("record_xml_attribute is an experimental feature"))
xml = getattr(request.config, "_xml", None)
if xml is not None:
node_reporter = xml.node_reporter(request.node.nodeid)
return node_reporter.add_attribute
else:
# Declare noop
def add_attr_noop(name, value): def add_attr_noop(name, value):
pass pass
return add_attr_noop attr_func = add_attr_noop
xml = getattr(request.config, "_xml", None)
if 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:
node_reporter = xml.node_reporter(request.node.nodeid)
attr_func = node_reporter.add_attribute
return attr_func
def pytest_addoption(parser): def pytest_addoption(parser):
@ -356,7 +363,7 @@ def pytest_addoption(parser):
parser.addini( parser.addini(
"junit_family", "junit_family",
"Emit XML for schema: one of legacy|xunit1|xunit2", "Emit XML for schema: one of legacy|xunit1|xunit2",
default="legacy", default="xunit1",
) )
@ -405,7 +412,7 @@ class LogXML(object):
suite_name="pytest", suite_name="pytest",
logging="no", logging="no",
report_duration="total", report_duration="total",
family="legacy", family="xunit1",
): ):
logfile = os.path.expanduser(os.path.expandvars(logfile)) logfile = os.path.expanduser(os.path.expandvars(logfile))
self.logfile = os.path.normpath(os.path.abspath(logfile)) self.logfile = os.path.normpath(os.path.abspath(logfile))
@ -422,6 +429,10 @@ class LogXML(object):
self.open_reports = [] self.open_reports = []
self.cnt_double_fail_tests = 0 self.cnt_double_fail_tests = 0
# Replaces convenience family with real family
if self.family == "legacy":
self.family = "xunit1"
def finalize(self, report): def finalize(self, report):
nodeid = getattr(report, "nodeid", report) nodeid = getattr(report, "nodeid", report)
# local hack to handle xdist report order # local hack to handle xdist report order
@ -587,7 +598,7 @@ class LogXML(object):
logfile.write( logfile.write(
Junit.testsuite( Junit.testsuite(
self._get_global_properties_node(), self._get_global_properties_node(),
[x.raw() for x in self.node_reporters_ordered], [x.to_xml() for x in self.node_reporters_ordered],
name=self.suite_name, name=self.suite_name,
errors=self.stats["error"], errors=self.stats["error"],
failures=self.stats["failure"], failures=self.stats["failure"],
@ -598,10 +609,6 @@ class LogXML(object):
) )
logfile.close() logfile.close()
# TODO: GET RID OF
with open(self.logfile) as logfile:
print(logfile.read())
def pytest_terminal_summary(self, terminalreporter): def pytest_terminal_summary(self, terminalreporter):
terminalreporter.write_sep("-", "generated xml file: %s" % (self.logfile)) terminalreporter.write_sep("-", "generated xml file: %s" % (self.logfile))