This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
/
mocktest.py
288 lines (253 loc) · 8.4 KB
/
mocktest.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
Test Infrastructure
-------------------
"""
__all__ = (
'TestCase',
'pending',
'ignore',
'Skeleton',
)
import unittest
import re
import sys
import core
from mocking import _special_method
import types
from functools import wraps
__unittest = True
# set the SkipTest exception class
try:
from unittest import SkipTest
except ImportError:
SkipTest = None
def subclass_only(parent, method_names, safe_superclasses=()):
def filter_parent_class(cls):
if cls in safe_superclasses:
return cls
return subclass_only(cls, method_names, safe_superclasses)
filtered_parents = tuple(map(filter_parent_class, parent.__bases__))
cls = type("%s::Skeleton" % (parent.__name__,), filtered_parents, {})
safe_method = lambda name: name in method_names or name.startswith('_')
def copy_attr(name):
attr = getattr(parent, name)
if _special_method(name): return
if isinstance(attr, types.MethodType):
if not safe_method(name):
return
# make a copy of the method that's tied to the destination class instead of the source
attr = types.MethodType(attr.im_func, None, cls)
setattr(cls, name, attr)
map(copy_attr, dir(parent))
return cls
def Skeleton(cls):
"""
Generate a subclass inheriting only private methods and setUp/tearDown, for the purpose
of inheriting test setup but not any actual test implementations
"""
import mocktest
return subclass_only(cls, ('setUp', 'tearDown'), safe_superclasses=(unittest.TestCase, object, mocktest.TestCase))
def _compose(hook, func):
if hook is None:
return func
if func is None:
return hook
def run_hook():
hook()
func()
run_hook.__name__ = func.__name__
return run_hook
class ParamDecorator(object):
## use to decorate a decorator, allowing it to optionally take arguments
def __init__(self, decorator_function):
self.func = decorator_function
def __call__(self, *args, **kwargs):
if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
# we're being called without paramaters
# (just the decorated function)
return self.func(args[0])
return self.decorate_with(args, kwargs)
def decorate_with(self, args, kwargs):
def decorator(callable_):
return self.func(callable_, *args, **kwargs)
return decorator
@ParamDecorator
def pending(function, reason = None):
"""
A decorator for pending tests.
Note that pending tests are always run, and will cause a failure if they succeed (a pending test is assumed to be in a failing state.
To not run a test at all. use :py:func:mocktest.ignore:
"""
reason_str = "" if reason is None else " (%s)" % reason
@wraps(function)
def actually_call_it(*args, **kwargs):
fn_name = function.__name__
success = False
try:
function(*args, **kwargs)
success = True
except StandardError:
if reason_str:
print >> sys.stderr, "[[[ PENDING ]]]%s ... " % (reason_str,)
if SkipTest is not None:
raise SkipTest(reason_str)
if success:
raise AssertionError, "%s%s PASSED unexpectedly " % (fn_name, reason_str)
return actually_call_it
@ParamDecorator
def ignore(function, reason = None):
"""a decorator for tests that should not be run"""
reason_str = "" if reason is None else " (%s)" % reason
@wraps(function)
def actually_call_it(*args, **kwargs):
print >> sys.stderr, "[[[ IGNORED ]]]%s ... " % (reason_str,)
return actually_call_it
class TestCase(unittest.TestCase):
"""
A subclass of unittest.TestCase with the following additions:
- Automatically calls MockTransaction.__enter__ and __exit__ in \
order to reset mock state and verify expectations upon test \
completion.
- enhanced versions of assertTrue / False, assertRaises
"""
pending = globals()['pending']
ignore = globals()['ignore']
def __init__(self, methodName = 'runTest'):
super(TestCase, self).__init__(methodName)
try:
subclass_setup = self.setUp
except AttributeError:
subclass_setup = None
try:
subclass_teardown = self.tearDown
except AttributeError:
subclass_teardown = None
self.setUp = _compose(self.__setup, subclass_setup)
self.tearDown = _compose(self.__teardown, subclass_teardown)
def __setup(self):
core._setup()
def __teardown(self):
core._teardown()
def assert_(self, expr, desc = None):
if desc is None:
desc = "expected (%r) to be True" % (expr,)
super(TestCase, self).assert_(expr, desc)
assertTrue = assert_
failUnless = assert_
def assertFalse(self, expr, desc = None):
if desc is None:
desc = "Expected (%r) to be False" % (expr,)
super(TestCase, self).assertFalse(expr, desc)
def assertEqual(self, a, b, desc = None):
if a == b:
return
if desc is not None:
return super(TestCase, self).assertEqual(a, b, desc)
if (isinstance(a, list) and isinstance(b, list)) or (isinstance(a, tuple) and isinstance(b, tuple)):
self.__assertEqual_array(a, b)
elif isinstance(a, dict) and isinstance(b, dict):
self.__assertEqual_dict(a, b)
super(TestCase, self).assertEqual(a, b, desc)
assertEquals = assertEqual
def __assertEqual_dict(self, a, b):
akeys, bkeys = sorted(a.keys()), sorted(b.keys())
if not akeys == bkeys:
raise AssertionError("%r != %r\ndict keys differ: %r != %r" % (a, b, akeys, bkeys))
a_and_not_b = self.__dict_differences_btwn(a, b)
b_and_not_a = self.__dict_differences_btwn(b, a)
raise AssertionError("%r != %r\ndifference between dicts: %r vs %r" % (a, b, a_and_not_b, b_and_not_a))
def __assertEqual_array(self, a, b):
la, lb = len(a), len(b)
longest = max(la, lb)
def _raise(index, adesc, bdesc):
raise AssertionError("%r != %r\nlists differ at index %s:\n\t%s != %s" % (a, b, index, adesc, bdesc))
if la != lb:
# length mismatch
nomore = '(no more values)'
if la < lb:
index = la
a_desc = nomore
b_desc = b[index]
else:
index=lb
a_desc = a[index]
b_desc = nomore
_raise(index, a_desc, b_desc)
for i in range(0, longest):
same = False
if a[i] != b[i]:
_raise(i, repr(a[i]), repr(b[i]))
akeys, bkeys = sorted(a.keys()), sorted(b.keys())
if not akeys == bkeys:
raise AssertionError("%r != %r\ndict keys differ: %r != %r" % (a, b, akeys, bkeys))
a_and_not_b = self.__dict_differences_btwn(a, b)
b_and_not_a = self.__dict_differences_btwn(b, a)
def __dict_differences_btwn(self, a, b):
in_a_not_b = {}
for k,v in a.items():
same = False
try:
same = b[k] == v
except KeyError:
pass
if not same:
in_a_not_b[k] = v
return in_a_not_b
def assertRaises(self, exception, func, message = None, args = None, kwargs = None, matching=None):
"""
Enhanced assertRaises, able to:
- check arguments (args)
- check keyword arguments (kwargs)
- match a regular expression on the resulting expression message (matching)
- compare message strings (message)
"""
callsig = "%s()" % (func.__name__,)
try:
func()
except exception, exc:
if args is not None:
self.failIf(exc.args != args,
"%s raised %s with unexpected args: "\
"expected=%r, actual=%r"\
% (callsig, exc.__class__, args, exc.args))
if kwargs is not None:
self.failIf(exc.kwargs != kwargs,
"%s raised %s with unexpected keyword args: "\
"expected=%r, actual=%r"\
% (callsig, exc.__class__, kwargs, exc.kwargs))
if matching is not None:
pattern = re.compile(matching)
self.failUnless(pattern.search(str(exc)),
"%s raised %s, but the exception "\
"does not match '%s': %r"\
% (callsig, exc.__class__, matching, str(exc)))
if message is not None:
self.failUnless(str(exc) == message,
"%s raised %s, but the exception "\
"does not equal \"%s\": %r"\
% (callsig, exc.__class__, message, str(exc)))
except:
exc_info = sys.exc_info()
self.fail("%s raised an unexpected exception type: "\
"expected=%s, actual=%s (%s)"\
% (callsig, exception, exc_info[0], exc_info[1]))
else:
self.fail("%s did not raise an exception" % (callsig,))
failUnlessRaises = assertRaises
def run(self, result=None):
"""
This is (mostly) the default implementation of unittest.run
the only modification is that a `self.FailureException` raised
in the teardown method counts for a failure
"""
if result is None: result = self.defaultTestResult()
addError = result.addError
def patchedAddError(*a, **k):
if len(a) == 1 and len(a[0]) == 3:
type = a[0][0]
if issubclass(type, self.failureException):
# call it a failure instead of an error
return result.addFailure(*a, **k)
return addError(*a, **k)
result.addError = patchedAddError
return super(TestCase, self).run(result)