~free.ekanayaka/storm/any-expr

« back to all changes in this revision

Viewing changes to tests/conftest.py

  • Committer: Gustavo Niemeyer
  • Date: 2006-05-13 04:42:42 UTC
  • Revision ID: gustavo@niemeyer.net-20060513044242-39457d29f0849be3
Adding initial infrastructure.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Machinary to make py.test interpret standard unittest.TestCase classes.
 
3
"""
 
4
from unittest import TestCase, TestResult
 
5
import doctest
 
6
import sys
 
7
 
 
8
import py.test.collect
 
9
import py.test.compat
 
10
import py.test
 
11
 
 
12
from landscape.lib.security import safe
 
13
 
 
14
 
 
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])
 
24
        if not msg:
 
25
            if "expr" in locals and "msg" in locals:
 
26
                msg = repr(locals["expr"])
 
27
            else:
 
28
                msg = "!?"
 
29
        raise py.test.Item.Failed, py.test.Item.Failed(msg=msg), exc_info[2]
 
30
    addError = addFailure
 
31
 
 
32
class PyTestCase(TestCase):
 
33
    def __init__(self, methodName="setUp"):
 
34
        super(PyTestCase, self).__init__(methodName)
 
35
 
 
36
    class Function(py.test.Function):
 
37
        def execute(self, target, *args):
 
38
            __tracebackhide__ = True
 
39
            self = target.im_self
 
40
            self.__init__(target.__name__)
 
41
            self.run(PyTestResult())
 
42
 
 
43
class PyDocTest(py.test.collect.Module):
 
44
    def __init__(self, fspath, parent=None):
 
45
        super(PyDocTest, self).__init__(fspath.basename, parent)
 
46
        self.fspath = fspath
 
47
        self._obj = None
 
48
 
 
49
    def run(self):
 
50
        return [self.name]
 
51
 
 
52
    def join(self, name):
 
53
        return self.Function(name, parent=self, obj=self.fspath)
 
54
 
 
55
    class Function(py.test.Function):
 
56
        def getpathlineno(self):
 
57
            code = py.code.Code(self.failed)
 
58
            return code.path, code.firstlineno
 
59
 
 
60
        def failed(self, msg):
 
61
            raise self.Failed(msg)
 
62
 
 
63
        def execute(self, fspath):
 
64
            failures, total = doctest.testfile(str(fspath),
 
65
                                               module_relative=False,
 
66
                                               optionflags=doctest.ELLIPSIS)
 
67
            if failures:
 
68
                __tracebackhide__ = True
 
69
                self.failed("%d doctest cases" % failures)
 
70
 
 
71
class UnitTestModule(py.test.collect.Module):
 
72
    def buildname2items(self):
 
73
        d = {}
 
74
        for name in dir(self.obj):
 
75
            testclass = None
 
76
            obj = getattr(self.obj, name)
 
77
 
 
78
            try:
 
79
                if issubclass(obj, (TestCase, PyTestCase)):
 
80
                    testclass = obj
 
81
            except TypeError:
 
82
                pass
 
83
 
 
84
            if testclass:
 
85
                d[name] = self.Class(name, parent=self)
 
86
                if not issubclass(testclass, PyTestCase):
 
87
                    queue = [testclass]
 
88
                    while queue:
 
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)
 
94
                            break
 
95
                        queue.extend(testclass.__bases__)
 
96
        return d
 
97
 
 
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
 
102
            safe.install()
 
103
        super(UnitTestDirectory, self).__init__(*args, **kwargs)
 
104
 
 
105
    def filefilter(self, path):
 
106
        return path.check(fnmatch="*.py") and path.basename != "conftest.py"
 
107
 
 
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)
 
114
 
 
115
Module = UnitTestModule
 
116
Directory = UnitTestDirectory