2
Machinary to make py.test interpret standard unittest.TestCase classes.
4
from unittest import TestCase, TestResult
12
from landscape.lib.security import safe
15
class PyTestResult(TestResult):
16
def addFailure(self, test, exc_info):
17
traceback = exc_info[2]
18
while traceback.tb_next:
19
traceback = traceback.tb_next
20
locals = traceback.tb_frame.f_locals
21
if "msg" in locals or "excClass" in locals:
22
locals["__tracebackhide__"] = True
23
msg = str(exc_info[1])
25
if "expr" in locals and "msg" in locals:
26
msg = repr(locals["expr"])
29
raise py.test.Item.Failed, py.test.Item.Failed(msg=msg), exc_info[2]
32
class PyTestCase(TestCase):
33
def __init__(self, methodName="setUp"):
34
super(PyTestCase, self).__init__(methodName)
36
class Function(py.test.Function):
37
def execute(self, target, *args):
38
__tracebackhide__ = True
40
self.__init__(target.__name__)
41
self.run(PyTestResult())
43
class PyDocTest(py.test.collect.Module):
44
def __init__(self, fspath, parent=None):
45
super(PyDocTest, self).__init__(fspath.basename, parent)
53
return self.Function(name, parent=self, obj=self.fspath)
55
class Function(py.test.Function):
56
def getpathlineno(self):
57
code = py.code.Code(self.failed)
58
return code.path, code.firstlineno
60
def failed(self, msg):
61
raise self.Failed(msg)
63
def execute(self, fspath):
64
failures, total = doctest.testfile(str(fspath),
65
module_relative=False,
66
optionflags=doctest.ELLIPSIS)
68
__tracebackhide__ = True
69
self.failed("%d doctest cases" % failures)
71
class UnitTestModule(py.test.collect.Module):
72
def buildname2items(self):
74
for name in dir(self.obj):
76
obj = getattr(self.obj, name)
79
if issubclass(obj, (TestCase, PyTestCase)):
85
d[name] = self.Class(name, parent=self)
86
if not issubclass(testclass, PyTestCase):
89
testclass = queue.pop(0)
90
if TestCase in testclass.__bases__:
91
bases = list(testclass.__bases__)
92
bases[bases.index(TestCase)] = PyTestCase
93
testclass.__bases__ = tuple(bases)
95
queue.extend(testclass.__bases__)
98
class UnitTestDirectory(py.test.collect.Directory):
99
def __init__(self, *args, **kwargs):
100
if getattr(self.__class__, "__first_run__", True):
101
self.__class__.__first_run__ = False
103
super(UnitTestDirectory, self).__init__(*args, **kwargs)
105
def filefilter(self, path):
106
return path.check(fnmatch="*.py") and path.basename != "conftest.py"
108
def makeitem(self, basename, filefilter=None, recfilter=None):
109
path = self.fspath.join(basename)
110
if path.check(fnmatch="*.txt"):
111
return PyDocTest(path, parent=self)
112
return super(UnitTestDirectory, self).makeitem(basename,
113
filefilter, recfilter)
115
Module = UnitTestModule
116
Directory = UnitTestDirectory