2013-10-21 22:54:25 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
from xml.dom import minidom
|
2011-05-27 13:54:03 +08:00
|
|
|
import py, sys, os
|
2013-10-21 22:54:25 +08:00
|
|
|
from _pytest.junitxml import LogXML
|
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]
|
2013-09-26 14:45:50 +08:00
|
|
|
assert_attr(node, name="pytest", errors=0, failures=1, skips=3, tests=2)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2011-05-28 20:03:10 +08:00
|
|
|
def test_timing_function(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import time, pytest
|
|
|
|
def test_sleep():
|
|
|
|
time.sleep(0.01)
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
tnode = node.getElementsByTagName("testcase")[0]
|
|
|
|
val = tnode.getAttributeNode("time").value
|
2011-05-29 06:45:31 +08:00
|
|
|
assert float(val) >= 0.001
|
2011-05-28 20:03:10 +08:00
|
|
|
|
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()
|
|
|
|
|
2011-03-20 00:59:07 +08:00
|
|
|
def test_skip_contains_name_reason(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
def test_skip():
|
|
|
|
pytest.skip("hello23")
|
|
|
|
""")
|
|
|
|
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,
|
|
|
|
classname="test_skip_contains_name_reason",
|
|
|
|
name="test_skip")
|
|
|
|
snode = tnode.getElementsByTagName("skipped")[0]
|
|
|
|
assert_attr(snode,
|
|
|
|
type="pytest.skip",
|
|
|
|
message="hello23",
|
|
|
|
)
|
|
|
|
|
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):
|
2011-07-13 05:09:03 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import sys
|
|
|
|
def test_fail():
|
|
|
|
print ("hello-stdout")
|
|
|
|
sys.stderr.write("hello-stderr\\n")
|
|
|
|
raise ValueError(42)
|
|
|
|
""")
|
2012-06-23 17:32:32 +08:00
|
|
|
|
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]
|
|
|
|
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()
|
2011-07-14 00:47:27 +08:00
|
|
|
systemout = fnode.nextSibling
|
|
|
|
assert systemout.tagName == "system-out"
|
2011-07-13 05:09:03 +08:00
|
|
|
assert "hello-stdout" in systemout.toxml()
|
2011-07-14 00:47:27 +08:00
|
|
|
systemerr = systemout.nextSibling
|
|
|
|
assert systemerr.tagName == "system-err"
|
2011-07-13 05:09:03 +08:00
|
|
|
assert "hello-stderr" in systemerr.toxml()
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2010-06-09 21:27:45 +08:00
|
|
|
def test_failure_escape(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2012-08-17 22:08:08 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('arg1', "<&'", ids="<&'")
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func(arg1):
|
2012-08-29 04:37:43 +08:00
|
|
|
print(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]
|
2012-08-17 22:08:08 +08:00
|
|
|
assert_attr(node, failures=3, tests=3)
|
|
|
|
|
|
|
|
for index, char in enumerate("<&'"):
|
2012-10-19 16:53:28 +08:00
|
|
|
|
2012-08-17 22:08:08 +08:00
|
|
|
tnode = node.getElementsByTagName("testcase")[index]
|
|
|
|
assert_attr(tnode,
|
|
|
|
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
|
|
|
|
|
2010-06-09 21:27:45 +08:00
|
|
|
|
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("""
|
2012-10-19 16:53:28 +08:00
|
|
|
# coding: latin1
|
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
|
|
|
|
2013-04-16 12:45:14 +08:00
|
|
|
def test_pass_captures_stdout(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_pass():
|
|
|
|
print('hello-stdout')
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
node = dom.getElementsByTagName("testsuite")[0]
|
|
|
|
pnode = node.getElementsByTagName("testcase")[0]
|
|
|
|
systemout = pnode.getElementsByTagName("system-out")[0]
|
|
|
|
assert "hello-stdout" in systemout.toxml()
|
|
|
|
|
|
|
|
def test_pass_captures_stderr(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import sys
|
|
|
|
def test_pass():
|
|
|
|
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]
|
|
|
|
assert "hello-stderr" in systemout.toxml()
|
|
|
|
|
2012-05-22 23:18:04 +08:00
|
|
|
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"]
|
|
|
|
|
2013-03-25 03:43:25 +08:00
|
|
|
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 ?
|
|
|
|
xmlpath = str(tmpdir.join('junix.xml'))
|
|
|
|
register = gotten.append
|
|
|
|
fake_config = FakeConfig()
|
|
|
|
from _pytest import junitxml
|
|
|
|
junitxml.pytest_configure(fake_config)
|
|
|
|
assert len(gotten) == 1
|
|
|
|
FakeConfig.slaveinput = None
|
|
|
|
junitxml.pytest_configure(fake_config)
|
|
|
|
assert len(gotten) == 1
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-04-16 07:09:25 +08:00
|
|
|
|
|
|
|
def test_nullbyte(testdir):
|
|
|
|
# A null byte can not occur in XML (see section 2.2 of the spec)
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import sys
|
|
|
|
def test_print_nullbyte():
|
|
|
|
sys.stdout.write('Here the null -->' + chr(0) + '<--')
|
|
|
|
sys.stdout.write('In repr form -->' + repr(chr(0)) + '<--')
|
|
|
|
assert False
|
|
|
|
""")
|
|
|
|
xmlf = testdir.tmpdir.join('junit.xml')
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.runpytest('--junitxml=%s' % xmlf)
|
2011-04-16 07:09:25 +08:00
|
|
|
text = xmlf.read()
|
|
|
|
assert '\x00' not in text
|
|
|
|
assert '#x00' in text
|
|
|
|
|
|
|
|
|
|
|
|
def test_nullbyte_replace(testdir):
|
|
|
|
# Check if the null byte gets replaced
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import sys
|
|
|
|
def test_print_nullbyte():
|
|
|
|
sys.stdout.write('Here the null -->' + chr(0) + '<--')
|
|
|
|
sys.stdout.write('In repr form -->' + repr(chr(0)) + '<--')
|
|
|
|
assert False
|
|
|
|
""")
|
|
|
|
xmlf = testdir.tmpdir.join('junit.xml')
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.runpytest('--junitxml=%s' % xmlf)
|
2011-04-16 07:09:25 +08:00
|
|
|
text = xmlf.read()
|
|
|
|
assert '#x0' in text
|
|
|
|
|
|
|
|
|
2012-03-09 20:12:18 +08:00
|
|
|
def test_invalid_xml_escape():
|
2011-04-16 07:09:25 +08:00
|
|
|
# Test some more invalid xml chars, the full range should be
|
|
|
|
# tested really but let's just thest the edges of the ranges
|
|
|
|
# intead.
|
|
|
|
# XXX This only tests low unicode character points for now as
|
|
|
|
# there are some issues with the testing infrastructure for
|
|
|
|
# the higher ones.
|
|
|
|
# XXX Testing 0xD (\r) is tricky as it overwrites the just written
|
|
|
|
# line in the output, so we skip it too.
|
|
|
|
global unichr
|
|
|
|
try:
|
|
|
|
unichr(65)
|
|
|
|
except NameError:
|
|
|
|
unichr = chr
|
2012-03-09 20:12:18 +08:00
|
|
|
invalid = (0x00, 0x1, 0xB, 0xC, 0xE, 0x19,
|
2012-04-18 23:26:44 +08:00
|
|
|
27, # issue #126
|
2012-03-09 20:12:18 +08:00
|
|
|
0xD800, 0xDFFF, 0xFFFE, 0x0FFFF) #, 0x110000)
|
2011-04-16 07:09:25 +08:00
|
|
|
valid = (0x9, 0xA, 0x20,) # 0xD, 0xD7FF, 0xE000, 0xFFFD, 0x10000, 0x10FFFF)
|
2012-06-23 17:32:32 +08:00
|
|
|
|
2012-03-09 20:12:18 +08:00
|
|
|
from _pytest.junitxml import bin_xml_escape
|
|
|
|
|
|
|
|
|
2011-04-16 07:09:25 +08:00
|
|
|
for i in invalid:
|
2012-09-03 15:54:02 +08:00
|
|
|
got = bin_xml_escape(unichr(i)).uniobj
|
2011-04-16 07:09:25 +08:00
|
|
|
if i <= 0xFF:
|
2012-03-09 20:12:18 +08:00
|
|
|
expected = '#x%02X' % i
|
2011-04-16 07:09:25 +08:00
|
|
|
else:
|
2012-03-09 20:12:18 +08:00
|
|
|
expected = '#x%04X' % i
|
|
|
|
assert got == expected
|
2011-04-16 07:09:25 +08:00
|
|
|
for i in valid:
|
2012-09-03 15:54:02 +08:00
|
|
|
assert chr(i) == bin_xml_escape(unichr(i)).uniobj
|
2011-05-27 13:54:03 +08:00
|
|
|
|
2012-10-08 19:42:31 +08:00
|
|
|
def test_logxml_path_expansion(tmpdir, monkeypatch):
|
2013-10-01 18:37:11 +08:00
|
|
|
home_tilde = py.path.local(os.path.expanduser('~')).join('test.xml')
|
2011-05-27 13:54:03 +08:00
|
|
|
|
2013-10-01 18:37:11 +08:00
|
|
|
xml_tilde = LogXML('~%stest.xml' % tmpdir.sep, None)
|
2011-05-27 13:54:03 +08:00
|
|
|
assert xml_tilde.logfile == home_tilde
|
|
|
|
|
2012-10-08 19:42:31 +08:00
|
|
|
# this is here for when $HOME is not set correct
|
|
|
|
monkeypatch.setenv("HOME", tmpdir)
|
|
|
|
home_var = os.path.normpath(os.path.expandvars('$HOME/test.xml'))
|
|
|
|
|
2013-10-01 18:37:11 +08:00
|
|
|
xml_var = LogXML('$HOME%stest.xml' % tmpdir.sep, None)
|
2011-05-27 13:54:03 +08:00
|
|
|
assert xml_var.logfile == home_var
|
2012-08-04 16:33:43 +08:00
|
|
|
|
|
|
|
def test_logxml_changingdir(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_func():
|
|
|
|
import os
|
|
|
|
os.chdir("a")
|
|
|
|
""")
|
|
|
|
testdir.tmpdir.mkdir("a")
|
|
|
|
result = testdir.runpytest("--junitxml=a/x.xml")
|
|
|
|
assert result.ret == 0
|
|
|
|
assert testdir.tmpdir.join("a/x.xml").check()
|
|
|
|
|
2013-05-10 03:16:57 +08:00
|
|
|
def test_escaped_parametrized_names_xml(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('char', ["\\x00"])
|
|
|
|
def test_func(char):
|
|
|
|
assert char
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.getElementsByTagName("testcase")[0]
|
|
|
|
assert_attr(node,
|
|
|
|
name="test_func[#x00]")
|
|
|
|
|
2013-10-21 22:54:25 +08:00
|
|
|
def test_unicode_issue368(testdir):
|
|
|
|
path = testdir.tmpdir.join("test.xml")
|
|
|
|
log = LogXML(str(path), None)
|
2013-10-22 17:26:29 +08:00
|
|
|
ustr = py.builtin._totext("ВНИ!", "utf-8")
|
2013-10-21 22:54:25 +08:00
|
|
|
class report:
|
2013-10-22 17:26:29 +08:00
|
|
|
longrepr = ustr
|
2013-10-21 22:54:25 +08:00
|
|
|
sections = []
|
|
|
|
nodeid = "something"
|
|
|
|
|
|
|
|
# hopefully this is not too brittle ...
|
|
|
|
log.pytest_sessionstart()
|
|
|
|
log._opentestcase(report)
|
|
|
|
log.append_failure(report)
|
|
|
|
log.append_collect_failure(report)
|
|
|
|
log.append_collect_skipped(report)
|
|
|
|
log.append_error(report)
|
2013-10-22 17:26:29 +08:00
|
|
|
report.longrepr = "filename", 1, ustr
|
2013-10-21 22:54:25 +08:00
|
|
|
log.append_skipped(report)
|
2013-10-22 17:26:29 +08:00
|
|
|
report.wasxfail = ustr
|
2013-10-21 22:54:25 +08:00
|
|
|
log.append_skipped(report)
|
|
|
|
log.pytest_sessionfinish()
|
|
|
|
|
|
|
|
|