2009-12-31 18:25:07 +08:00
|
|
|
|
|
|
|
from xml.dom import minidom
|
2010-04-28 21:24:38 +08:00
|
|
|
import py, sys
|
2009-12-31 18:25:07 +08:00
|
|
|
|
|
|
|
def runandparse(testdir, *args):
|
|
|
|
resultpath = testdir.tmpdir.join("junit.xml")
|
2010-01-17 06:33:26 +08:00
|
|
|
result = testdir.runpytest("--junitxml=%s" % resultpath, *args)
|
2009-12-31 18:25:07 +08:00
|
|
|
xmldoc = minidom.parse(str(resultpath))
|
|
|
|
return result, xmldoc
|
|
|
|
|
|
|
|
def assert_attr(node, **kwargs):
|
2010-11-06 16:58:04 +08:00
|
|
|
__tracebackhide__ = True
|
2009-12-31 18:25:07 +08:00
|
|
|
for name, expected in kwargs.items():
|
|
|
|
anode = node.getAttributeNode(name)
|
|
|
|
assert anode, "node %r has no attribute %r" %(node, name)
|
2010-07-27 03:15:15 +08:00
|
|
|
val = anode.value
|
2010-11-06 16:58:04 +08:00
|
|
|
if val != str(expected):
|
|
|
|
py.test.fail("%r != %r" %(str(val), str(expected)))
|
2009-12-31 18:25:07 +08:00
|
|
|
|
|
|
|
class TestPython:
|
|
|
|
def test_summing_simple(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-12-31 18:25:07 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
def test_fail():
|
|
|
|
assert 0
|
|
|
|
def test_skip():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("")
|
|
|
|
@pytest.mark.xfail
|
2010-05-20 20:35:13 +08:00
|
|
|
def test_xfail():
|
|
|
|
assert 0
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.xfail
|
2010-05-20 20:35:13 +08:00
|
|
|
def test_xpass():
|
|
|
|
assert 1
|
2009-12-31 18:25:07 +08:00
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2009-12-31 18:25:07 +08:00
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
2010-05-20 20:35:13 +08:00
|
|
|
assert_attr(node, errors=0, failures=1, skips=3, tests=2)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
|
|
|
def test_setup_error(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def pytest_funcarg__arg(request):
|
|
|
|
raise ValueError()
|
|
|
|
def test_function(arg):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2009-12-31 18:25:07 +08:00
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
assert_attr(node, errors=1, tests=0)
|
|
|
|
tnode = node.getElementsByTagName("testcase")[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2010-11-06 16:58:04 +08:00
|
|
|
classname="test_setup_error",
|
2009-12-31 18:25:07 +08:00
|
|
|
name="test_function")
|
|
|
|
fnode = tnode.getElementsByTagName("error")[0]
|
|
|
|
assert_attr(fnode, message="test setup failure")
|
|
|
|
assert "ValueError" in fnode.toxml()
|
|
|
|
|
2010-01-12 08:35:50 +08:00
|
|
|
def test_classname_instance(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
class TestClass:
|
|
|
|
def test_method(self):
|
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2010-01-12 08:35:50 +08:00
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
assert_attr(node, failures=1)
|
|
|
|
tnode = node.getElementsByTagName("testcase")[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2010-11-06 16:58:04 +08:00
|
|
|
classname="test_classname_instance.TestClass",
|
2010-01-12 08:35:50 +08:00
|
|
|
name="test_method")
|
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
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,
|
|
|
|
classname="sub.test_hello",
|
|
|
|
name="test_func")
|
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def test_internal_error(self, testdir):
|
|
|
|
testdir.makeconftest("def pytest_runtest_protocol(): 0 / 0")
|
|
|
|
testdir.makepyfile("def test_function(): pass")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2009-12-31 18:25:07 +08:00
|
|
|
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")
|
|
|
|
assert "Division" in fnode.toxml()
|
|
|
|
|
|
|
|
def test_failure_function(self, testdir):
|
|
|
|
testdir.makepyfile("def test_fail(): raise ValueError(42)")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2009-12-31 18:25:07 +08:00
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
assert_attr(node, failures=1, tests=1)
|
|
|
|
tnode = node.getElementsByTagName("testcase")[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2010-11-06 16:58:04 +08:00
|
|
|
classname="test_failure_function",
|
2009-12-31 18:25:07 +08:00
|
|
|
name="test_fail")
|
|
|
|
fnode = tnode.getElementsByTagName("failure")[0]
|
|
|
|
assert_attr(fnode, message="test failure")
|
|
|
|
assert "ValueError" in fnode.toxml()
|
|
|
|
|
2010-06-09 21:27:45 +08:00
|
|
|
def test_failure_escape(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.addcall(id="<", funcargs=dict(arg1=42))
|
|
|
|
metafunc.addcall(id="&", funcargs=dict(arg1=44))
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func(arg1):
|
2010-06-09 21:27:45 +08:00
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2010-06-09 21:27:45 +08:00
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
assert_attr(node, failures=2, tests=2)
|
|
|
|
tnode = node.getElementsByTagName("testcase")[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2010-11-06 16:58:04 +08:00
|
|
|
classname="test_failure_escape",
|
2010-06-09 21:27:45 +08:00
|
|
|
name="test_func[<]")
|
|
|
|
tnode = node.getElementsByTagName("testcase")[1]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2010-11-06 16:58:04 +08:00
|
|
|
classname="test_failure_escape",
|
2010-06-09 21:27:45 +08:00
|
|
|
name="test_func[&]")
|
|
|
|
|
2010-07-07 20:43:31 +08:00
|
|
|
def test_junit_prefixing(self, testdir):
|
2010-06-09 22:18:47 +08:00
|
|
|
testdir.makepyfile("""
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func():
|
2010-06-09 22:18:47 +08:00
|
|
|
assert 0
|
|
|
|
class TestHello:
|
|
|
|
def test_hello(self):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir, "--junitprefix=xyz")
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2010-06-09 22:18:47 +08:00
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
assert_attr(node, failures=1, tests=2)
|
|
|
|
tnode = node.getElementsByTagName("testcase")[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2010-11-06 16:58:04 +08:00
|
|
|
classname="xyz.test_junit_prefixing",
|
2010-06-09 22:18:47 +08:00
|
|
|
name="test_func")
|
|
|
|
tnode = node.getElementsByTagName("testcase")[1]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2010-11-06 16:58:04 +08:00
|
|
|
classname="xyz.test_junit_prefixing."
|
2010-07-27 03:15:15 +08:00
|
|
|
"TestHello",
|
2010-06-09 22:18:47 +08:00
|
|
|
name="test_hello")
|
|
|
|
|
2010-05-20 20:35:13 +08:00
|
|
|
def test_xfailure_function(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-05-20 20:35:13 +08:00
|
|
|
def test_xfail():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.xfail("42")
|
2010-05-20 20:35:13 +08:00
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not result.ret
|
2010-05-20 20:35:13 +08:00
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
assert_attr(node, skips=1, tests=0)
|
|
|
|
tnode = node.getElementsByTagName("testcase")[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2010-11-06 16:58:04 +08:00
|
|
|
classname="test_xfailure_function",
|
2010-05-20 20:35:13 +08:00
|
|
|
name="test_xfail")
|
|
|
|
fnode = tnode.getElementsByTagName("skipped")[0]
|
|
|
|
assert_attr(fnode, message="expected test failure")
|
|
|
|
#assert "ValueError" in fnode.toxml()
|
|
|
|
|
|
|
|
def test_xfailure_xpass(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.xfail
|
2010-05-20 20:35:13 +08:00
|
|
|
def test_xpass():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
#assert result.ret
|
2010-05-20 20:35:13 +08:00
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
assert_attr(node, skips=1, tests=0)
|
|
|
|
tnode = node.getElementsByTagName("testcase")[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2010-11-06 16:58:04 +08:00
|
|
|
classname="test_xfailure_xpass",
|
2010-05-20 20:35:13 +08:00
|
|
|
name="test_xpass")
|
|
|
|
fnode = tnode.getElementsByTagName("skipped")[0]
|
|
|
|
assert_attr(fnode, message="xfail-marked test passes unexpectedly")
|
|
|
|
#assert "ValueError" in fnode.toxml()
|
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def test_collect_error(self, testdir):
|
|
|
|
testdir.makepyfile("syntax error")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2009-12-31 18:25:07 +08:00
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
assert_attr(node, errors=1, tests=0)
|
|
|
|
tnode = node.getElementsByTagName("testcase")[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2009-12-31 18:25:07 +08:00
|
|
|
#classname="test_collect_error",
|
|
|
|
name="test_collect_error")
|
|
|
|
fnode = tnode.getElementsByTagName("failure")[0]
|
|
|
|
assert_attr(fnode, message="collection failure")
|
2010-04-28 21:24:38 +08:00
|
|
|
assert "SyntaxError" in fnode.toxml()
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2009-12-31 18:50:01 +08:00
|
|
|
def test_collect_skipped(self, testdir):
|
2010-11-18 05:12:16 +08:00
|
|
|
testdir.makepyfile("import pytest; pytest.skip('xyz')")
|
2009-12-31 18:50:01 +08:00
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not result.ret
|
2009-12-31 18:50:01 +08:00
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
assert_attr(node, skips=1, tests=0)
|
|
|
|
tnode = node.getElementsByTagName("testcase")[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2009-12-31 18:50:01 +08:00
|
|
|
#classname="test_collect_error",
|
|
|
|
name="test_collect_skipped")
|
|
|
|
fnode = tnode.getElementsByTagName("skipped")[0]
|
|
|
|
assert_attr(fnode, message="collection skipped")
|
|
|
|
|
2010-04-27 21:15:43 +08:00
|
|
|
def test_unicode(self, testdir):
|
|
|
|
value = 'hx\xc4\x85\xc4\x87\n'
|
|
|
|
testdir.makepyfile("""
|
2010-11-09 06:48:15 +08:00
|
|
|
# coding: utf-8
|
2010-04-27 21:15:43 +08:00
|
|
|
def test_hello():
|
|
|
|
print (%r)
|
|
|
|
assert 0
|
|
|
|
""" % value)
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret == 1
|
|
|
|
tnode = dom.getElementsByTagName("testcase")[0]
|
|
|
|
fnode = tnode.getElementsByTagName("failure")[0]
|
2010-04-28 21:24:38 +08:00
|
|
|
if not sys.platform.startswith("java"):
|
|
|
|
assert "hx" in fnode.toxml()
|
2010-04-27 21:15:43 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
class TestNonPython:
|
|
|
|
def test_summing_simple(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2009-12-31 18:25:07 +08:00
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
if path.ext == ".xyz":
|
|
|
|
return MyItem(path, parent)
|
2010-11-13 16:05:11 +08:00
|
|
|
class MyItem(pytest.Item):
|
2009-12-31 18:25:07 +08:00
|
|
|
def __init__(self, path, parent):
|
|
|
|
super(MyItem, self).__init__(path.basename, parent)
|
|
|
|
self.fspath = path
|
|
|
|
def runtest(self):
|
|
|
|
raise ValueError(42)
|
|
|
|
def repr_failure(self, excinfo):
|
|
|
|
return "custom item runtest failed"
|
|
|
|
""")
|
|
|
|
testdir.tmpdir.join("myfile.xyz").write("hello")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2009-12-31 18:25:07 +08:00
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
assert_attr(node, errors=0, failures=1, skips=0, tests=1)
|
|
|
|
tnode = node.getElementsByTagName("testcase")[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert_attr(tnode,
|
2009-12-31 18:25:07 +08:00
|
|
|
#classname="test_collect_error",
|
|
|
|
name="myfile.xyz")
|
|
|
|
fnode = tnode.getElementsByTagName("failure")[0]
|
|
|
|
assert_attr(fnode, message="test failure")
|
|
|
|
assert "custom item runtest failed" in fnode.toxml()
|
2010-07-27 03:15:15 +08:00
|
|
|
|