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
|
from unittest import TestCase
from testfixtures.mock import Mock, MagicMock, patch, DEFAULT
from testfixtures import wrap, compare, log_capture, LogCapture
class TestWrap(TestCase):
def test_wrapping(self):
m = Mock()
@wrap(m.before, m.after)
def test_function(r):
m.test()
return 'something'
compare(m.method_calls, [])
compare(test_function(), 'something')
compare(m.method_calls, [
('before', (), {}),
('test', (), {}),
('after', (), {})
])
def test_wrapping_only_before(self):
before = Mock()
@wrap(before)
def test_function():
return 'something'
self.assertFalse(before.called)
compare(test_function(), 'something')
compare(before.call_count, 1)
def test_wrapping_wants_return(self):
m = Mock()
m.before.return_value = 'something'
@wrap(m.before, m.after)
def test_function(r):
m.test(r)
return 'r:'+r
compare(m.method_calls, [])
compare(test_function(), 'r:something')
compare(m.method_calls, [
('before', (), {}),
('test', ('something', ), {}),
('after', (), {})
])
def test_wrapping_wants_arguments(self):
# This only works in python 2.5+, for
# earlier versions, you'll have to come
# up with your own `partial` class...
from functools import partial
m = Mock()
@wrap(partial(m.before, 1, x=2), partial(m.after, 3, y=4))
def test_function(r):
m.test()
return 'something'
compare(m.method_calls, [])
compare(test_function(), 'something')
compare(m.method_calls, [
('before', (1, ), {'x': 2}),
('test', (), {}),
('after', (3, ), {'y': 4})
])
def test_multiple_wrappers(self):
m = Mock()
@wrap(m.before2, m.after2)
@wrap(m.before1, m.after1)
def test_function():
m.test_function()
return 'something'
compare(m.method_calls, [])
compare(test_function(), 'something')
compare(m.method_calls, [
('before1', (), {}),
('before2', (), {}),
('test_function', (), {}),
('after2', (), {}),
('after1', (), {}),
])
def test_multiple_wrappers_wants_return(self):
m = Mock()
m.before1.return_value = 1
m.before2.return_value = 2
@wrap(m.before2, m.after2)
@wrap(m.before1, m.after1)
def test_function(r1, r2):
m.test_function(r1, r2)
return 'something'
compare(m.method_calls, [])
compare(test_function(), 'something')
compare(m.method_calls, [
('before1', (), {}),
('before2', (), {}),
('test_function', (1, 2), {}),
('after2', (), {}),
('after1', (), {}),
])
def test_multiple_wrappers_only_want_first_return(self):
m = Mock()
m.before1.return_value = 1
@wrap(m.before2, m.after2)
@wrap(m.before1, m.after1)
def test_function(r1):
m.test_function(r1)
return 'something'
compare(m.method_calls, [])
compare(test_function(), 'something')
compare(m.method_calls, [
('before1', (), {}),
('before2', (), {}),
('test_function', (1, ), {}),
('after2', (), {}),
('after1', (), {}),
])
def test_wrap_method(self):
m = Mock()
class T:
@wrap(m.before, m.after)
def method(self):
m.method()
T().method()
compare(m.method_calls, [
('before', (), {}),
('method', (), {}),
('after', (), {})
])
def test_wrap_method_wants_return(self):
m = Mock()
m.before.return_value = 'return'
class T:
@wrap(m.before, m.after)
def method(self, r):
m.method(r)
T().method()
compare(m.method_calls, [
('before', (), {}),
('method', ('return', ), {}),
('after', (), {})
])
def test_wrapping_different_functions(self):
m = Mock()
@wrap(m.before1, m.after1)
def test_function1():
m.something1()
return 'something1'
@wrap(m.before2, m.after2)
def test_function2():
m.something2()
return 'something2'
compare(m.method_calls, [])
compare(test_function1(), 'something1')
compare(m.method_calls, [
('before1', (), {}),
('something1', (), {}),
('after1', (), {})
])
compare(test_function2(), 'something2')
compare(m.method_calls, [
('before1', (), {}),
('something1', (), {}),
('after1', (), {}),
('before2', (), {}),
('something2', (), {}),
('after2', (), {})
])
def test_wrapping_local_vars(self):
m = Mock()
@wrap(m.before, m.after)
def test_function():
something = 1
m.test()
return 'something'
compare(m.method_calls, [])
compare(test_function(), 'something')
compare(m.method_calls, [
('before', (), {}),
('test', (), {}),
('after', (), {})
])
def test_wrapping__name__(self):
m = Mock()
@wrap(m.before, m.after)
def test_function():
pass # pragma: no cover
compare(test_function.__name__, 'test_function')
def test_our_wrap_dealing_with_mock_patch(self):
@patch.multiple('testfixtures.tests.sample1', X=DEFAULT)
@log_capture()
def patched(log, X):
from testfixtures.tests.sample1 import X as imported_X
assert isinstance(log, LogCapture)
assert isinstance(X, MagicMock)
assert imported_X is X
patched()
def test_patch_with_dict(self):
@patch('testfixtures.tests.sample1.X', {'x': 1})
@log_capture()
def patched(log):
assert isinstance(log, LogCapture)
from testfixtures.tests.sample1 import X
assert X == {'x': 1}
patched()
|