File: unittest.py

package info (click to toggle)
pypy 5.6.0+dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (184 lines) | stat: -rw-r--r-- 6,470 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
""" discovery and running of std-library "unittest" style tests. """
import pytest, py
import sys

# for transfering markers
from _pytest.python import transfer_markers


def is_unittest(obj):
    """Is obj a subclass of unittest.TestCase?"""
    unittest = sys.modules.get('unittest')
    if unittest is None:
        return  # nobody can have derived unittest.TestCase
    try:
        return issubclass(obj, unittest.TestCase)
    except KeyboardInterrupt:
        raise
    except:
        return False


def pytest_pycollect_makeitem(collector, name, obj):
    if is_unittest(obj):
        return UnitTestCase(name, parent=collector)


class UnitTestCase(pytest.Class):
    nofuncargs = True  # marker for fixturemanger.getfixtureinfo()
                       # to declare that our children do not support funcargs
                       #
    def setup(self):
        cls = self.obj
        if getattr(cls, '__unittest_skip__', False):
            return  # skipped
        setup = getattr(cls, 'setUpClass', None)
        if setup is not None:
            setup()
        teardown = getattr(cls, 'tearDownClass', None)
        if teardown is not None:
            self.addfinalizer(teardown)
        super(UnitTestCase, self).setup()

    def collect(self):
        self.session._fixturemanager.parsefactories(self, unittest=True)
        loader = py.std.unittest.TestLoader()
        module = self.getparent(pytest.Module).obj
        cls = self.obj
        foundsomething = False
        for name in loader.getTestCaseNames(self.obj):
            x = getattr(self.obj, name)
            funcobj = getattr(x, 'im_func', x)
            transfer_markers(funcobj, cls, module)
            yield TestCaseFunction(name, parent=self)
            foundsomething = True

        if not foundsomething:
            runtest = getattr(self.obj, 'runTest', None)
            if runtest is not None:
                ut = sys.modules.get("twisted.trial.unittest", None)
                if ut is None or runtest != ut.TestCase.runTest:
                    yield TestCaseFunction('runTest', parent=self)



class TestCaseFunction(pytest.Function):
    _excinfo = None

    def setup(self):
        self._testcase = self.parent.obj(self.name)
        self._obj = getattr(self._testcase, self.name)
        if hasattr(self._testcase, 'setup_method'):
            self._testcase.setup_method(self._obj)
        if hasattr(self, "_request"):
            self._request._fillfixtures()

    def teardown(self):
        if hasattr(self._testcase, 'teardown_method'):
            self._testcase.teardown_method(self._obj)

    def startTest(self, testcase):
        pass

    def _addexcinfo(self, rawexcinfo):
        # unwrap potential exception info (see twisted trial support below)
        rawexcinfo = getattr(rawexcinfo, '_rawexcinfo', rawexcinfo)
        try:
            excinfo = py.code.ExceptionInfo(rawexcinfo)
        except TypeError:
            try:
                try:
                    l = py.std.traceback.format_exception(*rawexcinfo)
                    l.insert(0, "NOTE: Incompatible Exception Representation, "
                        "displaying natively:\n\n")
                    pytest.fail("".join(l), pytrace=False)
                except (pytest.fail.Exception, KeyboardInterrupt):
                    raise
                except:
                    pytest.fail("ERROR: Unknown Incompatible Exception "
                        "representation:\n%r" %(rawexcinfo,), pytrace=False)
            except KeyboardInterrupt:
                raise
            except pytest.fail.Exception:
                excinfo = py.code.ExceptionInfo()
        self.__dict__.setdefault('_excinfo', []).append(excinfo)

    def addError(self, testcase, rawexcinfo):
        self._addexcinfo(rawexcinfo)
    def addFailure(self, testcase, rawexcinfo):
        self._addexcinfo(rawexcinfo)

    def addSkip(self, testcase, reason):
        try:
            pytest.skip(reason)
        except pytest.skip.Exception:
            self._addexcinfo(sys.exc_info())

    def addExpectedFailure(self, testcase, rawexcinfo, reason=""):
        try:
            pytest.xfail(str(reason))
        except pytest.xfail.Exception:
            self._addexcinfo(sys.exc_info())

    def addUnexpectedSuccess(self, testcase, reason=""):
        self._unexpectedsuccess = reason

    def addSuccess(self, testcase):
        pass

    def stopTest(self, testcase):
        pass

    def runtest(self):
        self._testcase(result=self)

    def _prunetraceback(self, excinfo):
        pytest.Function._prunetraceback(self, excinfo)
        traceback = excinfo.traceback.filter(
            lambda x:not x.frame.f_globals.get('__unittest'))
        if traceback:
            excinfo.traceback = traceback

@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call):
    if isinstance(item, TestCaseFunction):
        if item._excinfo:
            call.excinfo = item._excinfo.pop(0)
            try:
                del call.result
            except AttributeError:
                pass

# twisted trial support
def pytest_runtest_protocol(item, __multicall__):
    if isinstance(item, TestCaseFunction):
        if 'twisted.trial.unittest' in sys.modules:
            ut = sys.modules['twisted.python.failure']
            Failure__init__ = ut.Failure.__init__.im_func
            check_testcase_implements_trial_reporter()
            def excstore(self, exc_value=None, exc_type=None, exc_tb=None,
                captureVars=None):
                if exc_value is None:
                    self._rawexcinfo = sys.exc_info()
                else:
                    if exc_type is None:
                        exc_type = type(exc_value)
                    self._rawexcinfo = (exc_type, exc_value, exc_tb)
                try:
                    Failure__init__(self, exc_value, exc_type, exc_tb,
                        captureVars=captureVars)
                except TypeError:
                    Failure__init__(self, exc_value, exc_type, exc_tb)
            ut.Failure.__init__ = excstore
            try:
                return __multicall__.execute()
            finally:
                ut.Failure.__init__ = Failure__init__

def check_testcase_implements_trial_reporter(done=[]):
    if done:
        return
    from zope.interface import classImplements
    from twisted.trial.itrial import IReporter
    classImplements(TestCaseFunction, IReporter)
    done.append(1)