From 02f5defd891d1a2fb1478a3498feb102270904c6 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 29 Sep 2015 20:46:40 +0200 Subject: [PATCH 01/20] yapf junitxml --- _pytest/junitxml.py | 89 +++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 35 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index 95ed72a6c..59011651c 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -1,9 +1,13 @@ -""" report test results in JUnit-XML format, for use with Hudson and build integration servers. +""" + report test results in JUnit-XML format, + for use with Jenkins and build integration servers. -Output conforms to https://github.com/jenkinsci/xunit-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd Based on initial code from Ross Lawley. """ +# Output conforms to https://github.com/jenkinsci/xunit-plugin/blob/master/ +# src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd + import py import os import re @@ -19,10 +23,10 @@ else: unicode = str long = int + class Junit(py.xml.Namespace): pass - # We need to get the subset of the invalid unicode ranges according to # XML 1.0 which are valid in this python build. Hence we calculate # this dynamically instead of hardcoding it. The spec range of valid @@ -30,21 +34,19 @@ class Junit(py.xml.Namespace): # | [#x10000-#x10FFFF] _legal_chars = (0x09, 0x0A, 0x0d) _legal_ranges = ( - (0x20, 0x7E), - (0x80, 0xD7FF), - (0xE000, 0xFFFD), - (0x10000, 0x10FFFF), + (0x20, 0x7E), (0x80, 0xD7FF), (0xE000, 0xFFFD), (0x10000, 0x10FFFF), ) -_legal_xml_re = [unicode("%s-%s") % (unichr(low), unichr(high)) - for (low, high) in _legal_ranges - if low < sys.maxunicode] +_legal_xml_re = [ + unicode("%s-%s") % (unichr(low), unichr(high)) + for (low, high) in _legal_ranges if low < sys.maxunicode +] _legal_xml_re = [unichr(x) for x in _legal_chars] + _legal_xml_re -illegal_xml_re = re.compile(unicode('[^%s]') % - unicode('').join(_legal_xml_re)) +illegal_xml_re = re.compile(unicode('[^%s]') % unicode('').join(_legal_xml_re)) del _legal_chars del _legal_ranges del _legal_xml_re + def bin_xml_escape(arg): def repl(matchobj): i = ord(matchobj.group()) @@ -52,30 +54,44 @@ def bin_xml_escape(arg): return unicode('#x%02X') % i else: return unicode('#x%04X') % i + return py.xml.raw(illegal_xml_re.sub(repl, py.xml.escape(arg))) + @pytest.fixture def record_xml_property(request): """Fixture that adds extra xml properties to the tag for the calling test. The fixture is callable with (name, value), with value being automatically xml-encoded. """ + def inner(name, value): if hasattr(request.config, "_xml"): request.config._xml.add_custom_property(name, value) msg = 'record_xml_property is an experimental feature' - request.config.warn(code='C3', message=msg, + request.config.warn(code='C3', + message=msg, fslocation=request.node.location[:2]) + return inner + def pytest_addoption(parser): group = parser.getgroup("terminal reporting") - group.addoption('--junitxml', '--junit-xml', action="store", - dest="xmlpath", metavar="path", default=None, - help="create junit-xml style report file at given path.") - group.addoption('--junitprefix', '--junit-prefix', action="store", - metavar="str", default=None, - help="prepend prefix to classnames in junit-xml output") + group.addoption( + '--junitxml', '--junit-xml', + action="store", + dest="xmlpath", + metavar="path", + default=None, + help="create junit-xml style report file at given path.") + group.addoption( + '--junitprefix', '--junit-prefix', + action="store", + metavar="str", + default=None, + help="prepend prefix to classnames in junit-xml output") + def pytest_configure(config): xmlpath = config.option.xmlpath @@ -84,17 +100,20 @@ def pytest_configure(config): config._xml = LogXML(xmlpath, config.option.junitprefix) config.pluginmanager.register(config._xml) + def pytest_unconfigure(config): xml = getattr(config, '_xml', None) if xml: del config._xml config.pluginmanager.unregister(xml) + def mangle_testnames(names): names = [x.replace(".py", "") for x in names if x != '()'] names[0] = names[0].replace("/", '.') return names + class LogXML(object): def __init__(self, logfile, prefix): logfile = os.path.expanduser(os.path.expandvars(logfile)) @@ -134,10 +153,10 @@ class LogXML(object): for capname in ('out', 'err'): allcontent = "" for name, content in report.get_sections("Captured std%s" % - capname): + capname): allcontent += content if allcontent: - tag = getattr(Junit, 'system-'+capname) + tag = getattr(Junit, 'system-' + capname) self.append(tag(bin_xml_escape(allcontent))) def append(self, obj): @@ -150,10 +169,10 @@ class LogXML(object): if self.custom_properties: result = Junit.properties( [ - Junit.property(name=name, value=value) + Junit.property(name=name, + value=value) for name, value in self.custom_properties.items() - ] - ) + ]) self.custom_properties.clear() return result return None @@ -163,7 +182,7 @@ class LogXML(object): self._write_captured_output(report) def append_failure(self, report): - #msg = str(report.longrepr.reprtraceback.extraline) + # msg = str(report.longrepr.reprtraceback.extraline) if hasattr(report, "wasxfail"): self.append( Junit.skipped(message="xfail-marked test passes unexpectedly")) @@ -183,13 +202,13 @@ class LogXML(object): self._write_captured_output(report) def append_collect_error(self, report): - #msg = str(report.longrepr.reprtraceback.extraline) + # msg = str(report.longrepr.reprtraceback.extraline) self.append(Junit.error(bin_xml_escape(report.longrepr), message="collection failure")) self.errors += 1 def append_collect_skipped(self, report): - #msg = str(report.longrepr.reprtraceback.extraline) + # msg = str(report.longrepr.reprtraceback.extraline) self.append(Junit.skipped(bin_xml_escape(report.longrepr), message="collection skipped")) self.skipped += 1 @@ -210,8 +229,7 @@ class LogXML(object): self.append( Junit.skipped("%s:%s: %s" % (filename, lineno, skipreason), type="pytest.skip", - message=skipreason - )) + message=skipreason)) self.skipped += 1 self._write_captured_output(report) @@ -278,9 +296,10 @@ class LogXML(object): data = bin_xml_escape(excrepr) self.tests.append( Junit.testcase( - Junit.error(data, message="internal error"), - classname="pytest", - name="internal")) + Junit.error(data, + message="internal error"), + classname="pytest", + name="internal")) def pytest_sessionstart(self): self.suite_start_time = time.time() @@ -302,9 +321,9 @@ class LogXML(object): failures=self.failed, skips=self.skipped, tests=numtests, - time="%.3f" % suite_time_delta, - ).unicode(indent=0)) + time="%.3f" % suite_time_delta, ).unicode(indent=0)) logfile.close() def pytest_terminal_summary(self, terminalreporter): - terminalreporter.write_sep("-", "generated xml file: %s" % (self.logfile)) + terminalreporter.write_sep("-", + "generated xml file: %s" % (self.logfile)) From 2a31df072b7599446662c3b76fcc864e21ccbd6e Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 29 Sep 2015 22:30:22 +0200 Subject: [PATCH 02/20] junitxml: reverse the if/else logic for failure appending --- _pytest/junitxml.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index 59011651c..ca4f1c71d 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -262,10 +262,10 @@ class LogXML(object): self.append_pass(report) elif report.failed: self._opentestcase(report) - if report.when != "call": - self.append_error(report) - else: + if report.when == "call": self.append_failure(report) + else: + self.append_error(report) elif report.skipped: self._opentestcase(report) self.append_skipped(report) From b8df5446c0ae73c0a422726d5cdfffc15df56309 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 29 Sep 2015 22:39:18 +0200 Subject: [PATCH 03/20] junitxml: yapf clean the tests --- testing/test_junitxml.py | 158 +++++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 71 deletions(-) diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index b25b6c70a..7f91b1054 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -2,7 +2,9 @@ from xml.dom import minidom from _pytest.main import EXIT_NOTESTSCOLLECTED -import py, sys, os +import py +import sys +import os from _pytest.junitxml import LogXML import pytest @@ -13,14 +15,16 @@ def runandparse(testdir, *args): xmldoc = minidom.parse(str(resultpath)) return result, xmldoc + def assert_attr(node, **kwargs): __tracebackhide__ = True for name, expected in kwargs.items(): anode = node.getAttributeNode(name) - assert anode, "node %r has no attribute %r" %(node, name) + assert anode, "node %r has no attribute %r" % (node, name) val = anode.value if val != str(expected): - py.test.fail("%r != %r" %(str(val), str(expected))) + py.test.fail("%r != %r" % (str(val), str(expected))) + class TestPython: def test_summing_simple(self, testdir): @@ -42,7 +46,12 @@ class TestPython: result, dom = runandparse(testdir) assert result.ret node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, name="pytest", errors=0, failures=1, skips=3, tests=2) + assert_attr(node, + name="pytest", + errors=0, + failures=1, + skips=3, + tests=2) def test_timing_function(self, testdir): testdir.makepyfile(""" @@ -73,10 +82,10 @@ class TestPython: assert_attr(node, errors=1, tests=0) tnode = node.getElementsByTagName("testcase")[0] assert_attr(tnode, - file="test_setup_error.py", - line="2", - classname="test_setup_error", - name="test_function") + file="test_setup_error.py", + line="2", + classname="test_setup_error", + name="test_function") fnode = tnode.getElementsByTagName("error")[0] assert_attr(fnode, message="test setup failure") assert "ValueError" in fnode.toxml() @@ -93,15 +102,12 @@ class TestPython: assert_attr(node, skips=1) tnode = node.getElementsByTagName("testcase")[0] assert_attr(tnode, - file="test_skip_contains_name_reason.py", - line="1", - classname="test_skip_contains_name_reason", - name="test_skip") + file="test_skip_contains_name_reason.py", + line="1", + classname="test_skip_contains_name_reason", + name="test_skip") snode = tnode.getElementsByTagName("skipped")[0] - assert_attr(snode, - type="pytest.skip", - message="hello23", - ) + assert_attr(snode, type="pytest.skip", message="hello23", ) def test_classname_instance(self, testdir): testdir.makepyfile(""" @@ -115,10 +121,10 @@ class TestPython: assert_attr(node, failures=1) tnode = node.getElementsByTagName("testcase")[0] assert_attr(tnode, - file="test_classname_instance.py", - line="1", - classname="test_classname_instance.TestClass", - name="test_method") + file="test_classname_instance.py", + line="1", + classname="test_classname_instance.TestClass", + name="test_method") def test_classname_nested_dir(self, testdir): p = testdir.tmpdir.ensure("sub", "test_hello.py") @@ -129,10 +135,10 @@ class TestPython: assert_attr(node, failures=1) tnode = node.getElementsByTagName("testcase")[0] assert_attr(tnode, - file=os.path.join("sub", "test_hello.py"), - line="0", - classname="sub.test_hello", - name="test_func") + file=os.path.join("sub", "test_hello.py"), + line="0", + classname="sub.test_hello", + name="test_func") def test_internal_error(self, testdir): testdir.makeconftest("def pytest_runtest_protocol(): 0 / 0") @@ -162,10 +168,10 @@ class TestPython: assert_attr(node, failures=1, tests=1) tnode = node.getElementsByTagName("testcase")[0] assert_attr(tnode, - file="test_failure_function.py", - line="1", - classname="test_failure_function", - name="test_fail") + file="test_failure_function.py", + line="1", + classname="test_failure_function", + name="test_fail") fnode = tnode.getElementsByTagName("failure")[0] assert_attr(fnode, message="ValueError: 42") assert "ValueError" in fnode.toxml() @@ -206,15 +212,14 @@ class TestPython: tnode = node.getElementsByTagName("testcase")[index] assert_attr(tnode, - file="test_failure_escape.py", - line="1", - classname="test_failure_escape", - name="test_func[%s]" % char) + file="test_failure_escape.py", + line="1", + classname="test_failure_escape", + name="test_func[%s]" % char) sysout = tnode.getElementsByTagName('system-out')[0] text = sysout.childNodes[0].wholeText assert text == '%s\n' % char - def test_junit_prefixing(self, testdir): testdir.makepyfile(""" def test_func(): @@ -229,17 +234,17 @@ class TestPython: assert_attr(node, failures=1, tests=2) tnode = node.getElementsByTagName("testcase")[0] assert_attr(tnode, - file="test_junit_prefixing.py", - line="0", - classname="xyz.test_junit_prefixing", - name="test_func") + file="test_junit_prefixing.py", + line="0", + classname="xyz.test_junit_prefixing", + name="test_func") tnode = node.getElementsByTagName("testcase")[1] assert_attr(tnode, - file="test_junit_prefixing.py", - line="3", - classname="xyz.test_junit_prefixing." - "TestHello", - name="test_hello") + file="test_junit_prefixing.py", + line="3", + classname="xyz.test_junit_prefixing." + "TestHello", + name="test_hello") def test_xfailure_function(self, testdir): testdir.makepyfile(""" @@ -253,13 +258,13 @@ class TestPython: assert_attr(node, skips=1, tests=0) tnode = node.getElementsByTagName("testcase")[0] assert_attr(tnode, - file="test_xfailure_function.py", - line="1", - classname="test_xfailure_function", - name="test_xfail") + file="test_xfailure_function.py", + line="1", + classname="test_xfailure_function", + name="test_xfail") fnode = tnode.getElementsByTagName("skipped")[0] assert_attr(fnode, message="expected test failure") - #assert "ValueError" in fnode.toxml() + # assert "ValueError" in fnode.toxml() def test_xfailure_xpass(self, testdir): testdir.makepyfile(""" @@ -269,18 +274,18 @@ class TestPython: pass """) result, dom = runandparse(testdir) - #assert result.ret + # assert result.ret node = dom.getElementsByTagName("testsuite")[0] assert_attr(node, skips=1, tests=0) tnode = node.getElementsByTagName("testcase")[0] assert_attr(tnode, - file="test_xfailure_xpass.py", - line="1", - classname="test_xfailure_xpass", - name="test_xpass") + file="test_xfailure_xpass.py", + line="1", + classname="test_xfailure_xpass", + name="test_xpass") fnode = tnode.getElementsByTagName("skipped")[0] assert_attr(fnode, message="xfail-marked test passes unexpectedly") - #assert "ValueError" in fnode.toxml() + # assert "ValueError" in fnode.toxml() def test_collect_error(self, testdir): testdir.makepyfile("syntax error") @@ -290,9 +295,8 @@ class TestPython: assert_attr(node, errors=1, tests=0) tnode = node.getElementsByTagName("testcase")[0] assert_attr(tnode, - file="test_collect_error.py", - #classname="test_collect_error", - name="test_collect_error") + file="test_collect_error.py", + name="test_collect_error") assert tnode.getAttributeNode("line") is None fnode = tnode.getElementsByTagName("error")[0] assert_attr(fnode, message="collection failure") @@ -306,10 +310,12 @@ class TestPython: assert_attr(node, skips=1, tests=0) tnode = node.getElementsByTagName("testcase")[0] assert_attr(tnode, - file="test_collect_skipped.py", - #classname="test_collect_error", - name="test_collect_skipped") - assert tnode.getAttributeNode("line") is None # py.test doesn't give us a line here. + file="test_collect_skipped.py", + name="test_collect_skipped") + + # py.test doesn't give us a line here. + assert tnode.getAttributeNode("line") is None + fnode = tnode.getElementsByTagName("skipped")[0] assert_attr(fnode, message="collection skipped") @@ -364,22 +370,27 @@ class TestPython: systemout = pnode.getElementsByTagName("system-err")[0] assert "hello-stderr" in systemout.toxml() + def test_mangle_testnames(): from _pytest.junitxml import mangle_testnames names = ["a/pything.py", "Class", "()", "method"] newnames = mangle_testnames(names) assert newnames == ["a.pything", "Class", "method"] + def test_dont_configure_on_slaves(tmpdir): gotten = [] + class FakeConfig: def __init__(self): self.pluginmanager = self self.option = self + junitprefix = None - #XXX: shouldnt need tmpdir ? + # XXX: shouldnt need tmpdir ? xmlpath = str(tmpdir.join('junix.xml')) register = gotten.append + fake_config = FakeConfig() from _pytest import junitxml junitxml.pytest_configure(fake_config) @@ -411,9 +422,8 @@ class TestNonPython: node = dom.getElementsByTagName("testsuite")[0] assert_attr(node, errors=0, failures=1, skips=0, tests=1) tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, - #classname="test_collect_error", - name="myfile.xyz") + assert_attr(tnode, # classname="test_collect_error", + name="myfile.xyz") fnode = tnode.getElementsByTagName("failure")[0] assert_attr(fnode, message="custom item runtest failed") assert "custom item runtest failed" in fnode.toxml() @@ -449,6 +459,7 @@ def test_nullbyte_replace(testdir): text = xmlf.read() assert '#x0' in text + def test_invalid_xml_escape(): # Test some more invalid xml chars, the full range should be # tested really but let's just thest the edges of the ranges @@ -463,14 +474,13 @@ def test_invalid_xml_escape(): unichr(65) except NameError: unichr = chr - invalid = (0x00, 0x1, 0xB, 0xC, 0xE, 0x19, - 27, # issue #126 - 0xD800, 0xDFFF, 0xFFFE, 0x0FFFF) #, 0x110000) - valid = (0x9, 0xA, 0x20,) # 0xD, 0xD7FF, 0xE000, 0xFFFD, 0x10000, 0x10FFFF) + invalid = (0x00, 0x1, 0xB, 0xC, 0xE, 0x19, 27, # issue #126 + 0xD800, 0xDFFF, 0xFFFE, 0x0FFFF) # , 0x110000) + valid = (0x9, 0xA, 0x20, ) + # 0xD, 0xD7FF, 0xE000, 0xFFFD, 0x10000, 0x10FFFF) from _pytest.junitxml import bin_xml_escape - for i in invalid: got = bin_xml_escape(unichr(i)).uniobj if i <= 0xFF: @@ -481,6 +491,7 @@ def test_invalid_xml_escape(): for i in valid: assert chr(i) == bin_xml_escape(unichr(i)).uniobj + def test_logxml_path_expansion(tmpdir, monkeypatch): home_tilde = py.path.local(os.path.expanduser('~')).join('test.xml') @@ -494,6 +505,7 @@ def test_logxml_path_expansion(tmpdir, monkeypatch): xml_var = LogXML('$HOME%stest.xml' % tmpdir.sep, None) assert xml_var.logfile == home_var + def test_logxml_changingdir(testdir): testdir.makepyfile(""" def test_func(): @@ -505,6 +517,7 @@ def test_logxml_changingdir(testdir): assert result.ret == 0 assert testdir.tmpdir.join("a/x.xml").check() + def test_logxml_makedir(testdir): """--junitxml should automatically create directories for the xml file""" testdir.makepyfile(""" @@ -515,6 +528,7 @@ def test_logxml_makedir(testdir): assert result.ret == 0 assert testdir.tmpdir.join("path/to/results.xml").check() + def test_escaped_parametrized_names_xml(testdir): testdir.makepyfile(""" import pytest @@ -525,19 +539,21 @@ def test_escaped_parametrized_names_xml(testdir): result, dom = runandparse(testdir) assert result.ret == 0 node = dom.getElementsByTagName("testcase")[0] - assert_attr(node, - name="test_func[#x00]") + assert_attr(node, name="test_func[#x00]") + def test_unicode_issue368(testdir): path = testdir.tmpdir.join("test.xml") log = LogXML(str(path), None) ustr = py.builtin._totext("ВНИ!", "utf-8") from _pytest.runner import BaseReport + class Report(BaseReport): longrepr = ustr sections = [] nodeid = "something" location = 'tests/filename.py', 42, 'TestClass.method' + report = Report() # hopefully this is not too brittle ... From 80bcf8d624a3a2968867c257ffb20931220614eb Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 10 Oct 2015 00:40:50 +0200 Subject: [PATCH 04/20] junitxml: simplify tests by creating a api wrapper --- testing/test_junitxml.py | 370 +++++++++++++++++++++------------------ 1 file changed, 204 insertions(+), 166 deletions(-) diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 7f91b1054..dc19c29bd 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -13,17 +13,61 @@ def runandparse(testdir, *args): resultpath = testdir.tmpdir.join("junit.xml") result = testdir.runpytest("--junitxml=%s" % resultpath, *args) xmldoc = minidom.parse(str(resultpath)) - return result, xmldoc + return result, DomNode(xmldoc) def assert_attr(node, **kwargs): __tracebackhide__ = True - for name, expected in kwargs.items(): - anode = node.getAttributeNode(name) - assert anode, "node %r has no attribute %r" % (node, name) - val = anode.value - if val != str(expected): - py.test.fail("%r != %r" % (str(val), str(expected))) + expected = dict((name, str(value)) for name, value in kwargs.items()) + on_node = dict((name, node.getAttributeNode(name).value) for name in expected) + assert on_node == expected + + +class DomNode(object): + def __init__(self, dom): + self.__node = dom + + def find_first_by_tag(self, tag): + return self.find_nth_by_tag(tag, 0) + + def _by_tag(self, tag): + return self.__node.getElementsByTagName(tag) + + def find_nth_by_tag(self, tag, n): + items = self._by_tag(tag) + try: + nth = items[n] + except IndexError: + pass + else: + return type(self)(nth) + + def find_by_tag(self, tag): + t = type(self) + return map(t, self.__node.getElementsByTagName(tag)) + + def __getitem__(self, key): + node = self.__node.getAttributeNode(key) + if node is not None: + return node.value + + def assert_attr(self, **kwargs): + return assert_attr(self.__node, **kwargs) + + def toxml(self): + return self.__node.toxml() + + @property + def text(self): + return self.__node.childNodes[0].wholeText + + @property + def tag(self): + return self.__node.tagName + + @property + def next_siebling(self): + return type(self)(self.__node.nextSibling) class TestPython: @@ -45,13 +89,8 @@ class TestPython: """) result, dom = runandparse(testdir) assert result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, - name="pytest", - errors=0, - failures=1, - skips=3, - tests=2) + node = dom.find_first_by_tag("testsuite") + node.assert_attr(name="pytest", errors=0, failures=1, skips=3, tests=2) def test_timing_function(self, testdir): testdir.makepyfile(""" @@ -64,9 +103,9 @@ class TestPython: time.sleep(0.01) """) result, dom = runandparse(testdir) - node = dom.getElementsByTagName("testsuite")[0] - tnode = node.getElementsByTagName("testcase")[0] - val = tnode.getAttributeNode("time").value + node = dom.find_first_by_tag("testsuite") + tnode = node.find_first_by_tag("testcase") + val = tnode["time"] assert round(float(val), 2) >= 0.03 def test_setup_error(self, testdir): @@ -78,16 +117,16 @@ class TestPython: """) result, dom = runandparse(testdir) assert result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, errors=1, tests=0) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, - file="test_setup_error.py", - line="2", - classname="test_setup_error", - name="test_function") - fnode = tnode.getElementsByTagName("error")[0] - assert_attr(fnode, message="test setup failure") + node = dom.find_first_by_tag("testsuite") + node.assert_attr(errors=1, tests=0) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr( + file="test_setup_error.py", + line="2", + classname="test_setup_error", + name="test_function") + fnode = tnode.find_first_by_tag("error") + fnode.assert_attr(message="test setup failure") assert "ValueError" in fnode.toxml() def test_skip_contains_name_reason(self, testdir): @@ -98,16 +137,16 @@ class TestPython: """) result, dom = runandparse(testdir) assert result.ret == 0 - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, skips=1) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, - file="test_skip_contains_name_reason.py", - line="1", - classname="test_skip_contains_name_reason", - name="test_skip") - snode = tnode.getElementsByTagName("skipped")[0] - assert_attr(snode, type="pytest.skip", message="hello23", ) + node = dom.find_first_by_tag("testsuite") + node.assert_attr(skips=1) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr( + file="test_skip_contains_name_reason.py", + line="1", + classname="test_skip_contains_name_reason", + name="test_skip") + snode = tnode.find_first_by_tag("skipped") + snode.assert_attr(type="pytest.skip", message="hello23", ) def test_classname_instance(self, testdir): testdir.makepyfile(""" @@ -117,40 +156,40 @@ class TestPython: """) result, dom = runandparse(testdir) assert result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, failures=1) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, - file="test_classname_instance.py", - line="1", - classname="test_classname_instance.TestClass", - name="test_method") + node = dom.find_first_by_tag("testsuite") + node.assert_attr(failures=1) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr( + file="test_classname_instance.py", + line="1", + classname="test_classname_instance.TestClass", + name="test_method") def test_classname_nested_dir(self, testdir): p = testdir.tmpdir.ensure("sub", "test_hello.py") p.write("def test_func(): 0/0") result, dom = runandparse(testdir) assert result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, failures=1) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, - file=os.path.join("sub", "test_hello.py"), - line="0", - classname="sub.test_hello", - name="test_func") + node = dom.find_first_by_tag("testsuite") + node.assert_attr(failures=1) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr( + file=os.path.join("sub", "test_hello.py"), + line="0", + classname="sub.test_hello", + name="test_func") def test_internal_error(self, testdir): testdir.makeconftest("def pytest_runtest_protocol(): 0 / 0") testdir.makepyfile("def test_function(): pass") result, dom = runandparse(testdir) assert result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, errors=1, tests=0) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, classname="pytest", name="internal") - fnode = tnode.getElementsByTagName("error")[0] - assert_attr(fnode, message="internal error") + node = dom.find_first_by_tag("testsuite") + node.assert_attr(errors=1, tests=0) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr(classname="pytest", name="internal") + fnode = tnode.find_first_by_tag("error") + fnode.assert_attr(message="internal error") assert "Division" in fnode.toxml() def test_failure_function(self, testdir): @@ -164,22 +203,22 @@ class TestPython: result, dom = runandparse(testdir) assert result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, failures=1, tests=1) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, - file="test_failure_function.py", - line="1", - classname="test_failure_function", - name="test_fail") - fnode = tnode.getElementsByTagName("failure")[0] - assert_attr(fnode, message="ValueError: 42") + node = dom.find_first_by_tag("testsuite") + node.assert_attr(failures=1, tests=1) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr( + file="test_failure_function.py", + line="1", + classname="test_failure_function", + name="test_fail") + fnode = tnode.find_first_by_tag("failure") + fnode.assert_attr(message="ValueError: 42") assert "ValueError" in fnode.toxml() - systemout = fnode.nextSibling - assert systemout.tagName == "system-out" + systemout = fnode.next_siebling + assert systemout.tag == "system-out" assert "hello-stdout" in systemout.toxml() - systemerr = systemout.nextSibling - assert systemerr.tagName == "system-err" + systemerr = systemout.next_siebling + assert systemerr.tag == "system-err" assert "hello-stderr" in systemerr.toxml() def test_failure_verbose_message(self, testdir): @@ -190,10 +229,10 @@ class TestPython: """) result, dom = runandparse(testdir) - node = dom.getElementsByTagName("testsuite")[0] - tnode = node.getElementsByTagName("testcase")[0] - fnode = tnode.getElementsByTagName("failure")[0] - assert_attr(fnode, message="AssertionError: An error assert 0") + node = dom.find_first_by_tag("testsuite") + tnode = node.find_first_by_tag("testcase") + fnode = tnode.find_first_by_tag("failure") + fnode.assert_attr(message="AssertionError: An error assert 0") def test_failure_escape(self, testdir): testdir.makepyfile(""" @@ -205,19 +244,19 @@ class TestPython: """) result, dom = runandparse(testdir) assert result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, failures=3, tests=3) + node = dom.find_first_by_tag("testsuite") + node.assert_attr(failures=3, tests=3) for index, char in enumerate("<&'"): - tnode = node.getElementsByTagName("testcase")[index] - assert_attr(tnode, - file="test_failure_escape.py", - line="1", - classname="test_failure_escape", - name="test_func[%s]" % char) - sysout = tnode.getElementsByTagName('system-out')[0] - text = sysout.childNodes[0].wholeText + tnode = node.find_nth_by_tag("testcase", index) + tnode.assert_attr( + file="test_failure_escape.py", + line="1", + classname="test_failure_escape", + name="test_func[%s]" % char) + sysout = tnode.find_first_by_tag('system-out') + text = sysout.text assert text == '%s\n' % char def test_junit_prefixing(self, testdir): @@ -230,21 +269,21 @@ class TestPython: """) result, dom = runandparse(testdir, "--junitprefix=xyz") assert result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, failures=1, tests=2) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, - file="test_junit_prefixing.py", - line="0", - classname="xyz.test_junit_prefixing", - name="test_func") - tnode = node.getElementsByTagName("testcase")[1] - assert_attr(tnode, - file="test_junit_prefixing.py", - line="3", - classname="xyz.test_junit_prefixing." - "TestHello", - name="test_hello") + node = dom.find_first_by_tag("testsuite") + node.assert_attr(failures=1, tests=2) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr( + file="test_junit_prefixing.py", + line="0", + classname="xyz.test_junit_prefixing", + name="test_func") + tnode = node.find_nth_by_tag("testcase", 1) + tnode.assert_attr( + file="test_junit_prefixing.py", + line="3", + classname="xyz.test_junit_prefixing." + "TestHello", + name="test_hello") def test_xfailure_function(self, testdir): testdir.makepyfile(""" @@ -254,16 +293,16 @@ class TestPython: """) result, dom = runandparse(testdir) assert not result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, skips=1, tests=0) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, - file="test_xfailure_function.py", - line="1", - classname="test_xfailure_function", - name="test_xfail") - fnode = tnode.getElementsByTagName("skipped")[0] - assert_attr(fnode, message="expected test failure") + node = dom.find_first_by_tag("testsuite") + node.assert_attr(skips=1, tests=0) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr( + file="test_xfailure_function.py", + line="1", + classname="test_xfailure_function", + name="test_xfail") + fnode = tnode.find_first_by_tag("skipped") + fnode.assert_attr(message="expected test failure") # assert "ValueError" in fnode.toxml() def test_xfailure_xpass(self, testdir): @@ -275,49 +314,49 @@ class TestPython: """) result, dom = runandparse(testdir) # assert result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, skips=1, tests=0) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, - file="test_xfailure_xpass.py", - line="1", - classname="test_xfailure_xpass", - name="test_xpass") - fnode = tnode.getElementsByTagName("skipped")[0] - assert_attr(fnode, message="xfail-marked test passes unexpectedly") + node = dom.find_first_by_tag("testsuite") + node.assert_attr(skips=1, tests=0) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr( + file="test_xfailure_xpass.py", + line="1", + classname="test_xfailure_xpass", + name="test_xpass") + fnode = tnode.find_first_by_tag("skipped") + fnode.assert_attr(message="xfail-marked test passes unexpectedly") # assert "ValueError" in fnode.toxml() def test_collect_error(self, testdir): testdir.makepyfile("syntax error") result, dom = runandparse(testdir) assert result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, errors=1, tests=0) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, - file="test_collect_error.py", - name="test_collect_error") - assert tnode.getAttributeNode("line") is None - fnode = tnode.getElementsByTagName("error")[0] - assert_attr(fnode, message="collection failure") + node = dom.find_first_by_tag("testsuite") + node.assert_attr(errors=1, tests=0) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr( + file="test_collect_error.py", + name="test_collect_error") + assert tnode["line"] is None + fnode = tnode.find_first_by_tag("error") + fnode.assert_attr(message="collection failure") assert "SyntaxError" in fnode.toxml() def test_collect_skipped(self, testdir): testdir.makepyfile("import pytest; pytest.skip('xyz')") result, dom = runandparse(testdir) assert result.ret == EXIT_NOTESTSCOLLECTED - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, skips=1, tests=0) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, - file="test_collect_skipped.py", - name="test_collect_skipped") + node = dom.find_first_by_tag("testsuite") + node.assert_attr(skips=1, tests=0) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr( + file="test_collect_skipped.py", + name="test_collect_skipped") # py.test doesn't give us a line here. - assert tnode.getAttributeNode("line") is None + assert tnode["line"] is None - fnode = tnode.getElementsByTagName("skipped")[0] - assert_attr(fnode, message="collection skipped") + fnode = tnode.find_first_by_tag("skipped") + fnode.assert_attr(message="collection skipped") def test_unicode(self, testdir): value = 'hx\xc4\x85\xc4\x87\n' @@ -329,8 +368,8 @@ class TestPython: """ % value) result, dom = runandparse(testdir) assert result.ret == 1 - tnode = dom.getElementsByTagName("testcase")[0] - fnode = tnode.getElementsByTagName("failure")[0] + tnode = dom.find_first_by_tag("testcase") + fnode = tnode.find_first_by_tag("failure") if not sys.platform.startswith("java"): assert "hx" in fnode.toxml() @@ -353,9 +392,9 @@ class TestPython: print('hello-stdout') """) result, dom = runandparse(testdir) - node = dom.getElementsByTagName("testsuite")[0] - pnode = node.getElementsByTagName("testcase")[0] - systemout = pnode.getElementsByTagName("system-out")[0] + node = dom.find_first_by_tag("testsuite") + pnode = node.find_first_by_tag("testcase") + systemout = pnode.find_first_by_tag("system-out") assert "hello-stdout" in systemout.toxml() def test_pass_captures_stderr(self, testdir): @@ -365,9 +404,9 @@ class TestPython: sys.stderr.write('hello-stderr') """) result, dom = runandparse(testdir) - node = dom.getElementsByTagName("testsuite")[0] - pnode = node.getElementsByTagName("testcase")[0] - systemout = pnode.getElementsByTagName("system-err")[0] + node = dom.find_first_by_tag("testsuite") + pnode = node.find_first_by_tag("testcase") + systemout = pnode.find_first_by_tag("system-err") assert "hello-stderr" in systemout.toxml() @@ -419,13 +458,12 @@ class TestNonPython: testdir.tmpdir.join("myfile.xyz").write("hello") result, dom = runandparse(testdir) assert result.ret - node = dom.getElementsByTagName("testsuite")[0] - assert_attr(node, errors=0, failures=1, skips=0, tests=1) - tnode = node.getElementsByTagName("testcase")[0] - assert_attr(tnode, # classname="test_collect_error", - name="myfile.xyz") - fnode = tnode.getElementsByTagName("failure")[0] - assert_attr(fnode, message="custom item runtest failed") + node = dom.find_first_by_tag("testsuite") + node.assert_attr(errors=0, failures=1, skips=0, tests=1) + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr(name="myfile.xyz") + fnode = tnode.find_first_by_tag("failure") + fnode.assert_attr(message="custom item runtest failed") assert "custom item runtest failed" in fnode.toxml() @@ -538,8 +576,8 @@ def test_escaped_parametrized_names_xml(testdir): """) result, dom = runandparse(testdir) assert result.ret == 0 - node = dom.getElementsByTagName("testcase")[0] - assert_attr(node, name="test_func[#x00]") + node = dom.find_first_by_tag("testcase") + node.assert_attr(name="test_func[#x00]") def test_unicode_issue368(testdir): @@ -578,11 +616,11 @@ def test_record_property(testdir): record_xml_property("foo", "<1"); """) result, dom = runandparse(testdir, '-rw') - node = dom.getElementsByTagName("testsuite")[0] - tnode = node.getElementsByTagName("testcase")[0] - psnode = tnode.getElementsByTagName('properties')[0] - pnode = psnode.getElementsByTagName('property')[0] - assert_attr(pnode, name="foo", value="<1") + node = dom.find_first_by_tag("testsuite") + tnode = node.find_first_by_tag("testcase") + psnode = tnode.find_first_by_tag('properties') + pnode = psnode.find_first_by_tag('property') + pnode.assert_attr(name="foo", value="<1") result.stdout.fnmatch_lines('*C3*test_record_property.py*experimental*') @@ -599,10 +637,10 @@ def test_random_report_log_xdist(testdir): assert i != 22 """) _, dom = runandparse(testdir, '-n2') - suite_node = dom.getElementsByTagName("testsuite")[0] + suite_node = dom.find_first_by_tag("testsuite") failed = [] - for case_node in suite_node.getElementsByTagName("testcase"): - if case_node.getElementsByTagName('failure'): - failed.append(case_node.getAttributeNode('name').value) + for case_node in suite_node.find_by_tag("testcase"): + if case_node.find_first_by_tag('failure'): + failed.append(case_node['name']) assert failed == ['test_x[22]'] From 9128fec4c49b9cf53528fac3a1e2eabe2b011227 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 10 Oct 2015 08:19:41 +0200 Subject: [PATCH 05/20] junitxml: simplify the api used for testing junitml --- testing/test_junitxml.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index dc19c29bd..55668f5ca 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -612,7 +612,12 @@ def test_unicode_issue368(testdir): def test_record_property(testdir): testdir.makepyfile(""" - def test_record(record_xml_property): + import pytest + + @pytest.fixture + def other(record_xml_property): + record_xml_property("bar", 1) + def test_record(record_xml_property, other): record_xml_property("foo", "<1"); """) result, dom = runandparse(testdir, '-rw') From d0107c898ecd9a49246f22c3ab2b6ea0a54bd290 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 10 Oct 2015 09:31:52 +0200 Subject: [PATCH 06/20] junitxml restrucutre stat generation - use node tags --- _pytest/junitxml.py | 74 +++++++++++++++++++++++----------------- testing/test_junitxml.py | 7 +++- 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index ca4f1c71d..915f045f0 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -120,15 +120,33 @@ class LogXML(object): self.logfile = os.path.normpath(os.path.abspath(logfile)) self.prefix = prefix self.tests = [] + self.stats = dict.fromkeys([ + 'error', + 'passed', + 'failure', + 'skipped', + ], 0) self.tests_by_nodeid = {} # nodeid -> Junit.testcase self.durations = {} # nodeid -> total duration (setup+call+teardown) - self.passed = self.skipped = 0 - self.failed = self.errors = 0 self.custom_properties = {} def add_custom_property(self, name, value): self.custom_properties[str(name)] = bin_xml_escape(str(value)) + def _addtestcase(self, attrs=None, **kw): + testcase = Junit.testcase(**(attrs or kw)) + self.tests.append(testcase) + return testcase + + def _add_stats(self, key): + if key in self.stats: + self.stats[key] += 1 + + def _add_simple(self, kind, message, data=None): + data = bin_xml_escape(data) + node = kind(data, message=message) + self.append(node) + def _opentestcase(self, report): names = mangle_testnames(report.nodeid.split("::")) classnames = names[:-1] @@ -142,11 +160,10 @@ class LogXML(object): } if report.location[1] is not None: attrs["line"] = report.location[1] - testcase = Junit.testcase(**attrs) + testcase = self._addtestcase(attrs) custom_properties = self.pop_custom_properties() if custom_properties: testcase.append(custom_properties) - self.tests.append(testcase) self.tests_by_nodeid[report.nodeid] = testcase def _write_captured_output(self, report): @@ -160,6 +177,7 @@ class LogXML(object): self.append(tag(bin_xml_escape(allcontent))) def append(self, obj): + self._add_stats(type(obj).__name__) self.tests[-1].append(obj) def pop_custom_properties(self): @@ -178,15 +196,15 @@ class LogXML(object): return None def append_pass(self, report): - self.passed += 1 + self._add_stats('passed') self._write_captured_output(report) def append_failure(self, report): # msg = str(report.longrepr.reprtraceback.extraline) if hasattr(report, "wasxfail"): - self.append( - Junit.skipped(message="xfail-marked test passes unexpectedly")) - self.skipped += 1 + self._add_simple( + Junit.skipped, + "xfail-marked test passes unexpectedly") else: if hasattr(report.longrepr, "reprcrash"): message = report.longrepr.reprcrash.message @@ -198,30 +216,26 @@ class LogXML(object): fail = Junit.failure(message=message) fail.append(bin_xml_escape(report.longrepr)) self.append(fail) - self.failed += 1 self._write_captured_output(report) def append_collect_error(self, report): # msg = str(report.longrepr.reprtraceback.extraline) self.append(Junit.error(bin_xml_escape(report.longrepr), message="collection failure")) - self.errors += 1 def append_collect_skipped(self, report): - # msg = str(report.longrepr.reprtraceback.extraline) - self.append(Junit.skipped(bin_xml_escape(report.longrepr), - message="collection skipped")) - self.skipped += 1 + self._add_simple( + Junit.skipped, "collection skipped", report.longrepr) def append_error(self, report): - self.append(Junit.error(bin_xml_escape(report.longrepr), - message="test setup failure")) - self.errors += 1 + self._add_simple( + Junit.error, "test setup failure", report.longrepr) def append_skipped(self, report): if hasattr(report, "wasxfail"): - self.append(Junit.skipped(bin_xml_escape(report.wasxfail), - message="expected test failure")) + self._add_simple( + Junit.skipped, "expected test failure", report.wasxfail + ) else: filename, lineno, skipreason = report.longrepr if skipreason.startswith("Skipped: "): @@ -230,7 +244,6 @@ class LogXML(object): Junit.skipped("%s:%s: %s" % (filename, lineno, skipreason), type="pytest.skip", message=skipreason)) - self.skipped += 1 self._write_captured_output(report) def pytest_runtest_logreport(self, report): @@ -292,14 +305,9 @@ class LogXML(object): self.append_collect_skipped(report) def pytest_internalerror(self, excrepr): - self.errors += 1 - data = bin_xml_escape(excrepr) - self.tests.append( - Junit.testcase( - Junit.error(data, - message="internal error"), - classname="pytest", - name="internal")) + + self._addtestcase(classname="pytest", name='internal') + self._add_simple(Junit.error, 'internal error', excrepr) def pytest_sessionstart(self): self.suite_start_time = time.time() @@ -311,15 +319,16 @@ class LogXML(object): logfile = open(self.logfile, 'w', encoding='utf-8') suite_stop_time = time.time() suite_time_delta = suite_stop_time - self.suite_start_time - numtests = self.passed + self.failed + + numtests = self.stats['passed'] + self.stats['failure'] logfile.write('') logfile.write(Junit.testsuite( self.tests, name="pytest", - errors=self.errors, - failures=self.failed, - skips=self.skipped, + errors=self.stats['error'], + failures=self.stats['failure'], + skips=self.stats['skipped'], tests=numtests, time="%.3f" % suite_time_delta, ).unicode(indent=0)) logfile.close() @@ -327,3 +336,4 @@ class LogXML(object): def pytest_terminal_summary(self, terminalreporter): terminalreporter.write_sep("-", "generated xml file: %s" % (self.logfile)) + terminalreporter.write(str(self.stats)) diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 55668f5ca..e6df2150c 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -18,8 +18,13 @@ def runandparse(testdir, *args): def assert_attr(node, **kwargs): __tracebackhide__ = True + def nodeval(node, name): + anode = node.getAttributeNode(name) + if anode is not None: + return anode.value + expected = dict((name, str(value)) for name, value in kwargs.items()) - on_node = dict((name, node.getAttributeNode(name).value) for name in expected) + on_node = dict((name, nodeval(node, name)) for name in expected) assert on_node == expected From 0664ae137c7f525c153316fff6d44f93d4f56856 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 10 Oct 2015 10:49:05 +0200 Subject: [PATCH 07/20] junitxml: remove debug print --- _pytest/junitxml.py | 1 - 1 file changed, 1 deletion(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index 915f045f0..b88050712 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -336,4 +336,3 @@ class LogXML(object): def pytest_terminal_summary(self, terminalreporter): terminalreporter.write_sep("-", "generated xml file: %s" % (self.logfile)) - terminalreporter.write(str(self.stats)) From 1f609f96e6320106bdec163b0edb63b89311a276 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 10 Oct 2015 11:45:56 +0200 Subject: [PATCH 08/20] junitxml: introduce nodereporter and track durations/properties there --- _pytest/junitxml.py | 86 +++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 34 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index b88050712..de7c368ee 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -58,22 +58,48 @@ def bin_xml_escape(arg): return py.xml.raw(illegal_xml_re.sub(repl, py.xml.escape(arg))) +class _NodeReporter(object): + def __init__(self, nodeid, xml): + + self.id = nodeid + self.duration = 0 + self.properties = {} + self.testcase = None + + def add_property(self, name, value): + self.properties[str(name)] = bin_xml_escape(value) + + + def make_properties_node(self): + """Return a Junit node containing custom properties set for + the current test, if any, and reset the current custom properties. + """ + if self.properties: + return Junit.properties([ + Junit.property(name=name, value=value) + for name, value in self.properties.items() + ]) + + @pytest.fixture def record_xml_property(request): """Fixture that adds extra xml properties to the tag for the calling test. The fixture is callable with (name, value), with value being automatically xml-encoded. """ + request.config.warn( + code='C3', + message='record_xml_property is an experimental feature', + fslocation=request.node.location[:2]) + xml = getattr(request.config, "_xml", None) + if xml is not None: + nodereporter = xml.nodereporter(request.node.nodeid) + return nodereporter.add_property + else: + def add_property_noop(name, value): + pass - def inner(name, value): - if hasattr(request.config, "_xml"): - request.config._xml.add_custom_property(name, value) - msg = 'record_xml_property is an experimental feature' - request.config.warn(code='C3', - message=msg, - fslocation=request.node.location[:2]) - - return inner + return add_property_noop def pytest_addoption(parser): @@ -126,12 +152,16 @@ class LogXML(object): 'failure', 'skipped', ], 0) - self.tests_by_nodeid = {} # nodeid -> Junit.testcase - self.durations = {} # nodeid -> total duration (setup+call+teardown) - self.custom_properties = {} + self.nodereporters = {} # nodeid -> Junit.testcase + self.tests_by_nodeid = {} + + def nodereporter(self, nodeid): + if nodeid in self.nodereporters: + return self.nodereporters[nodeid] + reporter = _NodeReporter(nodeid, self) + self.nodereporters[nodeid] = reporter + return reporter - def add_custom_property(self, name, value): - self.custom_properties[str(name)] = bin_xml_escape(str(value)) def _addtestcase(self, attrs=None, **kw): testcase = Junit.testcase(**(attrs or kw)) @@ -148,6 +178,7 @@ class LogXML(object): self.append(node) def _opentestcase(self, report): + reporter = self.nodereporter(report.nodeid) names = mangle_testnames(report.nodeid.split("::")) classnames = names[:-1] if self.prefix: @@ -156,12 +187,13 @@ class LogXML(object): "classname": ".".join(classnames), "name": bin_xml_escape(names[-1]), "file": report.location[0], - "time": self.durations.get(report.nodeid, 0), + "time": reporter.duration, } if report.location[1] is not None: attrs["line"] = report.location[1] testcase = self._addtestcase(attrs) - custom_properties = self.pop_custom_properties() + reporter = self.nodereporter(report.nodeid) + custom_properties = reporter.make_properties_node() if custom_properties: testcase.append(custom_properties) self.tests_by_nodeid[report.nodeid] = testcase @@ -180,20 +212,7 @@ class LogXML(object): self._add_stats(type(obj).__name__) self.tests[-1].append(obj) - def pop_custom_properties(self): - """Return a Junit node containing custom properties set for - the current test, if any, and reset the current custom properties. - """ - if self.custom_properties: - result = Junit.properties( - [ - Junit.property(name=name, - value=value) - for name, value in self.custom_properties.items() - ]) - self.custom_properties.clear() - return result - return None + def append_pass(self, report): self._add_stats('passed') @@ -288,13 +307,12 @@ class LogXML(object): """accumulates total duration for nodeid from given report and updates the Junit.testcase with the new total if already created. """ - total = self.durations.get(report.nodeid, 0.0) - total += getattr(report, 'duration', 0.0) - self.durations[report.nodeid] = total + reporter = self.nodereporter(report.nodeid) + reporter.duration += getattr(report, 'duration', 0.0) testcase = self.tests_by_nodeid.get(report.nodeid) if testcase is not None: - testcase.attr.time = total + testcase.attr.time = reporter.duration def pytest_collectreport(self, report): if not report.passed: From faed54d6c7ed1c87015bbdf80fa30e941f1eda20 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 10 Oct 2015 14:20:51 +0200 Subject: [PATCH 09/20] junitxml: use node.warn to ensure fslocations --- _pytest/junitxml.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index de7c368ee..d7aeec96d 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -87,10 +87,10 @@ def record_xml_property(request): The fixture is callable with (name, value), with value being automatically xml-encoded. """ - request.config.warn( + request.node.warn( code='C3', message='record_xml_property is an experimental feature', - fslocation=request.node.location[:2]) + ) xml = getattr(request.config, "_xml", None) if xml is not None: nodereporter = xml.nodereporter(request.node.nodeid) From 9b0495830319f3028291e19b5317f64ec105dca0 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 10 Oct 2015 14:39:25 +0200 Subject: [PATCH 10/20] junitxml: keep track of custom property insert order + review: should we allow the same key multile times --- _pytest/junitxml.py | 10 +++++++--- testing/test_junitxml.py | 8 ++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index d7aeec96d..3e0ca147a 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -64,10 +64,14 @@ class _NodeReporter(object): self.id = nodeid self.duration = 0 self.properties = {} + self.property_insert_order = [] self.testcase = None def add_property(self, name, value): - self.properties[str(name)] = bin_xml_escape(value) + name = str(name) + if name not in self.property_insert_order: + self.property_insert_order.append(name) + self.properties[name] = bin_xml_escape(value) def make_properties_node(self): @@ -76,8 +80,8 @@ class _NodeReporter(object): """ if self.properties: return Junit.properties([ - Junit.property(name=name, value=value) - for name, value in self.properties.items() + Junit.property(name=name, value=self.properties[name]) + for name in self.property_insert_order ]) diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index e6df2150c..75819694f 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -32,6 +32,9 @@ class DomNode(object): def __init__(self, dom): self.__node = dom + def __repr__(self): + return self.__node.toxml() + def find_first_by_tag(self, tag): return self.find_nth_by_tag(tag, 0) @@ -629,8 +632,9 @@ def test_record_property(testdir): node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") psnode = tnode.find_first_by_tag('properties') - pnode = psnode.find_first_by_tag('property') - pnode.assert_attr(name="foo", value="<1") + pnodes = psnode.find_by_tag('property') + pnodes[0].assert_attr(name="bar", value="1") + pnodes[1].assert_attr(name="foo", value="<1") result.stdout.fnmatch_lines('*C3*test_record_property.py*experimental*') From 0528e5b45f4da751056af98412b60f16a8e52a74 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 10 Oct 2015 19:24:21 +0200 Subject: [PATCH 11/20] junitxml: intermediate, move testcase generation to NodeReporter --- _pytest/junitxml.py | 238 ++++++++++++++++++++------------------- testing/test_junitxml.py | 25 ++-- 2 files changed, 133 insertions(+), 130 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index 3e0ca147a..ec6c25eef 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -62,10 +62,19 @@ class _NodeReporter(object): def __init__(self, nodeid, xml): self.id = nodeid + self.xml = xml + self.add_stats = self.xml.add_stats self.duration = 0 self.properties = {} self.property_insert_order = [] + self.nodes = [] self.testcase = None + self.attrs = {} + + + def append(self, node): + self.xml.add_stats(type(node).__name__) + self.nodes.append(node) def add_property(self, name, value): name = str(name) @@ -83,6 +92,98 @@ class _NodeReporter(object): Junit.property(name=name, value=self.properties[name]) for name in self.property_insert_order ]) + return '' + + + def record_testreport(self, testreport): + assert not self.testcase + names = mangle_testnames(testreport.nodeid.split("::")) + classnames = names[:-1] + if self.xml.prefix: + classnames.insert(0, self.xml.prefix) + attrs = { + "classname": ".".join(classnames), + "name": bin_xml_escape(names[-1]), + "file": testreport.location[0], + } + if testreport.location[1] is not None: + attrs["line"] = testreport.location[1] + self.attrs = attrs + + def to_xml(self): + testcase = Junit.testcase(time=self.duration, **self.attrs) + testcase.append(self.make_properties_node()) + for node in self.nodes: + testcase.append(node) + return testcase + + def _add_simple(self, kind, message, data=None): + data = bin_xml_escape(data) + node = kind(data, message=message) + self.append(node) + + def _write_captured_output(self, report): + for capname in ('out', 'err'): + allcontent = "" + for name, content in report.get_sections("Captured std%s" % + capname): + allcontent += content + if allcontent: + tag = getattr(Junit, 'system-' + capname) + self.append(tag(bin_xml_escape(allcontent))) + + + + def append_pass(self, report): + self.add_stats('passed') + self._write_captured_output(report) + + def append_failure(self, report): + # msg = str(report.longrepr.reprtraceback.extraline) + if hasattr(report, "wasxfail"): + self._add_simple( + Junit.skipped, + "xfail-marked test passes unexpectedly") + else: + if hasattr(report.longrepr, "reprcrash"): + message = report.longrepr.reprcrash.message + elif isinstance(report.longrepr, (unicode, str)): + message = report.longrepr + else: + message = str(report.longrepr) + message = bin_xml_escape(message) + fail = Junit.failure(message=message) + fail.append(bin_xml_escape(report.longrepr)) + self.append(fail) + self._write_captured_output(report) + + def append_collect_error(self, report): + # msg = str(report.longrepr.reprtraceback.extraline) + self.append(Junit.error(bin_xml_escape(report.longrepr), + message="collection failure")) + + def append_collect_skipped(self, report): + self._add_simple( + Junit.skipped, "collection skipped", report.longrepr) + + def append_error(self, report): + self._add_simple( + Junit.error, "test setup failure", report.longrepr) + + def append_skipped(self, report): + if hasattr(report, "wasxfail"): + self._add_simple( + Junit.skipped, "expected test failure", report.wasxfail + ) + else: + filename, lineno, skipreason = report.longrepr + if skipreason.startswith("Skipped: "): + skipreason = bin_xml_escape(skipreason[9:]) + self.append( + Junit.skipped("%s:%s: %s" % (filename, lineno, skipreason), + type="pytest.skip", + message=skipreason)) + self._write_captured_output(report) @pytest.fixture @@ -149,125 +250,30 @@ class LogXML(object): logfile = os.path.expanduser(os.path.expandvars(logfile)) self.logfile = os.path.normpath(os.path.abspath(logfile)) self.prefix = prefix - self.tests = [] self.stats = dict.fromkeys([ 'error', 'passed', 'failure', 'skipped', ], 0) - self.nodereporters = {} # nodeid -> Junit.testcase - self.tests_by_nodeid = {} + self.nodereporters = {} # nodeid -> _NodeReporter def nodereporter(self, nodeid): if nodeid in self.nodereporters: + #TODO: breasks for --dist=each return self.nodereporters[nodeid] reporter = _NodeReporter(nodeid, self) self.nodereporters[nodeid] = reporter return reporter - - def _addtestcase(self, attrs=None, **kw): - testcase = Junit.testcase(**(attrs or kw)) - self.tests.append(testcase) - return testcase - - def _add_stats(self, key): + def add_stats(self, key): if key in self.stats: self.stats[key] += 1 - def _add_simple(self, kind, message, data=None): - data = bin_xml_escape(data) - node = kind(data, message=message) - self.append(node) - def _opentestcase(self, report): reporter = self.nodereporter(report.nodeid) - names = mangle_testnames(report.nodeid.split("::")) - classnames = names[:-1] - if self.prefix: - classnames.insert(0, self.prefix) - attrs = { - "classname": ".".join(classnames), - "name": bin_xml_escape(names[-1]), - "file": report.location[0], - "time": reporter.duration, - } - if report.location[1] is not None: - attrs["line"] = report.location[1] - testcase = self._addtestcase(attrs) - reporter = self.nodereporter(report.nodeid) - custom_properties = reporter.make_properties_node() - if custom_properties: - testcase.append(custom_properties) - self.tests_by_nodeid[report.nodeid] = testcase - - def _write_captured_output(self, report): - for capname in ('out', 'err'): - allcontent = "" - for name, content in report.get_sections("Captured std%s" % - capname): - allcontent += content - if allcontent: - tag = getattr(Junit, 'system-' + capname) - self.append(tag(bin_xml_escape(allcontent))) - - def append(self, obj): - self._add_stats(type(obj).__name__) - self.tests[-1].append(obj) - - - - def append_pass(self, report): - self._add_stats('passed') - self._write_captured_output(report) - - def append_failure(self, report): - # msg = str(report.longrepr.reprtraceback.extraline) - if hasattr(report, "wasxfail"): - self._add_simple( - Junit.skipped, - "xfail-marked test passes unexpectedly") - else: - if hasattr(report.longrepr, "reprcrash"): - message = report.longrepr.reprcrash.message - elif isinstance(report.longrepr, (unicode, str)): - message = report.longrepr - else: - message = str(report.longrepr) - message = bin_xml_escape(message) - fail = Junit.failure(message=message) - fail.append(bin_xml_escape(report.longrepr)) - self.append(fail) - self._write_captured_output(report) - - def append_collect_error(self, report): - # msg = str(report.longrepr.reprtraceback.extraline) - self.append(Junit.error(bin_xml_escape(report.longrepr), - message="collection failure")) - - def append_collect_skipped(self, report): - self._add_simple( - Junit.skipped, "collection skipped", report.longrepr) - - def append_error(self, report): - self._add_simple( - Junit.error, "test setup failure", report.longrepr) - - def append_skipped(self, report): - if hasattr(report, "wasxfail"): - self._add_simple( - Junit.skipped, "expected test failure", report.wasxfail - ) - else: - filename, lineno, skipreason = report.longrepr - if skipreason.startswith("Skipped: "): - skipreason = bin_xml_escape(skipreason[9:]) - self.append( - Junit.skipped("%s:%s: %s" % (filename, lineno, skipreason), - type="pytest.skip", - message=skipreason)) - self._write_captured_output(report) + reporter.record_testreport(report) + return reporter def pytest_runtest_logreport(self, report): """handle a setup/call/teardown report, generating the appropriate @@ -294,17 +300,17 @@ class LogXML(object): """ if report.passed: if report.when == "call": # ignore setup/teardown - self._opentestcase(report) - self.append_pass(report) + reporter = self._opentestcase(report) + reporter.append_pass(report) elif report.failed: - self._opentestcase(report) + reporter = self._opentestcase(report) if report.when == "call": - self.append_failure(report) + reporter.append_failure(report) else: - self.append_error(report) + reporter.append_error(report) elif report.skipped: - self._opentestcase(report) - self.append_skipped(report) + reporter = self._opentestcase(report) + reporter.append_skipped(report) self.update_testcase_duration(report) def update_testcase_duration(self, report): @@ -314,22 +320,18 @@ class LogXML(object): reporter = self.nodereporter(report.nodeid) reporter.duration += getattr(report, 'duration', 0.0) - testcase = self.tests_by_nodeid.get(report.nodeid) - if testcase is not None: - testcase.attr.time = reporter.duration - def pytest_collectreport(self, report): if not report.passed: - self._opentestcase(report) + reporter = self._opentestcase(report) if report.failed: - self.append_collect_error(report) + reporter.append_collect_error(report) else: - self.append_collect_skipped(report) + reporter.append_collect_skipped(report) def pytest_internalerror(self, excrepr): - - self._addtestcase(classname="pytest", name='internal') - self._add_simple(Junit.error, 'internal error', excrepr) + reporter = self.nodereporter('internal') + reporter.attrs.update(classname="pytest", name='internal') + reporter._add_simple(Junit.error, 'internal error', excrepr) def pytest_sessionstart(self): self.suite_start_time = time.time() @@ -346,7 +348,7 @@ class LogXML(object): logfile.write('') logfile.write(Junit.testsuite( - self.tests, + [x.to_xml() for x in self.nodereporters.values()], name="pytest", errors=self.stats['error'], failures=self.stats['failure'], diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 75819694f..6ef0e4aa9 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -12,6 +12,7 @@ import pytest def runandparse(testdir, *args): resultpath = testdir.tmpdir.join("junit.xml") result = testdir.runpytest("--junitxml=%s" % resultpath, *args) + print(resultpath.read()) xmldoc = minidom.parse(str(resultpath)) return result, DomNode(xmldoc) @@ -600,21 +601,21 @@ def test_unicode_issue368(testdir): nodeid = "something" location = 'tests/filename.py', 42, 'TestClass.method' - report = Report() + test_report = Report() # hopefully this is not too brittle ... log.pytest_sessionstart() - log._opentestcase(report) - log.append_failure(report) - log.append_collect_error(report) - log.append_collect_skipped(report) - log.append_error(report) - report.longrepr = "filename", 1, ustr - log.append_skipped(report) - report.longrepr = "filename", 1, "Skipped: 卡嘣嘣" - log.append_skipped(report) - report.wasxfail = ustr - log.append_skipped(report) + node_reporter = log._opentestcase(test_report) + node_reporter.append_failure(test_report) + node_reporter.append_collect_error(test_report) + node_reporter.append_collect_skipped(test_report) + node_reporter.append_error(test_report) + test_report.longrepr = "filename", 1, ustr + node_reporter.append_skipped(test_report) + test_report.longrepr = "filename", 1, "Skipped: 卡嘣嘣" + node_reporter.append_skipped(test_report) + test_report.wasxfail = ustr + node_reporter.append_skipped(test_report) log.pytest_sessionfinish() From 5f5a7995b9080488b42299ac5bb545e78615e95f Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 10 Oct 2015 19:35:58 +0200 Subject: [PATCH 12/20] reintroduce junitxml report order and debug cleanups --- _pytest/junitxml.py | 6 +++--- testing/test_junitxml.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index ec6c25eef..e6364f7c7 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -132,8 +132,6 @@ class _NodeReporter(object): tag = getattr(Junit, 'system-' + capname) self.append(tag(bin_xml_escape(allcontent))) - - def append_pass(self, report): self.add_stats('passed') self._write_captured_output(report) @@ -257,6 +255,7 @@ class LogXML(object): 'skipped', ], 0) self.nodereporters = {} # nodeid -> _NodeReporter + self.nodereporters_ordered = [] def nodereporter(self, nodeid): if nodeid in self.nodereporters: @@ -264,6 +263,7 @@ class LogXML(object): return self.nodereporters[nodeid] reporter = _NodeReporter(nodeid, self) self.nodereporters[nodeid] = reporter + self.nodereporters_ordered.append(reporter) return reporter def add_stats(self, key): @@ -348,7 +348,7 @@ class LogXML(object): logfile.write('') logfile.write(Junit.testsuite( - [x.to_xml() for x in self.nodereporters.values()], + [x.to_xml() for x in self.nodereporters_ordered], name="pytest", errors=self.stats['error'], failures=self.stats['failure'], diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 6ef0e4aa9..bc9df28a9 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -12,7 +12,6 @@ import pytest def runandparse(testdir, *args): resultpath = testdir.tmpdir.join("junit.xml") result = testdir.runpytest("--junitxml=%s" % resultpath, *args) - print(resultpath.read()) xmldoc = minidom.parse(str(resultpath)) return result, DomNode(xmldoc) @@ -61,6 +60,7 @@ class DomNode(object): return node.value def assert_attr(self, **kwargs): + __tracebackhide__ = True return assert_attr(self.__node, **kwargs) def toxml(self): From 518c88f149e5ca2a10d680fbff6bbaa82f8da6be Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 10 Oct 2015 19:44:14 +0200 Subject: [PATCH 13/20] finalize nodereporters by throwing away the intermediate xml nodes, fixes issue #1074 --- _pytest/junitxml.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index e6364f7c7..1b82c1c05 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -184,6 +184,12 @@ class _NodeReporter(object): self._write_captured_output(report) + def finalize(self): + data = self.to_xml().unicode(indent=0) + self.__dict__.clear() + self.to_xml = lambda: py.xml.raw(data) + + @pytest.fixture def record_xml_property(request): """Fixture that adds extra xml properties to the tag for the calling test. @@ -312,6 +318,8 @@ class LogXML(object): reporter = self._opentestcase(report) reporter.append_skipped(report) self.update_testcase_duration(report) + if report.when == "teardown": + self.nodereporter(report.nodeid).finalize() def update_testcase_duration(self, report): """accumulates total duration for nodeid from given report and updates From 5eef6a28214b21be31843803614ed35a1da0c85b Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 10 Oct 2015 19:47:21 +0200 Subject: [PATCH 14/20] junitxml: fix python3 compat of the tests --- testing/test_junitxml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index bc9df28a9..1ffe91343 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -52,7 +52,7 @@ class DomNode(object): def find_by_tag(self, tag): t = type(self) - return map(t, self.__node.getElementsByTagName(tag)) + return [t(x) for x in self.__node.getElementsByTagName(tag)] def __getitem__(self, key): node = self.__node.getAttributeNode(key) From 0aa54101c90d50e2ab27d59ba34c8640189dc4b0 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Wed, 14 Oct 2015 12:30:56 +0200 Subject: [PATCH 15/20] junitxml: follow Bruno's attribute/method naming hint from the review --- _pytest/junitxml.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index 1b82c1c05..4627378eb 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -202,8 +202,8 @@ def record_xml_property(request): ) xml = getattr(request.config, "_xml", None) if xml is not None: - nodereporter = xml.nodereporter(request.node.nodeid) - return nodereporter.add_property + node_reporter = xml.node_reporter(request.node.nodeid) + return node_reporter.add_property else: def add_property_noop(name, value): pass @@ -260,16 +260,16 @@ class LogXML(object): 'failure', 'skipped', ], 0) - self.nodereporters = {} # nodeid -> _NodeReporter - self.nodereporters_ordered = [] + self.nodere_porters = {} # nodeid -> _NodeReporter + self.nodere_porters_ordered = [] - def nodereporter(self, nodeid): - if nodeid in self.nodereporters: + def node_reporter(self, nodeid): + if nodeid in self.nodere_porters: #TODO: breasks for --dist=each - return self.nodereporters[nodeid] + return self.nodere_porters[nodeid] reporter = _NodeReporter(nodeid, self) - self.nodereporters[nodeid] = reporter - self.nodereporters_ordered.append(reporter) + self.nodere_porters[nodeid] = reporter + self.nodere_porters_ordered.append(reporter) return reporter def add_stats(self, key): @@ -277,7 +277,7 @@ class LogXML(object): self.stats[key] += 1 def _opentestcase(self, report): - reporter = self.nodereporter(report.nodeid) + reporter = self.node_reporter(report.nodeid) reporter.record_testreport(report) return reporter @@ -319,13 +319,13 @@ class LogXML(object): reporter.append_skipped(report) self.update_testcase_duration(report) if report.when == "teardown": - self.nodereporter(report.nodeid).finalize() + self.node_reporter(report.nodeid).finalize() def update_testcase_duration(self, report): """accumulates total duration for nodeid from given report and updates the Junit.testcase with the new total if already created. """ - reporter = self.nodereporter(report.nodeid) + reporter = self.node_reporter(report.nodeid) reporter.duration += getattr(report, 'duration', 0.0) def pytest_collectreport(self, report): @@ -337,7 +337,7 @@ class LogXML(object): reporter.append_collect_skipped(report) def pytest_internalerror(self, excrepr): - reporter = self.nodereporter('internal') + reporter = self.node_reporter('internal') reporter.attrs.update(classname="pytest", name='internal') reporter._add_simple(Junit.error, 'internal error', excrepr) @@ -356,7 +356,7 @@ class LogXML(object): logfile.write('') logfile.write(Junit.testsuite( - [x.to_xml() for x in self.nodereporters_ordered], + [x.to_xml() for x in self.nodere_porters_ordered], name="pytest", errors=self.stats['error'], failures=self.stats['failure'], From 09cc45b0c5a39b476b8b1c7568ad1446b70d48fd Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Wed, 21 Oct 2015 09:40:14 +0200 Subject: [PATCH 16/20] junitxml: correct docstring of make_properties_node --- _pytest/junitxml.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index 4627378eb..5f8fb1411 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -84,8 +84,7 @@ class _NodeReporter(object): def make_properties_node(self): - """Return a Junit node containing custom properties set for - the current test, if any, and reset the current custom properties. + """Return a Junit node containing custom properties, if any. """ if self.properties: return Junit.properties([ From 9ea78264278399919cc1b4f43a0849c9ba704527 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 24 Nov 2015 21:17:42 +0100 Subject: [PATCH 17/20] Junitxml: correct node reporter attribute names --- _pytest/junitxml.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index 5f8fb1411..c4798b586 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -259,16 +259,16 @@ class LogXML(object): 'failure', 'skipped', ], 0) - self.nodere_porters = {} # nodeid -> _NodeReporter - self.nodere_porters_ordered = [] + self.node_reporters = {} # nodeid -> _NodeReporter + self.node_reporters_ordered = [] def node_reporter(self, nodeid): - if nodeid in self.nodere_porters: + if nodeid in self.node_reporters: #TODO: breasks for --dist=each - return self.nodere_porters[nodeid] + return self.node_reporters[nodeid] reporter = _NodeReporter(nodeid, self) - self.nodere_porters[nodeid] = reporter - self.nodere_porters_ordered.append(reporter) + self.node_reporters[nodeid] = reporter + self.node_reporters_ordered.append(reporter) return reporter def add_stats(self, key): @@ -355,7 +355,7 @@ class LogXML(object): logfile.write('') logfile.write(Junit.testsuite( - [x.to_xml() for x in self.nodere_porters_ordered], + [x.to_xml() for x in self.node_reporters_ordered], name="pytest", errors=self.stats['error'], failures=self.stats['failure'], From 04e9ae75c80e877fea6d3f25694109799c000c2e Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 24 Nov 2015 21:24:15 +0100 Subject: [PATCH 18/20] add xfailing test for double test id failure --- testing/test_junitxml.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 1ffe91343..9ee6824c3 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -659,3 +659,14 @@ def test_random_report_log_xdist(testdir): failed.append(case_node['name']) assert failed == ['test_x[22]'] + + +@pytest.mark.xfail(reason='duplicate test ids kill us') +def test_runs_twice(testdir): + f = testdir.makepyfile(''' + def test_pass(): + pass + ''') + + result = testdir.runpytest(f, f, '--junitxml', testdir.tmpdir.join("test.xml")) + assert 'INTERNALERROR' not in result.stdout \ No newline at end of file From 7b7737bf9694001a00b556ceacd0477b5dd5ae4a Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 6 Dec 2015 13:21:52 +0100 Subject: [PATCH 19/20] handle duplicate test ids via collection and xdist each reporting --- _pytest/junitxml.py | 20 +++++++++++++------- testing/test_junitxml.py | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index c4798b586..1c114ddcb 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -262,12 +262,18 @@ class LogXML(object): self.node_reporters = {} # nodeid -> _NodeReporter self.node_reporters_ordered = [] - def node_reporter(self, nodeid): - if nodeid in self.node_reporters: + def node_reporter(self, report): + nodeid = getattr(report, 'nodeid', report) + # local hack to handle xdist report order + slavenode = getattr(report, 'node', None) + + key = nodeid, slavenode + + if key in self.node_reporters: #TODO: breasks for --dist=each - return self.node_reporters[nodeid] + return self.node_reporters[key] reporter = _NodeReporter(nodeid, self) - self.node_reporters[nodeid] = reporter + self.node_reporters[key] = reporter self.node_reporters_ordered.append(reporter) return reporter @@ -276,7 +282,7 @@ class LogXML(object): self.stats[key] += 1 def _opentestcase(self, report): - reporter = self.node_reporter(report.nodeid) + reporter = self.node_reporter(report) reporter.record_testreport(report) return reporter @@ -318,13 +324,13 @@ class LogXML(object): reporter.append_skipped(report) self.update_testcase_duration(report) if report.when == "teardown": - self.node_reporter(report.nodeid).finalize() + self.node_reporter(report).finalize() def update_testcase_duration(self, report): """accumulates total duration for nodeid from given report and updates the Junit.testcase with the new total if already created. """ - reporter = self.node_reporter(report.nodeid) + reporter = self.node_reporter(report) reporter.duration += getattr(report, 'duration', 0.0) def pytest_collectreport(self, report): diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 9ee6824c3..81a8f4e6a 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -661,7 +661,6 @@ def test_random_report_log_xdist(testdir): assert failed == ['test_x[22]'] -@pytest.mark.xfail(reason='duplicate test ids kill us') def test_runs_twice(testdir): f = testdir.makepyfile(''' def test_pass(): @@ -669,4 +668,17 @@ def test_runs_twice(testdir): ''') result = testdir.runpytest(f, f, '--junitxml', testdir.tmpdir.join("test.xml")) - assert 'INTERNALERROR' not in result.stdout \ No newline at end of file + assert 'INTERNALERROR' not in str(result.stdout) + + +def test_runs_twice_xdist(testdir): + pytest.importorskip('xdist') + f = testdir.makepyfile(''' + def test_pass(): + pass + ''') + + result = testdir.runpytest(f, + '--dist', 'each', '--tx', '2*popen', + '--junitxml', testdir.tmpdir.join("test.xml")) + assert 'INTERNALERROR' not in str(result.stdout) \ No newline at end of file From fde2a6f5fddb6d09503f81eccd2ea1adc4720750 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Mon, 7 Dec 2015 21:58:34 +0100 Subject: [PATCH 20/20] add changelog entry --- CHANGELOG | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 4417a3d6c..49d5a8704 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,10 @@ 2.8.5.dev0 ---------- +- fix #1074: precompute junitxml chunks instead of storing the whole tree in objects + Thanks Bruno Oliveira for the report and Ronny Pfannschmidt for the PR + + 2.8.4 -----