junitxml: simplify tests by creating a api wrapper

This commit is contained in:
Ronny Pfannschmidt 2015-10-10 00:40:50 +02:00
parent b8df5446c0
commit 80bcf8d624
1 changed files with 204 additions and 166 deletions

View File

@ -13,17 +13,61 @@ def runandparse(testdir, *args):
resultpath = testdir.tmpdir.join("junit.xml") resultpath = testdir.tmpdir.join("junit.xml")
result = testdir.runpytest("--junitxml=%s" % resultpath, *args) result = testdir.runpytest("--junitxml=%s" % resultpath, *args)
xmldoc = minidom.parse(str(resultpath)) xmldoc = minidom.parse(str(resultpath))
return result, xmldoc return result, DomNode(xmldoc)
def assert_attr(node, **kwargs): def assert_attr(node, **kwargs):
__tracebackhide__ = True __tracebackhide__ = True
for name, expected in kwargs.items(): expected = dict((name, str(value)) for name, value in kwargs.items())
anode = node.getAttributeNode(name) on_node = dict((name, node.getAttributeNode(name).value) for name in expected)
assert anode, "node %r has no attribute %r" % (node, name) assert on_node == expected
val = anode.value
if val != str(expected):
py.test.fail("%r != %r" % (str(val), str(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: class TestPython:
@ -45,13 +89,8 @@ class TestPython:
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret assert result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, node.assert_attr(name="pytest", errors=0, failures=1, skips=3, tests=2)
name="pytest",
errors=0,
failures=1,
skips=3,
tests=2)
def test_timing_function(self, testdir): def test_timing_function(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
@ -64,9 +103,9 @@ class TestPython:
time.sleep(0.01) time.sleep(0.01)
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
val = tnode.getAttributeNode("time").value val = tnode["time"]
assert round(float(val), 2) >= 0.03 assert round(float(val), 2) >= 0.03
def test_setup_error(self, testdir): def test_setup_error(self, testdir):
@ -78,16 +117,16 @@ class TestPython:
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret assert result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, errors=1, tests=0) node.assert_attr(errors=1, tests=0)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, tnode.assert_attr(
file="test_setup_error.py", file="test_setup_error.py",
line="2", line="2",
classname="test_setup_error", classname="test_setup_error",
name="test_function") name="test_function")
fnode = tnode.getElementsByTagName("error")[0] fnode = tnode.find_first_by_tag("error")
assert_attr(fnode, message="test setup failure") fnode.assert_attr(message="test setup failure")
assert "ValueError" in fnode.toxml() assert "ValueError" in fnode.toxml()
def test_skip_contains_name_reason(self, testdir): def test_skip_contains_name_reason(self, testdir):
@ -98,16 +137,16 @@ class TestPython:
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret == 0 assert result.ret == 0
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, skips=1) node.assert_attr(skips=1)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, tnode.assert_attr(
file="test_skip_contains_name_reason.py", file="test_skip_contains_name_reason.py",
line="1", line="1",
classname="test_skip_contains_name_reason", classname="test_skip_contains_name_reason",
name="test_skip") name="test_skip")
snode = tnode.getElementsByTagName("skipped")[0] snode = tnode.find_first_by_tag("skipped")
assert_attr(snode, type="pytest.skip", message="hello23", ) snode.assert_attr(type="pytest.skip", message="hello23", )
def test_classname_instance(self, testdir): def test_classname_instance(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
@ -117,10 +156,10 @@ class TestPython:
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret assert result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, failures=1) node.assert_attr(failures=1)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, tnode.assert_attr(
file="test_classname_instance.py", file="test_classname_instance.py",
line="1", line="1",
classname="test_classname_instance.TestClass", classname="test_classname_instance.TestClass",
@ -131,10 +170,10 @@ class TestPython:
p.write("def test_func(): 0/0") p.write("def test_func(): 0/0")
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret assert result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, failures=1) node.assert_attr(failures=1)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, tnode.assert_attr(
file=os.path.join("sub", "test_hello.py"), file=os.path.join("sub", "test_hello.py"),
line="0", line="0",
classname="sub.test_hello", classname="sub.test_hello",
@ -145,12 +184,12 @@ class TestPython:
testdir.makepyfile("def test_function(): pass") testdir.makepyfile("def test_function(): pass")
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret assert result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, errors=1, tests=0) node.assert_attr(errors=1, tests=0)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, classname="pytest", name="internal") tnode.assert_attr(classname="pytest", name="internal")
fnode = tnode.getElementsByTagName("error")[0] fnode = tnode.find_first_by_tag("error")
assert_attr(fnode, message="internal error") fnode.assert_attr(message="internal error")
assert "Division" in fnode.toxml() assert "Division" in fnode.toxml()
def test_failure_function(self, testdir): def test_failure_function(self, testdir):
@ -164,22 +203,22 @@ class TestPython:
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret assert result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, failures=1, tests=1) node.assert_attr(failures=1, tests=1)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, tnode.assert_attr(
file="test_failure_function.py", file="test_failure_function.py",
line="1", line="1",
classname="test_failure_function", classname="test_failure_function",
name="test_fail") name="test_fail")
fnode = tnode.getElementsByTagName("failure")[0] fnode = tnode.find_first_by_tag("failure")
assert_attr(fnode, message="ValueError: 42") fnode.assert_attr(message="ValueError: 42")
assert "ValueError" in fnode.toxml() assert "ValueError" in fnode.toxml()
systemout = fnode.nextSibling systemout = fnode.next_siebling
assert systemout.tagName == "system-out" assert systemout.tag == "system-out"
assert "hello-stdout" in systemout.toxml() assert "hello-stdout" in systemout.toxml()
systemerr = systemout.nextSibling systemerr = systemout.next_siebling
assert systemerr.tagName == "system-err" assert systemerr.tag == "system-err"
assert "hello-stderr" in systemerr.toxml() assert "hello-stderr" in systemerr.toxml()
def test_failure_verbose_message(self, testdir): def test_failure_verbose_message(self, testdir):
@ -190,10 +229,10 @@ class TestPython:
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
fnode = tnode.getElementsByTagName("failure")[0] fnode = tnode.find_first_by_tag("failure")
assert_attr(fnode, message="AssertionError: An error assert 0") fnode.assert_attr(message="AssertionError: An error assert 0")
def test_failure_escape(self, testdir): def test_failure_escape(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
@ -205,19 +244,19 @@ class TestPython:
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret assert result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, failures=3, tests=3) node.assert_attr(failures=3, tests=3)
for index, char in enumerate("<&'"): for index, char in enumerate("<&'"):
tnode = node.getElementsByTagName("testcase")[index] tnode = node.find_nth_by_tag("testcase", index)
assert_attr(tnode, tnode.assert_attr(
file="test_failure_escape.py", file="test_failure_escape.py",
line="1", line="1",
classname="test_failure_escape", classname="test_failure_escape",
name="test_func[%s]" % char) name="test_func[%s]" % char)
sysout = tnode.getElementsByTagName('system-out')[0] sysout = tnode.find_first_by_tag('system-out')
text = sysout.childNodes[0].wholeText text = sysout.text
assert text == '%s\n' % char assert text == '%s\n' % char
def test_junit_prefixing(self, testdir): def test_junit_prefixing(self, testdir):
@ -230,16 +269,16 @@ class TestPython:
""") """)
result, dom = runandparse(testdir, "--junitprefix=xyz") result, dom = runandparse(testdir, "--junitprefix=xyz")
assert result.ret assert result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, failures=1, tests=2) node.assert_attr(failures=1, tests=2)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, tnode.assert_attr(
file="test_junit_prefixing.py", file="test_junit_prefixing.py",
line="0", line="0",
classname="xyz.test_junit_prefixing", classname="xyz.test_junit_prefixing",
name="test_func") name="test_func")
tnode = node.getElementsByTagName("testcase")[1] tnode = node.find_nth_by_tag("testcase", 1)
assert_attr(tnode, tnode.assert_attr(
file="test_junit_prefixing.py", file="test_junit_prefixing.py",
line="3", line="3",
classname="xyz.test_junit_prefixing." classname="xyz.test_junit_prefixing."
@ -254,16 +293,16 @@ class TestPython:
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert not result.ret assert not result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, skips=1, tests=0) node.assert_attr(skips=1, tests=0)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, tnode.assert_attr(
file="test_xfailure_function.py", file="test_xfailure_function.py",
line="1", line="1",
classname="test_xfailure_function", classname="test_xfailure_function",
name="test_xfail") name="test_xfail")
fnode = tnode.getElementsByTagName("skipped")[0] fnode = tnode.find_first_by_tag("skipped")
assert_attr(fnode, message="expected test failure") fnode.assert_attr(message="expected test failure")
# assert "ValueError" in fnode.toxml() # assert "ValueError" in fnode.toxml()
def test_xfailure_xpass(self, testdir): def test_xfailure_xpass(self, testdir):
@ -275,49 +314,49 @@ class TestPython:
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
# assert result.ret # assert result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, skips=1, tests=0) node.assert_attr(skips=1, tests=0)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, tnode.assert_attr(
file="test_xfailure_xpass.py", file="test_xfailure_xpass.py",
line="1", line="1",
classname="test_xfailure_xpass", classname="test_xfailure_xpass",
name="test_xpass") name="test_xpass")
fnode = tnode.getElementsByTagName("skipped")[0] fnode = tnode.find_first_by_tag("skipped")
assert_attr(fnode, message="xfail-marked test passes unexpectedly") fnode.assert_attr(message="xfail-marked test passes unexpectedly")
# assert "ValueError" in fnode.toxml() # assert "ValueError" in fnode.toxml()
def test_collect_error(self, testdir): def test_collect_error(self, testdir):
testdir.makepyfile("syntax error") testdir.makepyfile("syntax error")
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret assert result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, errors=1, tests=0) node.assert_attr(errors=1, tests=0)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, tnode.assert_attr(
file="test_collect_error.py", file="test_collect_error.py",
name="test_collect_error") name="test_collect_error")
assert tnode.getAttributeNode("line") is None assert tnode["line"] is None
fnode = tnode.getElementsByTagName("error")[0] fnode = tnode.find_first_by_tag("error")
assert_attr(fnode, message="collection failure") fnode.assert_attr(message="collection failure")
assert "SyntaxError" in fnode.toxml() assert "SyntaxError" in fnode.toxml()
def test_collect_skipped(self, testdir): def test_collect_skipped(self, testdir):
testdir.makepyfile("import pytest; pytest.skip('xyz')") testdir.makepyfile("import pytest; pytest.skip('xyz')")
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret == EXIT_NOTESTSCOLLECTED assert result.ret == EXIT_NOTESTSCOLLECTED
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, skips=1, tests=0) node.assert_attr(skips=1, tests=0)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, tnode.assert_attr(
file="test_collect_skipped.py", file="test_collect_skipped.py",
name="test_collect_skipped") name="test_collect_skipped")
# py.test doesn't give us a line here. # 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] fnode = tnode.find_first_by_tag("skipped")
assert_attr(fnode, message="collection skipped") fnode.assert_attr(message="collection skipped")
def test_unicode(self, testdir): def test_unicode(self, testdir):
value = 'hx\xc4\x85\xc4\x87\n' value = 'hx\xc4\x85\xc4\x87\n'
@ -329,8 +368,8 @@ class TestPython:
""" % value) """ % value)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret == 1 assert result.ret == 1
tnode = dom.getElementsByTagName("testcase")[0] tnode = dom.find_first_by_tag("testcase")
fnode = tnode.getElementsByTagName("failure")[0] fnode = tnode.find_first_by_tag("failure")
if not sys.platform.startswith("java"): if not sys.platform.startswith("java"):
assert "hx" in fnode.toxml() assert "hx" in fnode.toxml()
@ -353,9 +392,9 @@ class TestPython:
print('hello-stdout') print('hello-stdout')
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
pnode = node.getElementsByTagName("testcase")[0] pnode = node.find_first_by_tag("testcase")
systemout = pnode.getElementsByTagName("system-out")[0] systemout = pnode.find_first_by_tag("system-out")
assert "hello-stdout" in systemout.toxml() assert "hello-stdout" in systemout.toxml()
def test_pass_captures_stderr(self, testdir): def test_pass_captures_stderr(self, testdir):
@ -365,9 +404,9 @@ class TestPython:
sys.stderr.write('hello-stderr') sys.stderr.write('hello-stderr')
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
pnode = node.getElementsByTagName("testcase")[0] pnode = node.find_first_by_tag("testcase")
systemout = pnode.getElementsByTagName("system-err")[0] systemout = pnode.find_first_by_tag("system-err")
assert "hello-stderr" in systemout.toxml() assert "hello-stderr" in systemout.toxml()
@ -419,13 +458,12 @@ class TestNonPython:
testdir.tmpdir.join("myfile.xyz").write("hello") testdir.tmpdir.join("myfile.xyz").write("hello")
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret assert result.ret
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
assert_attr(node, errors=0, failures=1, skips=0, tests=1) node.assert_attr(errors=0, failures=1, skips=0, tests=1)
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
assert_attr(tnode, # classname="test_collect_error", tnode.assert_attr(name="myfile.xyz")
name="myfile.xyz") fnode = tnode.find_first_by_tag("failure")
fnode = tnode.getElementsByTagName("failure")[0] fnode.assert_attr(message="custom item runtest failed")
assert_attr(fnode, message="custom item runtest failed")
assert "custom item runtest failed" in fnode.toxml() assert "custom item runtest failed" in fnode.toxml()
@ -538,8 +576,8 @@ def test_escaped_parametrized_names_xml(testdir):
""") """)
result, dom = runandparse(testdir) result, dom = runandparse(testdir)
assert result.ret == 0 assert result.ret == 0
node = dom.getElementsByTagName("testcase")[0] node = dom.find_first_by_tag("testcase")
assert_attr(node, name="test_func[#x00]") node.assert_attr(name="test_func[#x00]")
def test_unicode_issue368(testdir): def test_unicode_issue368(testdir):
@ -578,11 +616,11 @@ def test_record_property(testdir):
record_xml_property("foo", "<1"); record_xml_property("foo", "<1");
""") """)
result, dom = runandparse(testdir, '-rw') result, dom = runandparse(testdir, '-rw')
node = dom.getElementsByTagName("testsuite")[0] node = dom.find_first_by_tag("testsuite")
tnode = node.getElementsByTagName("testcase")[0] tnode = node.find_first_by_tag("testcase")
psnode = tnode.getElementsByTagName('properties')[0] psnode = tnode.find_first_by_tag('properties')
pnode = psnode.getElementsByTagName('property')[0] pnode = psnode.find_first_by_tag('property')
assert_attr(pnode, name="foo", value="<1") pnode.assert_attr(name="foo", value="<1")
result.stdout.fnmatch_lines('*C3*test_record_property.py*experimental*') result.stdout.fnmatch_lines('*C3*test_record_property.py*experimental*')
@ -599,10 +637,10 @@ def test_random_report_log_xdist(testdir):
assert i != 22 assert i != 22
""") """)
_, dom = runandparse(testdir, '-n2') _, dom = runandparse(testdir, '-n2')
suite_node = dom.getElementsByTagName("testsuite")[0] suite_node = dom.find_first_by_tag("testsuite")
failed = [] failed = []
for case_node in suite_node.getElementsByTagName("testcase"): for case_node in suite_node.find_by_tag("testcase"):
if case_node.getElementsByTagName('failure'): if case_node.find_first_by_tag('failure'):
failed.append(case_node.getAttributeNode('name').value) failed.append(case_node['name'])
assert failed == ['test_x[22]'] assert failed == ['test_x[22]']