-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuite.py
161 lines (131 loc) · 5.21 KB
/
suite.py
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
#!/usr/bin/env python
# encoding: utf-8
"""
@version: 2.7
@author: 'john'
@time: 2017/9/24 上午2:14
@contact: [email protected]
"""
import case
class BaseTestSuite(object):
"""A simple test suite that doesn't provide class or module shared fixtures.
"""
def __init__(self, tests=()):
self._tests = []
self.addTests(tests)
def addTests(self, tests):
if isinstance(tests, basestring):
raise TypeError("tests must be an iterable of tests, not a string")
for test in tests:
self.addTest(test)
def addTest(self, test):
# sanity checks
if not hasattr(test, '__call__'):
raise TypeError("{} is not callable".format(repr(test)))
if isinstance(test, type) and issubclass(test,
(case.TestCase, TestSuite)):
raise TypeError("TestCases and TestSuites must be instantiated "
"before passing them to addTest()")
self._tests.append(test)
def __iter__(self):
return iter(self._tests)
def __call__(self, *args, **kwds):
return self.run(*args, **kwds)
# def run(self, result):
# raise NotImplementedError
class TestSuite(BaseTestSuite):
"""A test suite is a composite test consisting of a number of TestCases.
For use, create an instance of TestSuite, then add test case instances.
When all tests have been added, the suite can be passed to a test
runner, such as TextTestRunner. It will run the individual test cases
in the order in which they were added, aggregating the results. When
subclassing, do not forget to call the base class constructor.
"""
def run(self, result):
# topLevel = False
# if getattr(result, '_testRunEntered', False) is False:
# result._testRunEntered = topLevel = True
for test in self:
if result.shouldStop:
break
if _isnotsuite(test):
self._tearDownPreviousClass(test, result)
# self._handleModuleFixture(test, result)
self._handleClassSetUp(test, result)
result._previousTestClass = test.__class__
# if (getattr(test.__class__, '_classSetupFailed', False) or
# getattr(result, '_moduleSetUpFailed', False)):
# continue
if (getattr(test.__class__, '_classSetupFailed', False)):
continue
# run test
test(result)
# if topLevel:
self._tearDownPreviousClass(None, result)
# self._handleModuleTearDown(result)
# result._testRunEntered = False
return result
def _tearDownPreviousClass(self, test, result):
previousClass = getattr(result, '_previousTestClass', None)
currentClass = test.__class__
if currentClass == previousClass:
return
if getattr(previousClass, '_classSetupFailed', False):
return
# if getattr(result, '_moduleSetUpFailed', False):
# return
if getattr(previousClass, "__unittest_skip__", False):
return
tearDownClass = getattr(previousClass, 'tearDownClass', None)
if tearDownClass is not None:
# _call_if_exists(result, '_setupStdout')
try:
tearDownClass()
except Exception, e:
pass
# if isinstance(result, _DebugResult):
# raise
# className = util.strclass(previousClass)
# errorName = 'tearDownClass (%s)' % className
# self._addClassOrModuleLevelException(result, e, errorName)
finally:
pass
# _call_if_exists(result, '_restoreStdout')
def _handleClassSetUp(self, test, result):
previousClass = getattr(result, '_previousTestClass', None)
currentClass = test.__class__
if currentClass == previousClass:
return
# if result._moduleSetUpFailed:
# return
if getattr(currentClass, "__unittest_skip__", False):
return
try:
currentClass._classSetupFailed = False
except TypeError:
# test may actually be a function
# so its class will be a builtin-type
pass
setUpClass = getattr(currentClass, 'setUpClass', None)
if setUpClass is not None:
# _call_if_exists(result, '_setupStdout')
try:
setUpClass()
except Exception as e:
pass
# if isinstance(result, _DebugResult):
# raise
currentClass._classSetupFailed = True
# className = util.strclass(currentClass)
# errorName = 'setUpClass (%s)' % className
# self._addClassOrModuleLevelException(result, e, errorName)
finally:
pass
# _call_if_exists(result, '_restoreStdout')
def _isnotsuite(test):
"A crude way to tell apart testcases and suites with duck-typing"
try:
iter(test)
except TypeError:
return True
return False