2013-10-21 22:54:25 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2009-12-31 18:25:07 +08:00
|
|
|
from xml.dom import minidom
|
2015-09-30 04:39:18 +08:00
|
|
|
import py
|
|
|
|
import sys
|
|
|
|
import os
|
2013-10-21 22:54:25 +08:00
|
|
|
from _pytest.junitxml import LogXML
|
2015-09-26 14:11:23 +08:00
|
|
|
import pytest
|
|
|
|
|
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))
|
2015-10-10 06:40:50 +08:00
|
|
|
return result, DomNode(xmldoc)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def assert_attr(node, **kwargs):
|
2010-11-06 16:58:04 +08:00
|
|
|
__tracebackhide__ = True
|
2015-12-17 02:07:36 +08:00
|
|
|
|
2015-10-10 15:31:52 +08:00
|
|
|
def nodeval(node, name):
|
|
|
|
anode = node.getAttributeNode(name)
|
|
|
|
if anode is not None:
|
|
|
|
return anode.value
|
|
|
|
|
2015-10-10 06:40:50 +08:00
|
|
|
expected = dict((name, str(value)) for name, value in kwargs.items())
|
2015-10-10 15:31:52 +08:00
|
|
|
on_node = dict((name, nodeval(node, name)) for name in expected)
|
2015-10-10 06:40:50 +08:00
|
|
|
assert on_node == expected
|
|
|
|
|
|
|
|
|
|
|
|
class DomNode(object):
|
|
|
|
def __init__(self, dom):
|
|
|
|
self.__node = dom
|
|
|
|
|
2015-10-10 20:39:25 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return self.__node.toxml()
|
|
|
|
|
2015-10-10 06:40:50 +08:00
|
|
|
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)
|
2015-10-11 01:47:21 +08:00
|
|
|
return [t(x) for x in self.__node.getElementsByTagName(tag)]
|
2015-10-10 06:40:50 +08:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
node = self.__node.getAttributeNode(key)
|
|
|
|
if node is not None:
|
|
|
|
return node.value
|
|
|
|
|
|
|
|
def assert_attr(self, **kwargs):
|
2015-10-11 01:35:58 +08:00
|
|
|
__tracebackhide__ = True
|
2015-10-10 06:40:50 +08:00
|
|
|
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)
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestPython(object):
|
2009-12-31 18:25:07 +08:00
|
|
|
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
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2016-08-16 06:58:16 +08:00
|
|
|
node.assert_attr(name="pytest", errors=0, failures=1, skips=2, tests=5)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2016-08-08 20:29:20 +08:00
|
|
|
def test_summing_simple_with_errors(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fixture():
|
|
|
|
raise Exception()
|
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
def test_fail():
|
|
|
|
assert 0
|
|
|
|
def test_error(fixture):
|
|
|
|
pass
|
|
|
|
@pytest.mark.xfail
|
2016-08-18 05:32:27 +08:00
|
|
|
def test_xfail():
|
|
|
|
assert False
|
|
|
|
@pytest.mark.xfail(strict=True)
|
2016-08-08 20:29:20 +08:00
|
|
|
def test_xpass():
|
2016-08-18 05:32:27 +08:00
|
|
|
assert True
|
2016-08-08 20:29:20 +08:00
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2016-08-18 05:32:27 +08:00
|
|
|
node.assert_attr(name="pytest", errors=1, failures=2, skips=1, tests=5)
|
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
|
2015-05-01 19:55:52 +08:00
|
|
|
def setup_module():
|
|
|
|
time.sleep(0.01)
|
|
|
|
def teardown_module():
|
|
|
|
time.sleep(0.01)
|
2011-05-28 20:03:10 +08:00
|
|
|
def test_sleep():
|
|
|
|
time.sleep(0.01)
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
val = tnode["time"]
|
2015-07-05 01:49:04 +08:00
|
|
|
assert round(float(val), 2) >= 0.03
|
2011-05-28 20:03:10 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def test_setup_error(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg(request):
|
2009-12-31 18:25:07 +08:00
|
|
|
raise ValueError()
|
|
|
|
def test_function(arg):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2016-08-08 20:29:20 +08:00
|
|
|
node.assert_attr(errors=1, tests=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(
|
|
|
|
file="test_setup_error.py",
|
2016-07-12 09:03:53 +08:00
|
|
|
line="5",
|
2015-10-10 06:40:50 +08:00
|
|
|
classname="test_setup_error",
|
|
|
|
name="test_function")
|
|
|
|
fnode = tnode.find_first_by_tag("error")
|
|
|
|
fnode.assert_attr(message="test setup failure")
|
2009-12-31 18:25:07 +08:00
|
|
|
assert "ValueError" in fnode.toxml()
|
|
|
|
|
2016-11-02 20:45:40 +08:00
|
|
|
def test_teardown_error(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg():
|
|
|
|
yield
|
|
|
|
raise ValueError()
|
|
|
|
def test_function(arg):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(
|
|
|
|
file="test_teardown_error.py",
|
|
|
|
line="6",
|
|
|
|
classname="test_teardown_error",
|
|
|
|
name="test_function")
|
|
|
|
fnode = tnode.find_first_by_tag("error")
|
|
|
|
fnode.assert_attr(message="test teardown failure")
|
|
|
|
assert "ValueError" in fnode.toxml()
|
|
|
|
|
2017-02-03 17:30:28 +08:00
|
|
|
def test_call_failure_teardown_error(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg():
|
|
|
|
yield
|
|
|
|
raise Exception("Teardown Exception")
|
|
|
|
def test_function(arg):
|
|
|
|
raise Exception("Call Exception")
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(errors=1, failures=1, tests=1)
|
|
|
|
first, second = dom.find_by_tag("testcase")
|
|
|
|
if not first or not second or first == second:
|
|
|
|
assert 0
|
|
|
|
fnode = first.find_first_by_tag("failure")
|
|
|
|
fnode.assert_attr(message="Exception: Call Exception")
|
|
|
|
snode = second.find_first_by_tag("error")
|
|
|
|
snode.assert_attr(message="test teardown failure")
|
|
|
|
|
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
|
2015-10-10 06:40:50 +08:00
|
|
|
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", )
|
2011-03-20 00:59:07 +08:00
|
|
|
|
2016-04-11 01:57:45 +08:00
|
|
|
def test_mark_skip_contains_name_reason(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.skip(reason="hello24")
|
|
|
|
def test_skip():
|
|
|
|
assert True
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(skips=1)
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(
|
|
|
|
file="test_mark_skip_contains_name_reason.py",
|
|
|
|
line="1",
|
|
|
|
classname="test_mark_skip_contains_name_reason",
|
|
|
|
name="test_skip")
|
|
|
|
snode = tnode.find_first_by_tag("skipped")
|
|
|
|
snode.assert_attr(type="pytest.skip", message="hello24", )
|
|
|
|
|
|
|
|
def test_mark_skipif_contains_name_reason(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
GLOBAL_CONDITION = True
|
|
|
|
@pytest.mark.skipif(GLOBAL_CONDITION, reason="hello25")
|
|
|
|
def test_skip():
|
|
|
|
assert True
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(skips=1)
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(
|
|
|
|
file="test_mark_skipif_contains_name_reason.py",
|
|
|
|
line="2",
|
|
|
|
classname="test_mark_skipif_contains_name_reason",
|
|
|
|
name="test_skip")
|
|
|
|
snode = tnode.find_first_by_tag("skipped")
|
|
|
|
snode.assert_attr(type="pytest.skip", message="hello25", )
|
|
|
|
|
2016-11-27 01:47:26 +08:00
|
|
|
def test_mark_skip_doesnt_capture_output(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.skip(reason="foo")
|
|
|
|
def test_skip():
|
|
|
|
print("bar!")
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret == 0
|
|
|
|
node_xml = dom.find_first_by_tag("testsuite").toxml()
|
|
|
|
assert "bar!" not in node_xml
|
|
|
|
|
2010-01-12 08:35:50 +08:00
|
|
|
def test_classname_instance(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2010-01-12 08:35:50 +08:00
|
|
|
def test_method(self):
|
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
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")
|
2010-01-12 08:35:50 +08:00
|
|
|
|
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
|
2015-10-10 06:40:50 +08:00
|
|
|
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")
|
2010-11-06 16:58:04 +08:00
|
|
|
|
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
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2016-08-08 20:29:20 +08:00
|
|
|
node.assert_attr(errors=1, tests=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
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")
|
2009-12-31 18:25:07 +08:00
|
|
|
assert "Division" in fnode.toxml()
|
|
|
|
|
2018-01-26 16:28:23 +08:00
|
|
|
@pytest.mark.parametrize('junit_logging', ['no', 'system-out', 'system-err'])
|
|
|
|
def test_failure_function(self, testdir, junit_logging):
|
2011-07-13 05:09:03 +08:00
|
|
|
testdir.makepyfile("""
|
2018-01-26 16:28:23 +08:00
|
|
|
import logging
|
2011-07-13 05:09:03 +08:00
|
|
|
import sys
|
2018-01-26 16:28:23 +08:00
|
|
|
|
2011-07-13 05:09:03 +08:00
|
|
|
def test_fail():
|
|
|
|
print ("hello-stdout")
|
|
|
|
sys.stderr.write("hello-stderr\\n")
|
2018-01-26 16:28:23 +08:00
|
|
|
logging.info('info msg')
|
|
|
|
logging.warning('warning msg')
|
2011-07-13 05:09:03 +08:00
|
|
|
raise ValueError(42)
|
|
|
|
""")
|
2012-06-23 17:32:32 +08:00
|
|
|
|
2018-01-26 16:28:23 +08:00
|
|
|
result, dom = runandparse(testdir, '-o', 'junit_logging=%s' % junit_logging)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
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",
|
2018-01-26 16:28:23 +08:00
|
|
|
line="3",
|
2015-10-10 06:40:50 +08:00
|
|
|
classname="test_failure_function",
|
|
|
|
name="test_fail")
|
|
|
|
fnode = tnode.find_first_by_tag("failure")
|
|
|
|
fnode.assert_attr(message="ValueError: 42")
|
2009-12-31 18:25:07 +08:00
|
|
|
assert "ValueError" in fnode.toxml()
|
2015-10-10 06:40:50 +08:00
|
|
|
systemout = fnode.next_siebling
|
|
|
|
assert systemout.tag == "system-out"
|
2011-07-13 05:09:03 +08:00
|
|
|
assert "hello-stdout" in systemout.toxml()
|
2018-01-26 16:28:23 +08:00
|
|
|
assert "info msg" not in systemout.toxml()
|
2015-10-10 06:40:50 +08:00
|
|
|
systemerr = systemout.next_siebling
|
|
|
|
assert systemerr.tag == "system-err"
|
2011-07-13 05:09:03 +08:00
|
|
|
assert "hello-stderr" in systemerr.toxml()
|
2018-01-26 16:28:23 +08:00
|
|
|
assert "info msg" not in systemerr.toxml()
|
|
|
|
|
|
|
|
if junit_logging == 'system-out':
|
|
|
|
assert "warning msg" in systemout.toxml()
|
|
|
|
assert "warning msg" not in systemerr.toxml()
|
|
|
|
elif junit_logging == 'system-err':
|
|
|
|
assert "warning msg" not in systemout.toxml()
|
|
|
|
assert "warning msg" in systemerr.toxml()
|
|
|
|
elif junit_logging == 'no':
|
|
|
|
assert "warning msg" not in systemout.toxml()
|
|
|
|
assert "warning msg" not in systemerr.toxml()
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2015-01-20 06:45:26 +08:00
|
|
|
def test_failure_verbose_message(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import sys
|
|
|
|
def test_fail():
|
|
|
|
assert 0, "An error"
|
|
|
|
""")
|
|
|
|
|
|
|
|
result, dom = runandparse(testdir)
|
2015-10-10 06:40:50 +08:00
|
|
|
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")
|
2015-01-20 06:45:26 +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
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(failures=3, tests=3)
|
2012-08-17 22:08:08 +08:00
|
|
|
|
|
|
|
for index, char in enumerate("<&'"):
|
2012-10-19 16:53:28 +08:00
|
|
|
|
2015-10-10 06:40:50 +08:00
|
|
|
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
|
2012-08-17 22:08:08 +08:00
|
|
|
assert text == '%s\n' % char
|
|
|
|
|
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
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestHello(object):
|
2010-06-09 22:18:47 +08:00
|
|
|
def test_hello(self):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir, "--junitprefix=xyz")
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
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")
|
2010-06-09 22:18:47 +08:00
|
|
|
|
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
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2016-05-05 04:36:27 +08:00
|
|
|
node.assert_attr(skips=1, tests=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
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")
|
2015-09-30 04:39:18 +08:00
|
|
|
# assert "ValueError" in fnode.toxml()
|
2010-05-20 20:35:13 +08:00
|
|
|
|
|
|
|
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)
|
2015-09-30 04:39:18 +08:00
|
|
|
# assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2016-08-18 05:02:54 +08:00
|
|
|
node.assert_attr(skips=0, tests=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(
|
|
|
|
file="test_xfailure_xpass.py",
|
|
|
|
line="1",
|
|
|
|
classname="test_xfailure_xpass",
|
|
|
|
name="test_xpass")
|
2016-08-18 05:02:54 +08:00
|
|
|
|
|
|
|
def test_xfailure_xpass_strict(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.xfail(strict=True, reason="This needs to fail!")
|
|
|
|
def test_xpass():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
# assert result.ret
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(skips=0, tests=1)
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(
|
|
|
|
file="test_xfailure_xpass_strict.py",
|
|
|
|
line="1",
|
|
|
|
classname="test_xfailure_xpass_strict",
|
|
|
|
name="test_xpass")
|
|
|
|
fnode = tnode.find_first_by_tag("failure")
|
|
|
|
fnode.assert_attr(message="[XPASS(strict)] This needs to fail!")
|
2010-05-20 20:35:13 +08:00
|
|
|
|
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
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2016-08-08 20:29:20 +08:00
|
|
|
node.assert_attr(errors=1, tests=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
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")
|
2010-04-28 21:24:38 +08:00
|
|
|
assert "SyntaxError" in fnode.toxml()
|
2009-12-31 18:25:07 +08:00
|
|
|
|
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
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = dom.find_first_by_tag("testcase")
|
|
|
|
fnode = tnode.find_first_by_tag("failure")
|
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-12-13 17:28:23 +08:00
|
|
|
def test_assertion_binchars(self, testdir):
|
|
|
|
"""this test did fail when the escaping wasnt strict"""
|
|
|
|
testdir.makepyfile("""
|
|
|
|
|
|
|
|
M1 = '\x01\x02\x03\x04'
|
|
|
|
M2 = '\x01\x02\x03\x05'
|
|
|
|
|
|
|
|
def test_str_compare():
|
|
|
|
assert M1 == M2
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
2013-12-16 18:51:04 +08:00
|
|
|
print(dom.toxml())
|
2013-12-13 17:28:23 +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)
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
pnode = node.find_first_by_tag("testcase")
|
|
|
|
systemout = pnode.find_first_by_tag("system-out")
|
2013-04-16 12:45:14 +08:00
|
|
|
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)
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
pnode = node.find_first_by_tag("testcase")
|
|
|
|
systemout = pnode.find_first_by_tag("system-err")
|
2013-04-16 12:45:14 +08:00
|
|
|
assert "hello-stderr" in systemout.toxml()
|
|
|
|
|
2016-01-21 01:13:01 +08:00
|
|
|
def test_setup_error_captures_stdout(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg(request):
|
2016-01-21 01:13:01 +08:00
|
|
|
print('hello-stdout')
|
|
|
|
raise ValueError()
|
|
|
|
def test_function(arg):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
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_setup_error_captures_stderr(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import sys
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg(request):
|
2016-01-21 01:13:01 +08:00
|
|
|
sys.stderr.write('hello-stderr')
|
|
|
|
raise ValueError()
|
|
|
|
def test_function(arg):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
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()
|
|
|
|
|
2017-02-22 21:17:45 +08:00
|
|
|
def test_avoid_double_stdout(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg(request):
|
|
|
|
yield
|
|
|
|
sys.stdout.write('hello-stdout teardown')
|
|
|
|
raise ValueError()
|
|
|
|
def test_function(arg):
|
|
|
|
sys.stdout.write('hello-stdout call')
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
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 call" in systemout.toxml()
|
|
|
|
assert "hello-stdout teardown" in systemout.toxml()
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-03-01 23:49:55 +08:00
|
|
|
def test_mangle_test_address():
|
|
|
|
from _pytest.junitxml import mangle_test_address
|
|
|
|
address = '::'.join(
|
|
|
|
["a/my.py.thing.py", "Class", "()", "method", "[a-1-::]"])
|
|
|
|
newnames = mangle_test_address(address)
|
|
|
|
assert newnames == ["a.my.py.thing", "Class", "method", "[a-1-::]"]
|
2012-05-22 23:18:04 +08:00
|
|
|
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2013-03-25 03:43:25 +08:00
|
|
|
def test_dont_configure_on_slaves(tmpdir):
|
|
|
|
gotten = []
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class FakeConfig(object):
|
2013-03-25 03:43:25 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.pluginmanager = self
|
|
|
|
self.option = self
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2017-05-13 04:52:50 +08:00
|
|
|
def getini(self, name):
|
|
|
|
return "pytest"
|
2017-05-12 09:45:20 +08:00
|
|
|
|
2013-03-25 03:43:25 +08:00
|
|
|
junitprefix = None
|
2015-09-30 04:39:18 +08:00
|
|
|
# XXX: shouldnt need tmpdir ?
|
2013-03-25 03:43:25 +08:00
|
|
|
xmlpath = str(tmpdir.join('junix.xml'))
|
|
|
|
register = gotten.append
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2013-03-25 03:43:25 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestNonPython(object):
|
2009-12-31 18:25:07 +08:00
|
|
|
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
|
2015-10-10 06:40:50 +08:00
|
|
|
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")
|
2009-12-31 18:25:07 +08:00
|
|
|
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
|
|
|
|
|
2015-09-30 04:39:18 +08:00
|
|
|
|
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
|
2015-09-30 04:39:18 +08:00
|
|
|
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)
|
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
|
|
|
|
2015-09-30 04:39:18 +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
|
|
|
|
2015-09-30 04:39:18 +08:00
|
|
|
|
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()
|
|
|
|
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2015-06-17 06:36:04 +08:00
|
|
|
def test_logxml_makedir(testdir):
|
2015-06-17 11:04:25 +08:00
|
|
|
"""--junitxml should automatically create directories for the xml file"""
|
2015-06-17 06:36:04 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--junitxml=path/to/results.xml")
|
|
|
|
assert result.ret == 0
|
|
|
|
assert testdir.tmpdir.join("path/to/results.xml").check()
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-11-28 07:55:49 +08:00
|
|
|
def test_logxml_check_isdir(testdir):
|
|
|
|
"""Give an error if --junit-xml is a directory (#2089)"""
|
|
|
|
result = testdir.runpytest("--junit-xml=.")
|
|
|
|
result.stderr.fnmatch_lines(["*--junitxml must be a filename*"])
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-05-10 03:16:57 +08:00
|
|
|
def test_escaped_parametrized_names_xml(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2016-04-02 10:45:44 +08:00
|
|
|
@pytest.mark.parametrize('char', [u"\\x00"])
|
2013-05-10 03:16:57 +08:00
|
|
|
def test_func(char):
|
|
|
|
assert char
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret == 0
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testcase")
|
2016-04-02 05:57:42 +08:00
|
|
|
node.assert_attr(name="test_func[\\x00]")
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2013-05-10 03:16:57 +08:00
|
|
|
|
2016-03-03 17:12:56 +08:00
|
|
|
def test_double_colon_split_function_issue469(testdir):
|
2016-03-01 23:49:55 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('param', ["double::colon"])
|
|
|
|
def test_func(param):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testcase")
|
2016-03-03 17:12:56 +08:00
|
|
|
node.assert_attr(classname="test_double_colon_split_function_issue469")
|
|
|
|
node.assert_attr(name='test_func[double::colon]')
|
|
|
|
|
|
|
|
|
|
|
|
def test_double_colon_split_method_issue469(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2016-03-03 17:12:56 +08:00
|
|
|
@pytest.mark.parametrize('param', ["double::colon"])
|
|
|
|
def test_func(self, param):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testcase")
|
|
|
|
node.assert_attr(
|
|
|
|
classname="test_double_colon_split_method_issue469.TestClass")
|
2016-03-01 23:49:55 +08:00
|
|
|
node.assert_attr(name='test_func[double::colon]')
|
|
|
|
|
|
|
|
|
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")
|
2014-03-14 19:49:34 +08:00
|
|
|
from _pytest.runner import BaseReport
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2014-03-14 19:49:34 +08:00
|
|
|
class Report(BaseReport):
|
2013-10-22 17:26:29 +08:00
|
|
|
longrepr = ustr
|
2013-10-21 22:54:25 +08:00
|
|
|
sections = []
|
|
|
|
nodeid = "something"
|
2015-07-01 07:30:20 +08:00
|
|
|
location = 'tests/filename.py', 42, 'TestClass.method'
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2015-10-11 01:24:21 +08:00
|
|
|
test_report = Report()
|
2013-10-21 22:54:25 +08:00
|
|
|
|
|
|
|
# hopefully this is not too brittle ...
|
|
|
|
log.pytest_sessionstart()
|
2015-10-11 01:24:21 +08:00
|
|
|
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)
|
2013-10-21 22:54:25 +08:00
|
|
|
log.pytest_sessionfinish()
|
|
|
|
|
2015-09-26 14:11:23 +08:00
|
|
|
|
2015-08-20 05:36:42 +08:00
|
|
|
def test_record_property(testdir):
|
|
|
|
testdir.makepyfile("""
|
2015-10-10 14:19:41 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
2017-08-16 19:23:28 +08:00
|
|
|
def other(record_property):
|
|
|
|
record_property("bar", 1)
|
|
|
|
def test_record(record_property, other):
|
|
|
|
record_property("foo", "<1");
|
2015-08-20 05:36:42 +08:00
|
|
|
""")
|
2015-08-23 22:45:39 +08:00
|
|
|
result, dom = runandparse(testdir, '-rw')
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
psnode = tnode.find_first_by_tag('properties')
|
2015-10-10 20:39:25 +08:00
|
|
|
pnodes = psnode.find_by_tag('property')
|
|
|
|
pnodes[0].assert_attr(name="bar", value="1")
|
|
|
|
pnodes[1].assert_attr(name="foo", value="<1")
|
2017-03-08 07:47:43 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
'test_record_property.py::test_record',
|
|
|
|
])
|
2015-09-26 14:11:23 +08:00
|
|
|
|
|
|
|
|
2016-02-29 22:39:37 +08:00
|
|
|
def test_record_property_same_name(testdir):
|
|
|
|
testdir.makepyfile("""
|
2017-08-16 19:23:28 +08:00
|
|
|
def test_record_with_same_name(record_property):
|
|
|
|
record_property("foo", "bar")
|
|
|
|
record_property("foo", "baz")
|
2016-02-29 22:39:37 +08:00
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir, '-rw')
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
psnode = tnode.find_first_by_tag('properties')
|
|
|
|
pnodes = psnode.find_by_tag('property')
|
|
|
|
pnodes[0].assert_attr(name="foo", value="bar")
|
|
|
|
pnodes[1].assert_attr(name="foo", value="baz")
|
|
|
|
|
|
|
|
|
2018-01-19 08:06:42 +08:00
|
|
|
def test_record_attribute(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def other(record_xml_attribute):
|
|
|
|
record_xml_attribute("bar", 1)
|
|
|
|
def test_record(record_xml_attribute, other):
|
|
|
|
record_xml_attribute("foo", "<1");
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir, '-rw')
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(bar="1")
|
|
|
|
tnode.assert_attr(foo="<1")
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
'test_record_attribute.py::test_record',
|
|
|
|
'*record_xml_attribute*experimental*',
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2015-09-26 14:11:23 +08:00
|
|
|
def test_random_report_log_xdist(testdir):
|
|
|
|
"""xdist calls pytest_runtest_logreport as they are executed by the slaves,
|
|
|
|
with nodes from several nodes overlapping, so junitxml must cope with that
|
|
|
|
to produce correct reports. #1064
|
|
|
|
"""
|
|
|
|
pytest.importorskip('xdist')
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest, time
|
|
|
|
@pytest.mark.parametrize('i', list(range(30)))
|
|
|
|
def test_x(i):
|
|
|
|
assert i != 22
|
|
|
|
""")
|
|
|
|
_, dom = runandparse(testdir, '-n2')
|
2015-10-10 06:40:50 +08:00
|
|
|
suite_node = dom.find_first_by_tag("testsuite")
|
2015-09-26 14:11:23 +08:00
|
|
|
failed = []
|
2015-10-10 06:40:50 +08:00
|
|
|
for case_node in suite_node.find_by_tag("testcase"):
|
|
|
|
if case_node.find_first_by_tag('failure'):
|
|
|
|
failed.append(case_node['name'])
|
2015-09-26 14:11:23 +08:00
|
|
|
|
|
|
|
assert failed == ['test_x[22]']
|
2015-11-25 04:24:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_runs_twice(testdir):
|
|
|
|
f = testdir.makepyfile('''
|
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
''')
|
|
|
|
|
2015-12-18 05:27:01 +08:00
|
|
|
result, dom = runandparse(testdir, f, f)
|
2015-12-17 02:07:36 +08:00
|
|
|
assert 'INTERNALERROR' not in result.stdout.str()
|
2015-12-18 05:27:01 +08:00
|
|
|
first, second = [x['classname'] for x in dom.find_by_tag("testcase")]
|
|
|
|
assert first == second
|
2015-12-06 20:21:52 +08:00
|
|
|
|
|
|
|
|
2015-12-17 02:07:36 +08:00
|
|
|
@pytest.mark.xfail(reason='hangs', run=False)
|
2015-12-06 20:21:52 +08:00
|
|
|
def test_runs_twice_xdist(testdir):
|
|
|
|
pytest.importorskip('xdist')
|
|
|
|
f = testdir.makepyfile('''
|
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
''')
|
|
|
|
|
2015-12-18 05:27:01 +08:00
|
|
|
result, dom = runandparse(
|
|
|
|
testdir, f,
|
|
|
|
'--dist', 'each', '--tx', '2*popen',)
|
2015-12-17 02:07:36 +08:00
|
|
|
assert 'INTERNALERROR' not in result.stdout.str()
|
2015-12-18 05:27:01 +08:00
|
|
|
first, second = [x['classname'] for x in dom.find_by_tag("testcase")]
|
|
|
|
assert first == second
|
2015-12-17 02:07:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_fancy_items_regression(testdir):
|
2015-12-18 05:27:01 +08:00
|
|
|
# issue 1259
|
2015-12-17 02:07:36 +08:00
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
class FunItem(pytest.Item):
|
|
|
|
def runtest(self):
|
|
|
|
pass
|
|
|
|
class NoFunItem(pytest.Item):
|
|
|
|
def runtest(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class FunCollector(pytest.File):
|
|
|
|
def collect(self):
|
|
|
|
return [
|
|
|
|
FunItem('a', self),
|
|
|
|
NoFunItem('a', self),
|
2015-12-18 05:30:27 +08:00
|
|
|
NoFunItem('b', self),
|
2015-12-17 02:07:36 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
def pytest_collect_file(path, parent):
|
2015-12-18 05:27:01 +08:00
|
|
|
if path.check(ext='.py'):
|
|
|
|
return FunCollector(path, parent)
|
2015-12-17 02:07:36 +08:00
|
|
|
""")
|
|
|
|
|
|
|
|
testdir.makepyfile('''
|
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
''')
|
|
|
|
|
2015-12-18 05:27:01 +08:00
|
|
|
result, dom = runandparse(testdir)
|
2015-12-17 02:07:36 +08:00
|
|
|
|
|
|
|
assert 'INTERNALERROR' not in result.stdout.str()
|
2015-12-18 05:27:01 +08:00
|
|
|
|
|
|
|
items = sorted(
|
|
|
|
'%(classname)s %(name)s %(file)s' % x
|
|
|
|
|
|
|
|
for x in dom.find_by_tag("testcase"))
|
|
|
|
import pprint
|
|
|
|
pprint.pprint(items)
|
|
|
|
assert items == [
|
|
|
|
u'conftest a conftest.py',
|
|
|
|
u'conftest a conftest.py',
|
2015-12-18 05:30:27 +08:00
|
|
|
u'conftest b conftest.py',
|
2015-12-18 05:27:01 +08:00
|
|
|
u'test_fancy_items_regression a test_fancy_items_regression.py',
|
|
|
|
u'test_fancy_items_regression a test_fancy_items_regression.py',
|
2015-12-18 05:30:27 +08:00
|
|
|
u'test_fancy_items_regression b test_fancy_items_regression.py',
|
2015-12-18 05:27:01 +08:00
|
|
|
u'test_fancy_items_regression test_pass'
|
|
|
|
u' test_fancy_items_regression.py',
|
|
|
|
]
|
2016-02-29 22:14:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_global_properties(testdir):
|
|
|
|
path = testdir.tmpdir.join("test_global_properties.xml")
|
|
|
|
log = LogXML(str(path), None)
|
|
|
|
from _pytest.runner import BaseReport
|
|
|
|
|
|
|
|
class Report(BaseReport):
|
|
|
|
sections = []
|
|
|
|
nodeid = "test_node_id"
|
|
|
|
|
|
|
|
log.pytest_sessionstart()
|
|
|
|
log.add_global_property('foo', 1)
|
|
|
|
log.add_global_property('bar', 2)
|
|
|
|
log.pytest_sessionfinish()
|
|
|
|
|
|
|
|
dom = minidom.parse(str(path))
|
|
|
|
|
|
|
|
properties = dom.getElementsByTagName('properties')
|
|
|
|
|
|
|
|
assert (properties.length == 1), "There must be one <properties> node"
|
|
|
|
|
|
|
|
property_list = dom.getElementsByTagName('property')
|
|
|
|
|
|
|
|
assert (property_list.length == 2), "There most be only 2 property nodes"
|
|
|
|
|
|
|
|
expected = {'foo': '1', 'bar': '2'}
|
|
|
|
actual = {}
|
|
|
|
|
|
|
|
for p in property_list:
|
|
|
|
k = str(p.getAttribute('name'))
|
|
|
|
v = str(p.getAttribute('value'))
|
|
|
|
actual[k] = v
|
|
|
|
|
|
|
|
assert actual == expected
|
2016-08-26 04:08:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_url_property(testdir):
|
|
|
|
test_url = "http://www.github.com/pytest-dev"
|
|
|
|
path = testdir.tmpdir.join("test_url_property.xml")
|
|
|
|
log = LogXML(str(path), None)
|
|
|
|
from _pytest.runner import BaseReport
|
|
|
|
|
|
|
|
class Report(BaseReport):
|
|
|
|
longrepr = "FooBarBaz"
|
|
|
|
sections = []
|
|
|
|
nodeid = "something"
|
|
|
|
location = 'tests/filename.py', 42, 'TestClass.method'
|
|
|
|
url = test_url
|
|
|
|
|
|
|
|
test_report = Report()
|
|
|
|
|
|
|
|
log.pytest_sessionstart()
|
|
|
|
node_reporter = log._opentestcase(test_report)
|
|
|
|
node_reporter.append_failure(test_report)
|
|
|
|
log.pytest_sessionfinish()
|
|
|
|
|
|
|
|
test_case = minidom.parse(str(path)).getElementsByTagName('testcase')[0]
|
|
|
|
|
2017-03-08 07:47:43 +08:00
|
|
|
assert (test_case.getAttribute('url') == test_url), "The URL did not get written to the xml"
|
2017-02-23 04:31:30 +08:00
|
|
|
|
|
|
|
|
2017-05-13 04:52:50 +08:00
|
|
|
@pytest.mark.parametrize('suite_name', ['my_suite', ''])
|
|
|
|
def test_set_suite_name(testdir, suite_name):
|
|
|
|
if suite_name:
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
junit_suite_name={0}
|
|
|
|
""".format(suite_name))
|
|
|
|
expected = suite_name
|
|
|
|
else:
|
|
|
|
expected = 'pytest'
|
2017-02-23 04:31:30 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result, dom = runandparse(testdir)
|
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2017-05-13 04:52:50 +08:00
|
|
|
node.assert_attr(name=expected)
|