Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | # Copyright (C) 2007-2012 Michael Foord & the mock team |
michael@0 | 2 | # E-mail: fuzzyman AT voidspace DOT org DOT uk |
michael@0 | 3 | # http://www.voidspace.org.uk/python/mock/ |
michael@0 | 4 | |
michael@0 | 5 | from tests.support import ( |
michael@0 | 6 | callable, unittest2, inPy3k, is_instance, next |
michael@0 | 7 | ) |
michael@0 | 8 | |
michael@0 | 9 | import copy |
michael@0 | 10 | import pickle |
michael@0 | 11 | import sys |
michael@0 | 12 | |
michael@0 | 13 | import mock |
michael@0 | 14 | from mock import ( |
michael@0 | 15 | call, DEFAULT, patch, sentinel, |
michael@0 | 16 | MagicMock, Mock, NonCallableMock, |
michael@0 | 17 | NonCallableMagicMock, _CallList, |
michael@0 | 18 | create_autospec |
michael@0 | 19 | ) |
michael@0 | 20 | |
michael@0 | 21 | |
michael@0 | 22 | try: |
michael@0 | 23 | unicode |
michael@0 | 24 | except NameError: |
michael@0 | 25 | unicode = str |
michael@0 | 26 | |
michael@0 | 27 | |
michael@0 | 28 | class Iter(object): |
michael@0 | 29 | def __init__(self): |
michael@0 | 30 | self.thing = iter(['this', 'is', 'an', 'iter']) |
michael@0 | 31 | |
michael@0 | 32 | def __iter__(self): |
michael@0 | 33 | return self |
michael@0 | 34 | |
michael@0 | 35 | def next(self): |
michael@0 | 36 | return next(self.thing) |
michael@0 | 37 | |
michael@0 | 38 | __next__ = next |
michael@0 | 39 | |
michael@0 | 40 | |
michael@0 | 41 | class Subclass(MagicMock): |
michael@0 | 42 | pass |
michael@0 | 43 | |
michael@0 | 44 | |
michael@0 | 45 | class Thing(object): |
michael@0 | 46 | attribute = 6 |
michael@0 | 47 | foo = 'bar' |
michael@0 | 48 | |
michael@0 | 49 | |
michael@0 | 50 | |
michael@0 | 51 | class MockTest(unittest2.TestCase): |
michael@0 | 52 | |
michael@0 | 53 | def test_all(self): |
michael@0 | 54 | # if __all__ is badly defined then import * will raise an error |
michael@0 | 55 | # We have to exec it because you can't import * inside a method |
michael@0 | 56 | # in Python 3 |
michael@0 | 57 | exec("from mock import *") |
michael@0 | 58 | |
michael@0 | 59 | |
michael@0 | 60 | def test_constructor(self): |
michael@0 | 61 | mock = Mock() |
michael@0 | 62 | |
michael@0 | 63 | self.assertFalse(mock.called, "called not initialised correctly") |
michael@0 | 64 | self.assertEqual(mock.call_count, 0, |
michael@0 | 65 | "call_count not initialised correctly") |
michael@0 | 66 | self.assertTrue(is_instance(mock.return_value, Mock), |
michael@0 | 67 | "return_value not initialised correctly") |
michael@0 | 68 | |
michael@0 | 69 | self.assertEqual(mock.call_args, None, |
michael@0 | 70 | "call_args not initialised correctly") |
michael@0 | 71 | self.assertEqual(mock.call_args_list, [], |
michael@0 | 72 | "call_args_list not initialised correctly") |
michael@0 | 73 | self.assertEqual(mock.method_calls, [], |
michael@0 | 74 | "method_calls not initialised correctly") |
michael@0 | 75 | |
michael@0 | 76 | # Can't use hasattr for this test as it always returns True on a mock |
michael@0 | 77 | self.assertFalse('_items' in mock.__dict__, |
michael@0 | 78 | "default mock should not have '_items' attribute") |
michael@0 | 79 | |
michael@0 | 80 | self.assertIsNone(mock._mock_parent, |
michael@0 | 81 | "parent not initialised correctly") |
michael@0 | 82 | self.assertIsNone(mock._mock_methods, |
michael@0 | 83 | "methods not initialised correctly") |
michael@0 | 84 | self.assertEqual(mock._mock_children, {}, |
michael@0 | 85 | "children not initialised incorrectly") |
michael@0 | 86 | |
michael@0 | 87 | |
michael@0 | 88 | def test_unicode_not_broken(self): |
michael@0 | 89 | # This used to raise an exception with Python 2.5 and Mock 0.4 |
michael@0 | 90 | unicode(Mock()) |
michael@0 | 91 | |
michael@0 | 92 | |
michael@0 | 93 | def test_return_value_in_constructor(self): |
michael@0 | 94 | mock = Mock(return_value=None) |
michael@0 | 95 | self.assertIsNone(mock.return_value, |
michael@0 | 96 | "return value in constructor not honoured") |
michael@0 | 97 | |
michael@0 | 98 | |
michael@0 | 99 | def test_repr(self): |
michael@0 | 100 | mock = Mock(name='foo') |
michael@0 | 101 | self.assertIn('foo', repr(mock)) |
michael@0 | 102 | self.assertIn("'%s'" % id(mock), repr(mock)) |
michael@0 | 103 | |
michael@0 | 104 | mocks = [(Mock(), 'mock'), (Mock(name='bar'), 'bar')] |
michael@0 | 105 | for mock, name in mocks: |
michael@0 | 106 | self.assertIn('%s.bar' % name, repr(mock.bar)) |
michael@0 | 107 | self.assertIn('%s.foo()' % name, repr(mock.foo())) |
michael@0 | 108 | self.assertIn('%s.foo().bing' % name, repr(mock.foo().bing)) |
michael@0 | 109 | self.assertIn('%s()' % name, repr(mock())) |
michael@0 | 110 | self.assertIn('%s()()' % name, repr(mock()())) |
michael@0 | 111 | self.assertIn('%s()().foo.bar.baz().bing' % name, |
michael@0 | 112 | repr(mock()().foo.bar.baz().bing)) |
michael@0 | 113 | |
michael@0 | 114 | |
michael@0 | 115 | def test_repr_with_spec(self): |
michael@0 | 116 | class X(object): |
michael@0 | 117 | pass |
michael@0 | 118 | |
michael@0 | 119 | mock = Mock(spec=X) |
michael@0 | 120 | self.assertIn(" spec='X' ", repr(mock)) |
michael@0 | 121 | |
michael@0 | 122 | mock = Mock(spec=X()) |
michael@0 | 123 | self.assertIn(" spec='X' ", repr(mock)) |
michael@0 | 124 | |
michael@0 | 125 | mock = Mock(spec_set=X) |
michael@0 | 126 | self.assertIn(" spec_set='X' ", repr(mock)) |
michael@0 | 127 | |
michael@0 | 128 | mock = Mock(spec_set=X()) |
michael@0 | 129 | self.assertIn(" spec_set='X' ", repr(mock)) |
michael@0 | 130 | |
michael@0 | 131 | mock = Mock(spec=X, name='foo') |
michael@0 | 132 | self.assertIn(" spec='X' ", repr(mock)) |
michael@0 | 133 | self.assertIn(" name='foo' ", repr(mock)) |
michael@0 | 134 | |
michael@0 | 135 | mock = Mock(name='foo') |
michael@0 | 136 | self.assertNotIn("spec", repr(mock)) |
michael@0 | 137 | |
michael@0 | 138 | mock = Mock() |
michael@0 | 139 | self.assertNotIn("spec", repr(mock)) |
michael@0 | 140 | |
michael@0 | 141 | mock = Mock(spec=['foo']) |
michael@0 | 142 | self.assertNotIn("spec", repr(mock)) |
michael@0 | 143 | |
michael@0 | 144 | |
michael@0 | 145 | def test_side_effect(self): |
michael@0 | 146 | mock = Mock() |
michael@0 | 147 | |
michael@0 | 148 | def effect(*args, **kwargs): |
michael@0 | 149 | raise SystemError('kablooie') |
michael@0 | 150 | |
michael@0 | 151 | mock.side_effect = effect |
michael@0 | 152 | self.assertRaises(SystemError, mock, 1, 2, fish=3) |
michael@0 | 153 | mock.assert_called_with(1, 2, fish=3) |
michael@0 | 154 | |
michael@0 | 155 | results = [1, 2, 3] |
michael@0 | 156 | def effect(): |
michael@0 | 157 | return results.pop() |
michael@0 | 158 | mock.side_effect = effect |
michael@0 | 159 | |
michael@0 | 160 | self.assertEqual([mock(), mock(), mock()], [3, 2, 1], |
michael@0 | 161 | "side effect not used correctly") |
michael@0 | 162 | |
michael@0 | 163 | mock = Mock(side_effect=sentinel.SideEffect) |
michael@0 | 164 | self.assertEqual(mock.side_effect, sentinel.SideEffect, |
michael@0 | 165 | "side effect in constructor not used") |
michael@0 | 166 | |
michael@0 | 167 | def side_effect(): |
michael@0 | 168 | return DEFAULT |
michael@0 | 169 | mock = Mock(side_effect=side_effect, return_value=sentinel.RETURN) |
michael@0 | 170 | self.assertEqual(mock(), sentinel.RETURN) |
michael@0 | 171 | |
michael@0 | 172 | |
michael@0 | 173 | @unittest2.skipUnless('java' in sys.platform, |
michael@0 | 174 | 'This test only applies to Jython') |
michael@0 | 175 | def test_java_exception_side_effect(self): |
michael@0 | 176 | import java |
michael@0 | 177 | mock = Mock(side_effect=java.lang.RuntimeException("Boom!")) |
michael@0 | 178 | |
michael@0 | 179 | # can't use assertRaises with java exceptions |
michael@0 | 180 | try: |
michael@0 | 181 | mock(1, 2, fish=3) |
michael@0 | 182 | except java.lang.RuntimeException: |
michael@0 | 183 | pass |
michael@0 | 184 | else: |
michael@0 | 185 | self.fail('java exception not raised') |
michael@0 | 186 | mock.assert_called_with(1,2, fish=3) |
michael@0 | 187 | |
michael@0 | 188 | |
michael@0 | 189 | def test_reset_mock(self): |
michael@0 | 190 | parent = Mock() |
michael@0 | 191 | spec = ["something"] |
michael@0 | 192 | mock = Mock(name="child", parent=parent, spec=spec) |
michael@0 | 193 | mock(sentinel.Something, something=sentinel.SomethingElse) |
michael@0 | 194 | something = mock.something |
michael@0 | 195 | mock.something() |
michael@0 | 196 | mock.side_effect = sentinel.SideEffect |
michael@0 | 197 | return_value = mock.return_value |
michael@0 | 198 | return_value() |
michael@0 | 199 | |
michael@0 | 200 | mock.reset_mock() |
michael@0 | 201 | |
michael@0 | 202 | self.assertEqual(mock._mock_name, "child", |
michael@0 | 203 | "name incorrectly reset") |
michael@0 | 204 | self.assertEqual(mock._mock_parent, parent, |
michael@0 | 205 | "parent incorrectly reset") |
michael@0 | 206 | self.assertEqual(mock._mock_methods, spec, |
michael@0 | 207 | "methods incorrectly reset") |
michael@0 | 208 | |
michael@0 | 209 | self.assertFalse(mock.called, "called not reset") |
michael@0 | 210 | self.assertEqual(mock.call_count, 0, "call_count not reset") |
michael@0 | 211 | self.assertEqual(mock.call_args, None, "call_args not reset") |
michael@0 | 212 | self.assertEqual(mock.call_args_list, [], "call_args_list not reset") |
michael@0 | 213 | self.assertEqual(mock.method_calls, [], |
michael@0 | 214 | "method_calls not initialised correctly: %r != %r" % |
michael@0 | 215 | (mock.method_calls, [])) |
michael@0 | 216 | self.assertEqual(mock.mock_calls, []) |
michael@0 | 217 | |
michael@0 | 218 | self.assertEqual(mock.side_effect, sentinel.SideEffect, |
michael@0 | 219 | "side_effect incorrectly reset") |
michael@0 | 220 | self.assertEqual(mock.return_value, return_value, |
michael@0 | 221 | "return_value incorrectly reset") |
michael@0 | 222 | self.assertFalse(return_value.called, "return value mock not reset") |
michael@0 | 223 | self.assertEqual(mock._mock_children, {'something': something}, |
michael@0 | 224 | "children reset incorrectly") |
michael@0 | 225 | self.assertEqual(mock.something, something, |
michael@0 | 226 | "children incorrectly cleared") |
michael@0 | 227 | self.assertFalse(mock.something.called, "child not reset") |
michael@0 | 228 | |
michael@0 | 229 | |
michael@0 | 230 | def test_reset_mock_recursion(self): |
michael@0 | 231 | mock = Mock() |
michael@0 | 232 | mock.return_value = mock |
michael@0 | 233 | |
michael@0 | 234 | # used to cause recursion |
michael@0 | 235 | mock.reset_mock() |
michael@0 | 236 | |
michael@0 | 237 | |
michael@0 | 238 | def test_call(self): |
michael@0 | 239 | mock = Mock() |
michael@0 | 240 | self.assertTrue(is_instance(mock.return_value, Mock), |
michael@0 | 241 | "Default return_value should be a Mock") |
michael@0 | 242 | |
michael@0 | 243 | result = mock() |
michael@0 | 244 | self.assertEqual(mock(), result, |
michael@0 | 245 | "different result from consecutive calls") |
michael@0 | 246 | mock.reset_mock() |
michael@0 | 247 | |
michael@0 | 248 | ret_val = mock(sentinel.Arg) |
michael@0 | 249 | self.assertTrue(mock.called, "called not set") |
michael@0 | 250 | self.assertEqual(mock.call_count, 1, "call_count incoreect") |
michael@0 | 251 | self.assertEqual(mock.call_args, ((sentinel.Arg,), {}), |
michael@0 | 252 | "call_args not set") |
michael@0 | 253 | self.assertEqual(mock.call_args_list, [((sentinel.Arg,), {})], |
michael@0 | 254 | "call_args_list not initialised correctly") |
michael@0 | 255 | |
michael@0 | 256 | mock.return_value = sentinel.ReturnValue |
michael@0 | 257 | ret_val = mock(sentinel.Arg, key=sentinel.KeyArg) |
michael@0 | 258 | self.assertEqual(ret_val, sentinel.ReturnValue, |
michael@0 | 259 | "incorrect return value") |
michael@0 | 260 | |
michael@0 | 261 | self.assertEqual(mock.call_count, 2, "call_count incorrect") |
michael@0 | 262 | self.assertEqual(mock.call_args, |
michael@0 | 263 | ((sentinel.Arg,), {'key': sentinel.KeyArg}), |
michael@0 | 264 | "call_args not set") |
michael@0 | 265 | self.assertEqual(mock.call_args_list, [ |
michael@0 | 266 | ((sentinel.Arg,), {}), |
michael@0 | 267 | ((sentinel.Arg,), {'key': sentinel.KeyArg}) |
michael@0 | 268 | ], |
michael@0 | 269 | "call_args_list not set") |
michael@0 | 270 | |
michael@0 | 271 | |
michael@0 | 272 | def test_call_args_comparison(self): |
michael@0 | 273 | mock = Mock() |
michael@0 | 274 | mock() |
michael@0 | 275 | mock(sentinel.Arg) |
michael@0 | 276 | mock(kw=sentinel.Kwarg) |
michael@0 | 277 | mock(sentinel.Arg, kw=sentinel.Kwarg) |
michael@0 | 278 | self.assertEqual(mock.call_args_list, [ |
michael@0 | 279 | (), |
michael@0 | 280 | ((sentinel.Arg,),), |
michael@0 | 281 | ({"kw": sentinel.Kwarg},), |
michael@0 | 282 | ((sentinel.Arg,), {"kw": sentinel.Kwarg}) |
michael@0 | 283 | ]) |
michael@0 | 284 | self.assertEqual(mock.call_args, |
michael@0 | 285 | ((sentinel.Arg,), {"kw": sentinel.Kwarg})) |
michael@0 | 286 | |
michael@0 | 287 | |
michael@0 | 288 | def test_assert_called_with(self): |
michael@0 | 289 | mock = Mock() |
michael@0 | 290 | mock() |
michael@0 | 291 | |
michael@0 | 292 | # Will raise an exception if it fails |
michael@0 | 293 | mock.assert_called_with() |
michael@0 | 294 | self.assertRaises(AssertionError, mock.assert_called_with, 1) |
michael@0 | 295 | |
michael@0 | 296 | mock.reset_mock() |
michael@0 | 297 | self.assertRaises(AssertionError, mock.assert_called_with) |
michael@0 | 298 | |
michael@0 | 299 | mock(1, 2, 3, a='fish', b='nothing') |
michael@0 | 300 | mock.assert_called_with(1, 2, 3, a='fish', b='nothing') |
michael@0 | 301 | |
michael@0 | 302 | |
michael@0 | 303 | def test_assert_called_once_with(self): |
michael@0 | 304 | mock = Mock() |
michael@0 | 305 | mock() |
michael@0 | 306 | |
michael@0 | 307 | # Will raise an exception if it fails |
michael@0 | 308 | mock.assert_called_once_with() |
michael@0 | 309 | |
michael@0 | 310 | mock() |
michael@0 | 311 | self.assertRaises(AssertionError, mock.assert_called_once_with) |
michael@0 | 312 | |
michael@0 | 313 | mock.reset_mock() |
michael@0 | 314 | self.assertRaises(AssertionError, mock.assert_called_once_with) |
michael@0 | 315 | |
michael@0 | 316 | mock('foo', 'bar', baz=2) |
michael@0 | 317 | mock.assert_called_once_with('foo', 'bar', baz=2) |
michael@0 | 318 | |
michael@0 | 319 | mock.reset_mock() |
michael@0 | 320 | mock('foo', 'bar', baz=2) |
michael@0 | 321 | self.assertRaises( |
michael@0 | 322 | AssertionError, |
michael@0 | 323 | lambda: mock.assert_called_once_with('bob', 'bar', baz=2) |
michael@0 | 324 | ) |
michael@0 | 325 | |
michael@0 | 326 | |
michael@0 | 327 | def test_attribute_access_returns_mocks(self): |
michael@0 | 328 | mock = Mock() |
michael@0 | 329 | something = mock.something |
michael@0 | 330 | self.assertTrue(is_instance(something, Mock), "attribute isn't a mock") |
michael@0 | 331 | self.assertEqual(mock.something, something, |
michael@0 | 332 | "different attributes returned for same name") |
michael@0 | 333 | |
michael@0 | 334 | # Usage example |
michael@0 | 335 | mock = Mock() |
michael@0 | 336 | mock.something.return_value = 3 |
michael@0 | 337 | |
michael@0 | 338 | self.assertEqual(mock.something(), 3, "method returned wrong value") |
michael@0 | 339 | self.assertTrue(mock.something.called, |
michael@0 | 340 | "method didn't record being called") |
michael@0 | 341 | |
michael@0 | 342 | |
michael@0 | 343 | def test_attributes_have_name_and_parent_set(self): |
michael@0 | 344 | mock = Mock() |
michael@0 | 345 | something = mock.something |
michael@0 | 346 | |
michael@0 | 347 | self.assertEqual(something._mock_name, "something", |
michael@0 | 348 | "attribute name not set correctly") |
michael@0 | 349 | self.assertEqual(something._mock_parent, mock, |
michael@0 | 350 | "attribute parent not set correctly") |
michael@0 | 351 | |
michael@0 | 352 | |
michael@0 | 353 | def test_method_calls_recorded(self): |
michael@0 | 354 | mock = Mock() |
michael@0 | 355 | mock.something(3, fish=None) |
michael@0 | 356 | mock.something_else.something(6, cake=sentinel.Cake) |
michael@0 | 357 | |
michael@0 | 358 | self.assertEqual(mock.something_else.method_calls, |
michael@0 | 359 | [("something", (6,), {'cake': sentinel.Cake})], |
michael@0 | 360 | "method calls not recorded correctly") |
michael@0 | 361 | self.assertEqual(mock.method_calls, [ |
michael@0 | 362 | ("something", (3,), {'fish': None}), |
michael@0 | 363 | ("something_else.something", (6,), {'cake': sentinel.Cake}) |
michael@0 | 364 | ], |
michael@0 | 365 | "method calls not recorded correctly") |
michael@0 | 366 | |
michael@0 | 367 | |
michael@0 | 368 | def test_method_calls_compare_easily(self): |
michael@0 | 369 | mock = Mock() |
michael@0 | 370 | mock.something() |
michael@0 | 371 | self.assertEqual(mock.method_calls, [('something',)]) |
michael@0 | 372 | self.assertEqual(mock.method_calls, [('something', (), {})]) |
michael@0 | 373 | |
michael@0 | 374 | mock = Mock() |
michael@0 | 375 | mock.something('different') |
michael@0 | 376 | self.assertEqual(mock.method_calls, [('something', ('different',))]) |
michael@0 | 377 | self.assertEqual(mock.method_calls, |
michael@0 | 378 | [('something', ('different',), {})]) |
michael@0 | 379 | |
michael@0 | 380 | mock = Mock() |
michael@0 | 381 | mock.something(x=1) |
michael@0 | 382 | self.assertEqual(mock.method_calls, [('something', {'x': 1})]) |
michael@0 | 383 | self.assertEqual(mock.method_calls, [('something', (), {'x': 1})]) |
michael@0 | 384 | |
michael@0 | 385 | mock = Mock() |
michael@0 | 386 | mock.something('different', some='more') |
michael@0 | 387 | self.assertEqual(mock.method_calls, [ |
michael@0 | 388 | ('something', ('different',), {'some': 'more'}) |
michael@0 | 389 | ]) |
michael@0 | 390 | |
michael@0 | 391 | |
michael@0 | 392 | def test_only_allowed_methods_exist(self): |
michael@0 | 393 | for spec in ['something'], ('something',): |
michael@0 | 394 | for arg in 'spec', 'spec_set': |
michael@0 | 395 | mock = Mock(**{arg: spec}) |
michael@0 | 396 | |
michael@0 | 397 | # this should be allowed |
michael@0 | 398 | mock.something |
michael@0 | 399 | self.assertRaisesRegexp( |
michael@0 | 400 | AttributeError, |
michael@0 | 401 | "Mock object has no attribute 'something_else'", |
michael@0 | 402 | getattr, mock, 'something_else' |
michael@0 | 403 | ) |
michael@0 | 404 | |
michael@0 | 405 | |
michael@0 | 406 | def test_from_spec(self): |
michael@0 | 407 | class Something(object): |
michael@0 | 408 | x = 3 |
michael@0 | 409 | __something__ = None |
michael@0 | 410 | def y(self): |
michael@0 | 411 | pass |
michael@0 | 412 | |
michael@0 | 413 | def test_attributes(mock): |
michael@0 | 414 | # should work |
michael@0 | 415 | mock.x |
michael@0 | 416 | mock.y |
michael@0 | 417 | mock.__something__ |
michael@0 | 418 | self.assertRaisesRegexp( |
michael@0 | 419 | AttributeError, |
michael@0 | 420 | "Mock object has no attribute 'z'", |
michael@0 | 421 | getattr, mock, 'z' |
michael@0 | 422 | ) |
michael@0 | 423 | self.assertRaisesRegexp( |
michael@0 | 424 | AttributeError, |
michael@0 | 425 | "Mock object has no attribute '__foobar__'", |
michael@0 | 426 | getattr, mock, '__foobar__' |
michael@0 | 427 | ) |
michael@0 | 428 | |
michael@0 | 429 | test_attributes(Mock(spec=Something)) |
michael@0 | 430 | test_attributes(Mock(spec=Something())) |
michael@0 | 431 | |
michael@0 | 432 | |
michael@0 | 433 | def test_wraps_calls(self): |
michael@0 | 434 | real = Mock() |
michael@0 | 435 | |
michael@0 | 436 | mock = Mock(wraps=real) |
michael@0 | 437 | self.assertEqual(mock(), real()) |
michael@0 | 438 | |
michael@0 | 439 | real.reset_mock() |
michael@0 | 440 | |
michael@0 | 441 | mock(1, 2, fish=3) |
michael@0 | 442 | real.assert_called_with(1, 2, fish=3) |
michael@0 | 443 | |
michael@0 | 444 | |
michael@0 | 445 | def test_wraps_call_with_nondefault_return_value(self): |
michael@0 | 446 | real = Mock() |
michael@0 | 447 | |
michael@0 | 448 | mock = Mock(wraps=real) |
michael@0 | 449 | mock.return_value = 3 |
michael@0 | 450 | |
michael@0 | 451 | self.assertEqual(mock(), 3) |
michael@0 | 452 | self.assertFalse(real.called) |
michael@0 | 453 | |
michael@0 | 454 | |
michael@0 | 455 | def test_wraps_attributes(self): |
michael@0 | 456 | class Real(object): |
michael@0 | 457 | attribute = Mock() |
michael@0 | 458 | |
michael@0 | 459 | real = Real() |
michael@0 | 460 | |
michael@0 | 461 | mock = Mock(wraps=real) |
michael@0 | 462 | self.assertEqual(mock.attribute(), real.attribute()) |
michael@0 | 463 | self.assertRaises(AttributeError, lambda: mock.fish) |
michael@0 | 464 | |
michael@0 | 465 | self.assertNotEqual(mock.attribute, real.attribute) |
michael@0 | 466 | result = mock.attribute.frog(1, 2, fish=3) |
michael@0 | 467 | Real.attribute.frog.assert_called_with(1, 2, fish=3) |
michael@0 | 468 | self.assertEqual(result, Real.attribute.frog()) |
michael@0 | 469 | |
michael@0 | 470 | |
michael@0 | 471 | def test_exceptional_side_effect(self): |
michael@0 | 472 | mock = Mock(side_effect=AttributeError) |
michael@0 | 473 | self.assertRaises(AttributeError, mock) |
michael@0 | 474 | |
michael@0 | 475 | mock = Mock(side_effect=AttributeError('foo')) |
michael@0 | 476 | self.assertRaises(AttributeError, mock) |
michael@0 | 477 | |
michael@0 | 478 | |
michael@0 | 479 | def test_baseexceptional_side_effect(self): |
michael@0 | 480 | mock = Mock(side_effect=KeyboardInterrupt) |
michael@0 | 481 | self.assertRaises(KeyboardInterrupt, mock) |
michael@0 | 482 | |
michael@0 | 483 | mock = Mock(side_effect=KeyboardInterrupt('foo')) |
michael@0 | 484 | self.assertRaises(KeyboardInterrupt, mock) |
michael@0 | 485 | |
michael@0 | 486 | |
michael@0 | 487 | def test_assert_called_with_message(self): |
michael@0 | 488 | mock = Mock() |
michael@0 | 489 | self.assertRaisesRegexp(AssertionError, 'Not called', |
michael@0 | 490 | mock.assert_called_with) |
michael@0 | 491 | |
michael@0 | 492 | |
michael@0 | 493 | def test__name__(self): |
michael@0 | 494 | mock = Mock() |
michael@0 | 495 | self.assertRaises(AttributeError, lambda: mock.__name__) |
michael@0 | 496 | |
michael@0 | 497 | mock.__name__ = 'foo' |
michael@0 | 498 | self.assertEqual(mock.__name__, 'foo') |
michael@0 | 499 | |
michael@0 | 500 | |
michael@0 | 501 | def test_spec_list_subclass(self): |
michael@0 | 502 | class Sub(list): |
michael@0 | 503 | pass |
michael@0 | 504 | mock = Mock(spec=Sub(['foo'])) |
michael@0 | 505 | |
michael@0 | 506 | mock.append(3) |
michael@0 | 507 | mock.append.assert_called_with(3) |
michael@0 | 508 | self.assertRaises(AttributeError, getattr, mock, 'foo') |
michael@0 | 509 | |
michael@0 | 510 | |
michael@0 | 511 | def test_spec_class(self): |
michael@0 | 512 | class X(object): |
michael@0 | 513 | pass |
michael@0 | 514 | |
michael@0 | 515 | mock = Mock(spec=X) |
michael@0 | 516 | self.assertTrue(isinstance(mock, X)) |
michael@0 | 517 | |
michael@0 | 518 | mock = Mock(spec=X()) |
michael@0 | 519 | self.assertTrue(isinstance(mock, X)) |
michael@0 | 520 | |
michael@0 | 521 | self.assertIs(mock.__class__, X) |
michael@0 | 522 | self.assertEqual(Mock().__class__.__name__, 'Mock') |
michael@0 | 523 | |
michael@0 | 524 | mock = Mock(spec_set=X) |
michael@0 | 525 | self.assertTrue(isinstance(mock, X)) |
michael@0 | 526 | |
michael@0 | 527 | mock = Mock(spec_set=X()) |
michael@0 | 528 | self.assertTrue(isinstance(mock, X)) |
michael@0 | 529 | |
michael@0 | 530 | |
michael@0 | 531 | def test_setting_attribute_with_spec_set(self): |
michael@0 | 532 | class X(object): |
michael@0 | 533 | y = 3 |
michael@0 | 534 | |
michael@0 | 535 | mock = Mock(spec=X) |
michael@0 | 536 | mock.x = 'foo' |
michael@0 | 537 | |
michael@0 | 538 | mock = Mock(spec_set=X) |
michael@0 | 539 | def set_attr(): |
michael@0 | 540 | mock.x = 'foo' |
michael@0 | 541 | |
michael@0 | 542 | mock.y = 'foo' |
michael@0 | 543 | self.assertRaises(AttributeError, set_attr) |
michael@0 | 544 | |
michael@0 | 545 | |
michael@0 | 546 | def test_copy(self): |
michael@0 | 547 | current = sys.getrecursionlimit() |
michael@0 | 548 | self.addCleanup(sys.setrecursionlimit, current) |
michael@0 | 549 | |
michael@0 | 550 | # can't use sys.maxint as this doesn't exist in Python 3 |
michael@0 | 551 | sys.setrecursionlimit(int(10e8)) |
michael@0 | 552 | # this segfaults without the fix in place |
michael@0 | 553 | copy.copy(Mock()) |
michael@0 | 554 | |
michael@0 | 555 | |
michael@0 | 556 | @unittest2.skipIf(inPy3k, "no old style classes in Python 3") |
michael@0 | 557 | def test_spec_old_style_classes(self): |
michael@0 | 558 | class Foo: |
michael@0 | 559 | bar = 7 |
michael@0 | 560 | |
michael@0 | 561 | mock = Mock(spec=Foo) |
michael@0 | 562 | mock.bar = 6 |
michael@0 | 563 | self.assertRaises(AttributeError, lambda: mock.foo) |
michael@0 | 564 | |
michael@0 | 565 | mock = Mock(spec=Foo()) |
michael@0 | 566 | mock.bar = 6 |
michael@0 | 567 | self.assertRaises(AttributeError, lambda: mock.foo) |
michael@0 | 568 | |
michael@0 | 569 | |
michael@0 | 570 | @unittest2.skipIf(inPy3k, "no old style classes in Python 3") |
michael@0 | 571 | def test_spec_set_old_style_classes(self): |
michael@0 | 572 | class Foo: |
michael@0 | 573 | bar = 7 |
michael@0 | 574 | |
michael@0 | 575 | mock = Mock(spec_set=Foo) |
michael@0 | 576 | mock.bar = 6 |
michael@0 | 577 | self.assertRaises(AttributeError, lambda: mock.foo) |
michael@0 | 578 | |
michael@0 | 579 | def _set(): |
michael@0 | 580 | mock.foo = 3 |
michael@0 | 581 | self.assertRaises(AttributeError, _set) |
michael@0 | 582 | |
michael@0 | 583 | mock = Mock(spec_set=Foo()) |
michael@0 | 584 | mock.bar = 6 |
michael@0 | 585 | self.assertRaises(AttributeError, lambda: mock.foo) |
michael@0 | 586 | |
michael@0 | 587 | def _set(): |
michael@0 | 588 | mock.foo = 3 |
michael@0 | 589 | self.assertRaises(AttributeError, _set) |
michael@0 | 590 | |
michael@0 | 591 | |
michael@0 | 592 | def test_subclass_with_properties(self): |
michael@0 | 593 | class SubClass(Mock): |
michael@0 | 594 | def _get(self): |
michael@0 | 595 | return 3 |
michael@0 | 596 | def _set(self, value): |
michael@0 | 597 | raise NameError('strange error') |
michael@0 | 598 | some_attribute = property(_get, _set) |
michael@0 | 599 | |
michael@0 | 600 | s = SubClass(spec_set=SubClass) |
michael@0 | 601 | self.assertEqual(s.some_attribute, 3) |
michael@0 | 602 | |
michael@0 | 603 | def test(): |
michael@0 | 604 | s.some_attribute = 3 |
michael@0 | 605 | self.assertRaises(NameError, test) |
michael@0 | 606 | |
michael@0 | 607 | def test(): |
michael@0 | 608 | s.foo = 'bar' |
michael@0 | 609 | self.assertRaises(AttributeError, test) |
michael@0 | 610 | |
michael@0 | 611 | |
michael@0 | 612 | def test_setting_call(self): |
michael@0 | 613 | mock = Mock() |
michael@0 | 614 | def __call__(self, a): |
michael@0 | 615 | return self._mock_call(a) |
michael@0 | 616 | |
michael@0 | 617 | type(mock).__call__ = __call__ |
michael@0 | 618 | mock('one') |
michael@0 | 619 | mock.assert_called_with('one') |
michael@0 | 620 | |
michael@0 | 621 | self.assertRaises(TypeError, mock, 'one', 'two') |
michael@0 | 622 | |
michael@0 | 623 | |
michael@0 | 624 | @unittest2.skipUnless(sys.version_info[:2] >= (2, 6), |
michael@0 | 625 | "__dir__ not available until Python 2.6 or later") |
michael@0 | 626 | def test_dir(self): |
michael@0 | 627 | mock = Mock() |
michael@0 | 628 | attrs = set(dir(mock)) |
michael@0 | 629 | type_attrs = set([m for m in dir(Mock) if not m.startswith('_')]) |
michael@0 | 630 | |
michael@0 | 631 | # all public attributes from the type are included |
michael@0 | 632 | self.assertEqual(set(), type_attrs - attrs) |
michael@0 | 633 | |
michael@0 | 634 | # creates these attributes |
michael@0 | 635 | mock.a, mock.b |
michael@0 | 636 | self.assertIn('a', dir(mock)) |
michael@0 | 637 | self.assertIn('b', dir(mock)) |
michael@0 | 638 | |
michael@0 | 639 | # instance attributes |
michael@0 | 640 | mock.c = mock.d = None |
michael@0 | 641 | self.assertIn('c', dir(mock)) |
michael@0 | 642 | self.assertIn('d', dir(mock)) |
michael@0 | 643 | |
michael@0 | 644 | # magic methods |
michael@0 | 645 | mock.__iter__ = lambda s: iter([]) |
michael@0 | 646 | self.assertIn('__iter__', dir(mock)) |
michael@0 | 647 | |
michael@0 | 648 | |
michael@0 | 649 | @unittest2.skipUnless(sys.version_info[:2] >= (2, 6), |
michael@0 | 650 | "__dir__ not available until Python 2.6 or later") |
michael@0 | 651 | def test_dir_from_spec(self): |
michael@0 | 652 | mock = Mock(spec=unittest2.TestCase) |
michael@0 | 653 | testcase_attrs = set(dir(unittest2.TestCase)) |
michael@0 | 654 | attrs = set(dir(mock)) |
michael@0 | 655 | |
michael@0 | 656 | # all attributes from the spec are included |
michael@0 | 657 | self.assertEqual(set(), testcase_attrs - attrs) |
michael@0 | 658 | |
michael@0 | 659 | # shadow a sys attribute |
michael@0 | 660 | mock.version = 3 |
michael@0 | 661 | self.assertEqual(dir(mock).count('version'), 1) |
michael@0 | 662 | |
michael@0 | 663 | |
michael@0 | 664 | @unittest2.skipUnless(sys.version_info[:2] >= (2, 6), |
michael@0 | 665 | "__dir__ not available until Python 2.6 or later") |
michael@0 | 666 | def test_filter_dir(self): |
michael@0 | 667 | patcher = patch.object(mock, 'FILTER_DIR', False) |
michael@0 | 668 | patcher.start() |
michael@0 | 669 | try: |
michael@0 | 670 | attrs = set(dir(Mock())) |
michael@0 | 671 | type_attrs = set(dir(Mock)) |
michael@0 | 672 | |
michael@0 | 673 | # ALL attributes from the type are included |
michael@0 | 674 | self.assertEqual(set(), type_attrs - attrs) |
michael@0 | 675 | finally: |
michael@0 | 676 | patcher.stop() |
michael@0 | 677 | |
michael@0 | 678 | |
michael@0 | 679 | def test_configure_mock(self): |
michael@0 | 680 | mock = Mock(foo='bar') |
michael@0 | 681 | self.assertEqual(mock.foo, 'bar') |
michael@0 | 682 | |
michael@0 | 683 | mock = MagicMock(foo='bar') |
michael@0 | 684 | self.assertEqual(mock.foo, 'bar') |
michael@0 | 685 | |
michael@0 | 686 | kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33, |
michael@0 | 687 | 'foo': MagicMock()} |
michael@0 | 688 | mock = Mock(**kwargs) |
michael@0 | 689 | self.assertRaises(KeyError, mock) |
michael@0 | 690 | self.assertEqual(mock.foo.bar(), 33) |
michael@0 | 691 | self.assertIsInstance(mock.foo, MagicMock) |
michael@0 | 692 | |
michael@0 | 693 | mock = Mock() |
michael@0 | 694 | mock.configure_mock(**kwargs) |
michael@0 | 695 | self.assertRaises(KeyError, mock) |
michael@0 | 696 | self.assertEqual(mock.foo.bar(), 33) |
michael@0 | 697 | self.assertIsInstance(mock.foo, MagicMock) |
michael@0 | 698 | |
michael@0 | 699 | |
michael@0 | 700 | def assertRaisesWithMsg(self, exception, message, func, *args, **kwargs): |
michael@0 | 701 | # needed because assertRaisesRegex doesn't work easily with newlines |
michael@0 | 702 | try: |
michael@0 | 703 | func(*args, **kwargs) |
michael@0 | 704 | except: |
michael@0 | 705 | instance = sys.exc_info()[1] |
michael@0 | 706 | self.assertIsInstance(instance, exception) |
michael@0 | 707 | else: |
michael@0 | 708 | self.fail('Exception %r not raised' % (exception,)) |
michael@0 | 709 | |
michael@0 | 710 | msg = str(instance) |
michael@0 | 711 | self.assertEqual(msg, message) |
michael@0 | 712 | |
michael@0 | 713 | |
michael@0 | 714 | def test_assert_called_with_failure_message(self): |
michael@0 | 715 | mock = NonCallableMock() |
michael@0 | 716 | |
michael@0 | 717 | expected = "mock(1, '2', 3, bar='foo')" |
michael@0 | 718 | message = 'Expected call: %s\nNot called' |
michael@0 | 719 | self.assertRaisesWithMsg( |
michael@0 | 720 | AssertionError, message % (expected,), |
michael@0 | 721 | mock.assert_called_with, 1, '2', 3, bar='foo' |
michael@0 | 722 | ) |
michael@0 | 723 | |
michael@0 | 724 | mock.foo(1, '2', 3, foo='foo') |
michael@0 | 725 | |
michael@0 | 726 | |
michael@0 | 727 | asserters = [ |
michael@0 | 728 | mock.foo.assert_called_with, mock.foo.assert_called_once_with |
michael@0 | 729 | ] |
michael@0 | 730 | for meth in asserters: |
michael@0 | 731 | actual = "foo(1, '2', 3, foo='foo')" |
michael@0 | 732 | expected = "foo(1, '2', 3, bar='foo')" |
michael@0 | 733 | message = 'Expected call: %s\nActual call: %s' |
michael@0 | 734 | self.assertRaisesWithMsg( |
michael@0 | 735 | AssertionError, message % (expected, actual), |
michael@0 | 736 | meth, 1, '2', 3, bar='foo' |
michael@0 | 737 | ) |
michael@0 | 738 | |
michael@0 | 739 | # just kwargs |
michael@0 | 740 | for meth in asserters: |
michael@0 | 741 | actual = "foo(1, '2', 3, foo='foo')" |
michael@0 | 742 | expected = "foo(bar='foo')" |
michael@0 | 743 | message = 'Expected call: %s\nActual call: %s' |
michael@0 | 744 | self.assertRaisesWithMsg( |
michael@0 | 745 | AssertionError, message % (expected, actual), |
michael@0 | 746 | meth, bar='foo' |
michael@0 | 747 | ) |
michael@0 | 748 | |
michael@0 | 749 | # just args |
michael@0 | 750 | for meth in asserters: |
michael@0 | 751 | actual = "foo(1, '2', 3, foo='foo')" |
michael@0 | 752 | expected = "foo(1, 2, 3)" |
michael@0 | 753 | message = 'Expected call: %s\nActual call: %s' |
michael@0 | 754 | self.assertRaisesWithMsg( |
michael@0 | 755 | AssertionError, message % (expected, actual), |
michael@0 | 756 | meth, 1, 2, 3 |
michael@0 | 757 | ) |
michael@0 | 758 | |
michael@0 | 759 | # empty |
michael@0 | 760 | for meth in asserters: |
michael@0 | 761 | actual = "foo(1, '2', 3, foo='foo')" |
michael@0 | 762 | expected = "foo()" |
michael@0 | 763 | message = 'Expected call: %s\nActual call: %s' |
michael@0 | 764 | self.assertRaisesWithMsg( |
michael@0 | 765 | AssertionError, message % (expected, actual), meth |
michael@0 | 766 | ) |
michael@0 | 767 | |
michael@0 | 768 | |
michael@0 | 769 | def test_mock_calls(self): |
michael@0 | 770 | mock = MagicMock() |
michael@0 | 771 | |
michael@0 | 772 | # need to do this because MagicMock.mock_calls used to just return |
michael@0 | 773 | # a MagicMock which also returned a MagicMock when __eq__ was called |
michael@0 | 774 | self.assertIs(mock.mock_calls == [], True) |
michael@0 | 775 | |
michael@0 | 776 | mock = MagicMock() |
michael@0 | 777 | mock() |
michael@0 | 778 | expected = [('', (), {})] |
michael@0 | 779 | self.assertEqual(mock.mock_calls, expected) |
michael@0 | 780 | |
michael@0 | 781 | mock.foo() |
michael@0 | 782 | expected.append(call.foo()) |
michael@0 | 783 | self.assertEqual(mock.mock_calls, expected) |
michael@0 | 784 | # intermediate mock_calls work too |
michael@0 | 785 | self.assertEqual(mock.foo.mock_calls, [('', (), {})]) |
michael@0 | 786 | |
michael@0 | 787 | mock = MagicMock() |
michael@0 | 788 | mock().foo(1, 2, 3, a=4, b=5) |
michael@0 | 789 | expected = [ |
michael@0 | 790 | ('', (), {}), ('().foo', (1, 2, 3), dict(a=4, b=5)) |
michael@0 | 791 | ] |
michael@0 | 792 | self.assertEqual(mock.mock_calls, expected) |
michael@0 | 793 | self.assertEqual(mock.return_value.foo.mock_calls, |
michael@0 | 794 | [('', (1, 2, 3), dict(a=4, b=5))]) |
michael@0 | 795 | self.assertEqual(mock.return_value.mock_calls, |
michael@0 | 796 | [('foo', (1, 2, 3), dict(a=4, b=5))]) |
michael@0 | 797 | |
michael@0 | 798 | mock = MagicMock() |
michael@0 | 799 | mock().foo.bar().baz() |
michael@0 | 800 | expected = [ |
michael@0 | 801 | ('', (), {}), ('().foo.bar', (), {}), |
michael@0 | 802 | ('().foo.bar().baz', (), {}) |
michael@0 | 803 | ] |
michael@0 | 804 | self.assertEqual(mock.mock_calls, expected) |
michael@0 | 805 | self.assertEqual(mock().mock_calls, |
michael@0 | 806 | call.foo.bar().baz().call_list()) |
michael@0 | 807 | |
michael@0 | 808 | for kwargs in dict(), dict(name='bar'): |
michael@0 | 809 | mock = MagicMock(**kwargs) |
michael@0 | 810 | int(mock.foo) |
michael@0 | 811 | expected = [('foo.__int__', (), {})] |
michael@0 | 812 | self.assertEqual(mock.mock_calls, expected) |
michael@0 | 813 | |
michael@0 | 814 | mock = MagicMock(**kwargs) |
michael@0 | 815 | mock.a()() |
michael@0 | 816 | expected = [('a', (), {}), ('a()', (), {})] |
michael@0 | 817 | self.assertEqual(mock.mock_calls, expected) |
michael@0 | 818 | self.assertEqual(mock.a().mock_calls, [call()]) |
michael@0 | 819 | |
michael@0 | 820 | mock = MagicMock(**kwargs) |
michael@0 | 821 | mock(1)(2)(3) |
michael@0 | 822 | self.assertEqual(mock.mock_calls, call(1)(2)(3).call_list()) |
michael@0 | 823 | self.assertEqual(mock().mock_calls, call(2)(3).call_list()) |
michael@0 | 824 | self.assertEqual(mock()().mock_calls, call(3).call_list()) |
michael@0 | 825 | |
michael@0 | 826 | mock = MagicMock(**kwargs) |
michael@0 | 827 | mock(1)(2)(3).a.b.c(4) |
michael@0 | 828 | self.assertEqual(mock.mock_calls, |
michael@0 | 829 | call(1)(2)(3).a.b.c(4).call_list()) |
michael@0 | 830 | self.assertEqual(mock().mock_calls, |
michael@0 | 831 | call(2)(3).a.b.c(4).call_list()) |
michael@0 | 832 | self.assertEqual(mock()().mock_calls, |
michael@0 | 833 | call(3).a.b.c(4).call_list()) |
michael@0 | 834 | |
michael@0 | 835 | mock = MagicMock(**kwargs) |
michael@0 | 836 | int(mock().foo.bar().baz()) |
michael@0 | 837 | last_call = ('().foo.bar().baz().__int__', (), {}) |
michael@0 | 838 | self.assertEqual(mock.mock_calls[-1], last_call) |
michael@0 | 839 | self.assertEqual(mock().mock_calls, |
michael@0 | 840 | call.foo.bar().baz().__int__().call_list()) |
michael@0 | 841 | self.assertEqual(mock().foo.bar().mock_calls, |
michael@0 | 842 | call.baz().__int__().call_list()) |
michael@0 | 843 | self.assertEqual(mock().foo.bar().baz.mock_calls, |
michael@0 | 844 | call().__int__().call_list()) |
michael@0 | 845 | |
michael@0 | 846 | |
michael@0 | 847 | def test_subclassing(self): |
michael@0 | 848 | class Subclass(Mock): |
michael@0 | 849 | pass |
michael@0 | 850 | |
michael@0 | 851 | mock = Subclass() |
michael@0 | 852 | self.assertIsInstance(mock.foo, Subclass) |
michael@0 | 853 | self.assertIsInstance(mock(), Subclass) |
michael@0 | 854 | |
michael@0 | 855 | class Subclass(Mock): |
michael@0 | 856 | def _get_child_mock(self, **kwargs): |
michael@0 | 857 | return Mock(**kwargs) |
michael@0 | 858 | |
michael@0 | 859 | mock = Subclass() |
michael@0 | 860 | self.assertNotIsInstance(mock.foo, Subclass) |
michael@0 | 861 | self.assertNotIsInstance(mock(), Subclass) |
michael@0 | 862 | |
michael@0 | 863 | |
michael@0 | 864 | def test_arg_lists(self): |
michael@0 | 865 | mocks = [ |
michael@0 | 866 | Mock(), |
michael@0 | 867 | MagicMock(), |
michael@0 | 868 | NonCallableMock(), |
michael@0 | 869 | NonCallableMagicMock() |
michael@0 | 870 | ] |
michael@0 | 871 | |
michael@0 | 872 | def assert_attrs(mock): |
michael@0 | 873 | names = 'call_args_list', 'method_calls', 'mock_calls' |
michael@0 | 874 | for name in names: |
michael@0 | 875 | attr = getattr(mock, name) |
michael@0 | 876 | self.assertIsInstance(attr, _CallList) |
michael@0 | 877 | self.assertIsInstance(attr, list) |
michael@0 | 878 | self.assertEqual(attr, []) |
michael@0 | 879 | |
michael@0 | 880 | for mock in mocks: |
michael@0 | 881 | assert_attrs(mock) |
michael@0 | 882 | |
michael@0 | 883 | if callable(mock): |
michael@0 | 884 | mock() |
michael@0 | 885 | mock(1, 2) |
michael@0 | 886 | mock(a=3) |
michael@0 | 887 | |
michael@0 | 888 | mock.reset_mock() |
michael@0 | 889 | assert_attrs(mock) |
michael@0 | 890 | |
michael@0 | 891 | mock.foo() |
michael@0 | 892 | mock.foo.bar(1, a=3) |
michael@0 | 893 | mock.foo(1).bar().baz(3) |
michael@0 | 894 | |
michael@0 | 895 | mock.reset_mock() |
michael@0 | 896 | assert_attrs(mock) |
michael@0 | 897 | |
michael@0 | 898 | |
michael@0 | 899 | def test_call_args_two_tuple(self): |
michael@0 | 900 | mock = Mock() |
michael@0 | 901 | mock(1, a=3) |
michael@0 | 902 | mock(2, b=4) |
michael@0 | 903 | |
michael@0 | 904 | self.assertEqual(len(mock.call_args), 2) |
michael@0 | 905 | args, kwargs = mock.call_args |
michael@0 | 906 | self.assertEqual(args, (2,)) |
michael@0 | 907 | self.assertEqual(kwargs, dict(b=4)) |
michael@0 | 908 | |
michael@0 | 909 | expected_list = [((1,), dict(a=3)), ((2,), dict(b=4))] |
michael@0 | 910 | for expected, call_args in zip(expected_list, mock.call_args_list): |
michael@0 | 911 | self.assertEqual(len(call_args), 2) |
michael@0 | 912 | self.assertEqual(expected[0], call_args[0]) |
michael@0 | 913 | self.assertEqual(expected[1], call_args[1]) |
michael@0 | 914 | |
michael@0 | 915 | |
michael@0 | 916 | def test_side_effect_iterator(self): |
michael@0 | 917 | mock = Mock(side_effect=iter([1, 2, 3])) |
michael@0 | 918 | self.assertEqual([mock(), mock(), mock()], [1, 2, 3]) |
michael@0 | 919 | self.assertRaises(StopIteration, mock) |
michael@0 | 920 | |
michael@0 | 921 | mock = MagicMock(side_effect=['a', 'b', 'c']) |
michael@0 | 922 | self.assertEqual([mock(), mock(), mock()], ['a', 'b', 'c']) |
michael@0 | 923 | self.assertRaises(StopIteration, mock) |
michael@0 | 924 | |
michael@0 | 925 | mock = Mock(side_effect='ghi') |
michael@0 | 926 | self.assertEqual([mock(), mock(), mock()], ['g', 'h', 'i']) |
michael@0 | 927 | self.assertRaises(StopIteration, mock) |
michael@0 | 928 | |
michael@0 | 929 | class Foo(object): |
michael@0 | 930 | pass |
michael@0 | 931 | mock = MagicMock(side_effect=Foo) |
michael@0 | 932 | self.assertIsInstance(mock(), Foo) |
michael@0 | 933 | |
michael@0 | 934 | mock = Mock(side_effect=Iter()) |
michael@0 | 935 | self.assertEqual([mock(), mock(), mock(), mock()], |
michael@0 | 936 | ['this', 'is', 'an', 'iter']) |
michael@0 | 937 | self.assertRaises(StopIteration, mock) |
michael@0 | 938 | |
michael@0 | 939 | |
michael@0 | 940 | def test_side_effect_setting_iterator(self): |
michael@0 | 941 | mock = Mock() |
michael@0 | 942 | mock.side_effect = iter([1, 2, 3]) |
michael@0 | 943 | self.assertEqual([mock(), mock(), mock()], [1, 2, 3]) |
michael@0 | 944 | self.assertRaises(StopIteration, mock) |
michael@0 | 945 | side_effect = mock.side_effect |
michael@0 | 946 | self.assertIsInstance(side_effect, type(iter([]))) |
michael@0 | 947 | |
michael@0 | 948 | mock.side_effect = ['a', 'b', 'c'] |
michael@0 | 949 | self.assertEqual([mock(), mock(), mock()], ['a', 'b', 'c']) |
michael@0 | 950 | self.assertRaises(StopIteration, mock) |
michael@0 | 951 | side_effect = mock.side_effect |
michael@0 | 952 | self.assertIsInstance(side_effect, type(iter([]))) |
michael@0 | 953 | |
michael@0 | 954 | this_iter = Iter() |
michael@0 | 955 | mock.side_effect = this_iter |
michael@0 | 956 | self.assertEqual([mock(), mock(), mock(), mock()], |
michael@0 | 957 | ['this', 'is', 'an', 'iter']) |
michael@0 | 958 | self.assertRaises(StopIteration, mock) |
michael@0 | 959 | self.assertIs(mock.side_effect, this_iter) |
michael@0 | 960 | |
michael@0 | 961 | |
michael@0 | 962 | def test_side_effect_iterator_exceptions(self): |
michael@0 | 963 | for Klass in Mock, MagicMock: |
michael@0 | 964 | iterable = (ValueError, 3, KeyError, 6) |
michael@0 | 965 | m = Klass(side_effect=iterable) |
michael@0 | 966 | self.assertRaises(ValueError, m) |
michael@0 | 967 | self.assertEqual(m(), 3) |
michael@0 | 968 | self.assertRaises(KeyError, m) |
michael@0 | 969 | self.assertEqual(m(), 6) |
michael@0 | 970 | |
michael@0 | 971 | |
michael@0 | 972 | def test_assert_has_calls_any_order(self): |
michael@0 | 973 | mock = Mock() |
michael@0 | 974 | mock(1, 2) |
michael@0 | 975 | mock(a=3) |
michael@0 | 976 | mock(3, 4) |
michael@0 | 977 | mock(b=6) |
michael@0 | 978 | mock(b=6) |
michael@0 | 979 | |
michael@0 | 980 | kalls = [ |
michael@0 | 981 | call(1, 2), ({'a': 3},), |
michael@0 | 982 | ((3, 4),), ((), {'a': 3}), |
michael@0 | 983 | ('', (1, 2)), ('', {'a': 3}), |
michael@0 | 984 | ('', (1, 2), {}), ('', (), {'a': 3}) |
michael@0 | 985 | ] |
michael@0 | 986 | for kall in kalls: |
michael@0 | 987 | mock.assert_has_calls([kall], any_order=True) |
michael@0 | 988 | |
michael@0 | 989 | for kall in call(1, '2'), call(b=3), call(), 3, None, 'foo': |
michael@0 | 990 | self.assertRaises( |
michael@0 | 991 | AssertionError, mock.assert_has_calls, |
michael@0 | 992 | [kall], any_order=True |
michael@0 | 993 | ) |
michael@0 | 994 | |
michael@0 | 995 | kall_lists = [ |
michael@0 | 996 | [call(1, 2), call(b=6)], |
michael@0 | 997 | [call(3, 4), call(1, 2)], |
michael@0 | 998 | [call(b=6), call(b=6)], |
michael@0 | 999 | ] |
michael@0 | 1000 | |
michael@0 | 1001 | for kall_list in kall_lists: |
michael@0 | 1002 | mock.assert_has_calls(kall_list, any_order=True) |
michael@0 | 1003 | |
michael@0 | 1004 | kall_lists = [ |
michael@0 | 1005 | [call(b=6), call(b=6), call(b=6)], |
michael@0 | 1006 | [call(1, 2), call(1, 2)], |
michael@0 | 1007 | [call(3, 4), call(1, 2), call(5, 7)], |
michael@0 | 1008 | [call(b=6), call(3, 4), call(b=6), call(1, 2), call(b=6)], |
michael@0 | 1009 | ] |
michael@0 | 1010 | for kall_list in kall_lists: |
michael@0 | 1011 | self.assertRaises( |
michael@0 | 1012 | AssertionError, mock.assert_has_calls, |
michael@0 | 1013 | kall_list, any_order=True |
michael@0 | 1014 | ) |
michael@0 | 1015 | |
michael@0 | 1016 | def test_assert_has_calls(self): |
michael@0 | 1017 | kalls1 = [ |
michael@0 | 1018 | call(1, 2), ({'a': 3},), |
michael@0 | 1019 | ((3, 4),), call(b=6), |
michael@0 | 1020 | ('', (1,), {'b': 6}), |
michael@0 | 1021 | ] |
michael@0 | 1022 | kalls2 = [call.foo(), call.bar(1)] |
michael@0 | 1023 | kalls2.extend(call.spam().baz(a=3).call_list()) |
michael@0 | 1024 | kalls2.extend(call.bam(set(), foo={}).fish([1]).call_list()) |
michael@0 | 1025 | |
michael@0 | 1026 | mocks = [] |
michael@0 | 1027 | for mock in Mock(), MagicMock(): |
michael@0 | 1028 | mock(1, 2) |
michael@0 | 1029 | mock(a=3) |
michael@0 | 1030 | mock(3, 4) |
michael@0 | 1031 | mock(b=6) |
michael@0 | 1032 | mock(1, b=6) |
michael@0 | 1033 | mocks.append((mock, kalls1)) |
michael@0 | 1034 | |
michael@0 | 1035 | mock = Mock() |
michael@0 | 1036 | mock.foo() |
michael@0 | 1037 | mock.bar(1) |
michael@0 | 1038 | mock.spam().baz(a=3) |
michael@0 | 1039 | mock.bam(set(), foo={}).fish([1]) |
michael@0 | 1040 | mocks.append((mock, kalls2)) |
michael@0 | 1041 | |
michael@0 | 1042 | for mock, kalls in mocks: |
michael@0 | 1043 | for i in range(len(kalls)): |
michael@0 | 1044 | for step in 1, 2, 3: |
michael@0 | 1045 | these = kalls[i:i+step] |
michael@0 | 1046 | mock.assert_has_calls(these) |
michael@0 | 1047 | |
michael@0 | 1048 | if len(these) > 1: |
michael@0 | 1049 | self.assertRaises( |
michael@0 | 1050 | AssertionError, |
michael@0 | 1051 | mock.assert_has_calls, |
michael@0 | 1052 | list(reversed(these)) |
michael@0 | 1053 | ) |
michael@0 | 1054 | |
michael@0 | 1055 | |
michael@0 | 1056 | def test_assert_any_call(self): |
michael@0 | 1057 | mock = Mock() |
michael@0 | 1058 | mock(1, 2) |
michael@0 | 1059 | mock(a=3) |
michael@0 | 1060 | mock(1, b=6) |
michael@0 | 1061 | |
michael@0 | 1062 | mock.assert_any_call(1, 2) |
michael@0 | 1063 | mock.assert_any_call(a=3) |
michael@0 | 1064 | mock.assert_any_call(1, b=6) |
michael@0 | 1065 | |
michael@0 | 1066 | self.assertRaises( |
michael@0 | 1067 | AssertionError, |
michael@0 | 1068 | mock.assert_any_call |
michael@0 | 1069 | ) |
michael@0 | 1070 | self.assertRaises( |
michael@0 | 1071 | AssertionError, |
michael@0 | 1072 | mock.assert_any_call, |
michael@0 | 1073 | 1, 3 |
michael@0 | 1074 | ) |
michael@0 | 1075 | self.assertRaises( |
michael@0 | 1076 | AssertionError, |
michael@0 | 1077 | mock.assert_any_call, |
michael@0 | 1078 | a=4 |
michael@0 | 1079 | ) |
michael@0 | 1080 | |
michael@0 | 1081 | |
michael@0 | 1082 | def test_mock_calls_create_autospec(self): |
michael@0 | 1083 | def f(a, b): |
michael@0 | 1084 | pass |
michael@0 | 1085 | obj = Iter() |
michael@0 | 1086 | obj.f = f |
michael@0 | 1087 | |
michael@0 | 1088 | funcs = [ |
michael@0 | 1089 | create_autospec(f), |
michael@0 | 1090 | create_autospec(obj).f |
michael@0 | 1091 | ] |
michael@0 | 1092 | for func in funcs: |
michael@0 | 1093 | func(1, 2) |
michael@0 | 1094 | func(3, 4) |
michael@0 | 1095 | |
michael@0 | 1096 | self.assertEqual( |
michael@0 | 1097 | func.mock_calls, [call(1, 2), call(3, 4)] |
michael@0 | 1098 | ) |
michael@0 | 1099 | |
michael@0 | 1100 | |
michael@0 | 1101 | def test_mock_add_spec(self): |
michael@0 | 1102 | class _One(object): |
michael@0 | 1103 | one = 1 |
michael@0 | 1104 | class _Two(object): |
michael@0 | 1105 | two = 2 |
michael@0 | 1106 | class Anything(object): |
michael@0 | 1107 | one = two = three = 'four' |
michael@0 | 1108 | |
michael@0 | 1109 | klasses = [ |
michael@0 | 1110 | Mock, MagicMock, NonCallableMock, NonCallableMagicMock |
michael@0 | 1111 | ] |
michael@0 | 1112 | for Klass in list(klasses): |
michael@0 | 1113 | klasses.append(lambda K=Klass: K(spec=Anything)) |
michael@0 | 1114 | klasses.append(lambda K=Klass: K(spec_set=Anything)) |
michael@0 | 1115 | |
michael@0 | 1116 | for Klass in klasses: |
michael@0 | 1117 | for kwargs in dict(), dict(spec_set=True): |
michael@0 | 1118 | mock = Klass() |
michael@0 | 1119 | #no error |
michael@0 | 1120 | mock.one, mock.two, mock.three |
michael@0 | 1121 | |
michael@0 | 1122 | for One, Two in [(_One, _Two), (['one'], ['two'])]: |
michael@0 | 1123 | for kwargs in dict(), dict(spec_set=True): |
michael@0 | 1124 | mock.mock_add_spec(One, **kwargs) |
michael@0 | 1125 | |
michael@0 | 1126 | mock.one |
michael@0 | 1127 | self.assertRaises( |
michael@0 | 1128 | AttributeError, getattr, mock, 'two' |
michael@0 | 1129 | ) |
michael@0 | 1130 | self.assertRaises( |
michael@0 | 1131 | AttributeError, getattr, mock, 'three' |
michael@0 | 1132 | ) |
michael@0 | 1133 | if 'spec_set' in kwargs: |
michael@0 | 1134 | self.assertRaises( |
michael@0 | 1135 | AttributeError, setattr, mock, 'three', None |
michael@0 | 1136 | ) |
michael@0 | 1137 | |
michael@0 | 1138 | mock.mock_add_spec(Two, **kwargs) |
michael@0 | 1139 | self.assertRaises( |
michael@0 | 1140 | AttributeError, getattr, mock, 'one' |
michael@0 | 1141 | ) |
michael@0 | 1142 | mock.two |
michael@0 | 1143 | self.assertRaises( |
michael@0 | 1144 | AttributeError, getattr, mock, 'three' |
michael@0 | 1145 | ) |
michael@0 | 1146 | if 'spec_set' in kwargs: |
michael@0 | 1147 | self.assertRaises( |
michael@0 | 1148 | AttributeError, setattr, mock, 'three', None |
michael@0 | 1149 | ) |
michael@0 | 1150 | # note that creating a mock, setting an instance attribute, and |
michael@0 | 1151 | # *then* setting a spec doesn't work. Not the intended use case |
michael@0 | 1152 | |
michael@0 | 1153 | |
michael@0 | 1154 | def test_mock_add_spec_magic_methods(self): |
michael@0 | 1155 | for Klass in MagicMock, NonCallableMagicMock: |
michael@0 | 1156 | mock = Klass() |
michael@0 | 1157 | int(mock) |
michael@0 | 1158 | |
michael@0 | 1159 | mock.mock_add_spec(object) |
michael@0 | 1160 | self.assertRaises(TypeError, int, mock) |
michael@0 | 1161 | |
michael@0 | 1162 | mock = Klass() |
michael@0 | 1163 | mock['foo'] |
michael@0 | 1164 | mock.__int__.return_value =4 |
michael@0 | 1165 | |
michael@0 | 1166 | mock.mock_add_spec(int) |
michael@0 | 1167 | self.assertEqual(int(mock), 4) |
michael@0 | 1168 | self.assertRaises(TypeError, lambda: mock['foo']) |
michael@0 | 1169 | |
michael@0 | 1170 | |
michael@0 | 1171 | def test_adding_child_mock(self): |
michael@0 | 1172 | for Klass in NonCallableMock, Mock, MagicMock, NonCallableMagicMock: |
michael@0 | 1173 | mock = Klass() |
michael@0 | 1174 | |
michael@0 | 1175 | mock.foo = Mock() |
michael@0 | 1176 | mock.foo() |
michael@0 | 1177 | |
michael@0 | 1178 | self.assertEqual(mock.method_calls, [call.foo()]) |
michael@0 | 1179 | self.assertEqual(mock.mock_calls, [call.foo()]) |
michael@0 | 1180 | |
michael@0 | 1181 | mock = Klass() |
michael@0 | 1182 | mock.bar = Mock(name='name') |
michael@0 | 1183 | mock.bar() |
michael@0 | 1184 | self.assertEqual(mock.method_calls, []) |
michael@0 | 1185 | self.assertEqual(mock.mock_calls, []) |
michael@0 | 1186 | |
michael@0 | 1187 | # mock with an existing _new_parent but no name |
michael@0 | 1188 | mock = Klass() |
michael@0 | 1189 | mock.baz = MagicMock()() |
michael@0 | 1190 | mock.baz() |
michael@0 | 1191 | self.assertEqual(mock.method_calls, []) |
michael@0 | 1192 | self.assertEqual(mock.mock_calls, []) |
michael@0 | 1193 | |
michael@0 | 1194 | |
michael@0 | 1195 | def test_adding_return_value_mock(self): |
michael@0 | 1196 | for Klass in Mock, MagicMock: |
michael@0 | 1197 | mock = Klass() |
michael@0 | 1198 | mock.return_value = MagicMock() |
michael@0 | 1199 | |
michael@0 | 1200 | mock()() |
michael@0 | 1201 | self.assertEqual(mock.mock_calls, [call(), call()()]) |
michael@0 | 1202 | |
michael@0 | 1203 | |
michael@0 | 1204 | def test_manager_mock(self): |
michael@0 | 1205 | class Foo(object): |
michael@0 | 1206 | one = 'one' |
michael@0 | 1207 | two = 'two' |
michael@0 | 1208 | manager = Mock() |
michael@0 | 1209 | p1 = patch.object(Foo, 'one') |
michael@0 | 1210 | p2 = patch.object(Foo, 'two') |
michael@0 | 1211 | |
michael@0 | 1212 | mock_one = p1.start() |
michael@0 | 1213 | self.addCleanup(p1.stop) |
michael@0 | 1214 | mock_two = p2.start() |
michael@0 | 1215 | self.addCleanup(p2.stop) |
michael@0 | 1216 | |
michael@0 | 1217 | manager.attach_mock(mock_one, 'one') |
michael@0 | 1218 | manager.attach_mock(mock_two, 'two') |
michael@0 | 1219 | |
michael@0 | 1220 | Foo.two() |
michael@0 | 1221 | Foo.one() |
michael@0 | 1222 | |
michael@0 | 1223 | self.assertEqual(manager.mock_calls, [call.two(), call.one()]) |
michael@0 | 1224 | |
michael@0 | 1225 | |
michael@0 | 1226 | def test_magic_methods_mock_calls(self): |
michael@0 | 1227 | for Klass in Mock, MagicMock: |
michael@0 | 1228 | m = Klass() |
michael@0 | 1229 | m.__int__ = Mock(return_value=3) |
michael@0 | 1230 | m.__float__ = MagicMock(return_value=3.0) |
michael@0 | 1231 | int(m) |
michael@0 | 1232 | float(m) |
michael@0 | 1233 | |
michael@0 | 1234 | self.assertEqual(m.mock_calls, [call.__int__(), call.__float__()]) |
michael@0 | 1235 | self.assertEqual(m.method_calls, []) |
michael@0 | 1236 | |
michael@0 | 1237 | |
michael@0 | 1238 | def test_attribute_deletion(self): |
michael@0 | 1239 | # this behaviour isn't *useful*, but at least it's now tested... |
michael@0 | 1240 | for Klass in Mock, MagicMock, NonCallableMagicMock, NonCallableMock: |
michael@0 | 1241 | m = Klass() |
michael@0 | 1242 | original = m.foo |
michael@0 | 1243 | m.foo = 3 |
michael@0 | 1244 | del m.foo |
michael@0 | 1245 | self.assertEqual(m.foo, original) |
michael@0 | 1246 | |
michael@0 | 1247 | new = m.foo = Mock() |
michael@0 | 1248 | del m.foo |
michael@0 | 1249 | self.assertEqual(m.foo, new) |
michael@0 | 1250 | |
michael@0 | 1251 | |
michael@0 | 1252 | def test_mock_parents(self): |
michael@0 | 1253 | for Klass in Mock, MagicMock: |
michael@0 | 1254 | m = Klass() |
michael@0 | 1255 | original_repr = repr(m) |
michael@0 | 1256 | m.return_value = m |
michael@0 | 1257 | self.assertIs(m(), m) |
michael@0 | 1258 | self.assertEqual(repr(m), original_repr) |
michael@0 | 1259 | |
michael@0 | 1260 | m.reset_mock() |
michael@0 | 1261 | self.assertIs(m(), m) |
michael@0 | 1262 | self.assertEqual(repr(m), original_repr) |
michael@0 | 1263 | |
michael@0 | 1264 | m = Klass() |
michael@0 | 1265 | m.b = m.a |
michael@0 | 1266 | self.assertIn("name='mock.a'", repr(m.b)) |
michael@0 | 1267 | self.assertIn("name='mock.a'", repr(m.a)) |
michael@0 | 1268 | m.reset_mock() |
michael@0 | 1269 | self.assertIn("name='mock.a'", repr(m.b)) |
michael@0 | 1270 | self.assertIn("name='mock.a'", repr(m.a)) |
michael@0 | 1271 | |
michael@0 | 1272 | m = Klass() |
michael@0 | 1273 | original_repr = repr(m) |
michael@0 | 1274 | m.a = m() |
michael@0 | 1275 | m.a.return_value = m |
michael@0 | 1276 | |
michael@0 | 1277 | self.assertEqual(repr(m), original_repr) |
michael@0 | 1278 | self.assertEqual(repr(m.a()), original_repr) |
michael@0 | 1279 | |
michael@0 | 1280 | |
michael@0 | 1281 | def test_attach_mock(self): |
michael@0 | 1282 | classes = Mock, MagicMock, NonCallableMagicMock, NonCallableMock |
michael@0 | 1283 | for Klass in classes: |
michael@0 | 1284 | for Klass2 in classes: |
michael@0 | 1285 | m = Klass() |
michael@0 | 1286 | |
michael@0 | 1287 | m2 = Klass2(name='foo') |
michael@0 | 1288 | m.attach_mock(m2, 'bar') |
michael@0 | 1289 | |
michael@0 | 1290 | self.assertIs(m.bar, m2) |
michael@0 | 1291 | self.assertIn("name='mock.bar'", repr(m2)) |
michael@0 | 1292 | |
michael@0 | 1293 | m.bar.baz(1) |
michael@0 | 1294 | self.assertEqual(m.mock_calls, [call.bar.baz(1)]) |
michael@0 | 1295 | self.assertEqual(m.method_calls, [call.bar.baz(1)]) |
michael@0 | 1296 | |
michael@0 | 1297 | |
michael@0 | 1298 | def test_attach_mock_return_value(self): |
michael@0 | 1299 | classes = Mock, MagicMock, NonCallableMagicMock, NonCallableMock |
michael@0 | 1300 | for Klass in Mock, MagicMock: |
michael@0 | 1301 | for Klass2 in classes: |
michael@0 | 1302 | m = Klass() |
michael@0 | 1303 | |
michael@0 | 1304 | m2 = Klass2(name='foo') |
michael@0 | 1305 | m.attach_mock(m2, 'return_value') |
michael@0 | 1306 | |
michael@0 | 1307 | self.assertIs(m(), m2) |
michael@0 | 1308 | self.assertIn("name='mock()'", repr(m2)) |
michael@0 | 1309 | |
michael@0 | 1310 | m2.foo() |
michael@0 | 1311 | self.assertEqual(m.mock_calls, call().foo().call_list()) |
michael@0 | 1312 | |
michael@0 | 1313 | |
michael@0 | 1314 | def test_attribute_deletion(self): |
michael@0 | 1315 | for mock in Mock(), MagicMock(): |
michael@0 | 1316 | self.assertTrue(hasattr(mock, 'm')) |
michael@0 | 1317 | |
michael@0 | 1318 | del mock.m |
michael@0 | 1319 | self.assertFalse(hasattr(mock, 'm')) |
michael@0 | 1320 | |
michael@0 | 1321 | del mock.f |
michael@0 | 1322 | self.assertFalse(hasattr(mock, 'f')) |
michael@0 | 1323 | self.assertRaises(AttributeError, getattr, mock, 'f') |
michael@0 | 1324 | |
michael@0 | 1325 | |
michael@0 | 1326 | def test_class_assignable(self): |
michael@0 | 1327 | for mock in Mock(), MagicMock(): |
michael@0 | 1328 | self.assertNotIsInstance(mock, int) |
michael@0 | 1329 | |
michael@0 | 1330 | mock.__class__ = int |
michael@0 | 1331 | self.assertIsInstance(mock, int) |
michael@0 | 1332 | |
michael@0 | 1333 | |
michael@0 | 1334 | @unittest2.expectedFailure |
michael@0 | 1335 | def test_pickle(self): |
michael@0 | 1336 | for Klass in (MagicMock, Mock, Subclass, NonCallableMagicMock): |
michael@0 | 1337 | mock = Klass(name='foo', attribute=3) |
michael@0 | 1338 | mock.foo(1, 2, 3) |
michael@0 | 1339 | data = pickle.dumps(mock) |
michael@0 | 1340 | new = pickle.loads(data) |
michael@0 | 1341 | |
michael@0 | 1342 | new.foo.assert_called_once_with(1, 2, 3) |
michael@0 | 1343 | self.assertFalse(new.called) |
michael@0 | 1344 | self.assertTrue(is_instance(new, Klass)) |
michael@0 | 1345 | self.assertIsInstance(new, Thing) |
michael@0 | 1346 | self.assertIn('name="foo"', repr(new)) |
michael@0 | 1347 | self.assertEqual(new.attribute, 3) |
michael@0 | 1348 | |
michael@0 | 1349 | |
michael@0 | 1350 | if __name__ == '__main__': |
michael@0 | 1351 | unittest2.main() |