1 import unittest 2 3 from unittest.mock import ( 4 call, _Call, create_autospec, MagicMock, 5 Mock, ANY, _CallList, patch, PropertyMock 6 ) 7 8 from datetime import datetime 9 10 class SomeClass(object): 11 def one(self, a, b): 12 pass 13 def two(self): 14 pass 15 def three(self, a=None): 16 pass 17 18 19 20 class AnyTest(unittest.TestCase): 21 22 def test_any(self): 23 self.assertEqual(ANY, object()) 24 25 mock = Mock() 26 mock(ANY) 27 mock.assert_called_with(ANY) 28 29 mock = Mock() 30 mock(foo=ANY) 31 mock.assert_called_with(foo=ANY) 32 33 def test_repr(self): 34 self.assertEqual(repr(ANY), '<ANY>') 35 self.assertEqual(str(ANY), '<ANY>') 36 37 38 def test_any_and_datetime(self): 39 mock = Mock() 40 mock(datetime.now(), foo=datetime.now()) 41 42 mock.assert_called_with(ANY, foo=ANY) 43 44 45 def test_any_mock_calls_comparison_order(self): 46 mock = Mock() 47 d = datetime.now() 48 class Foo(object): 49 def __eq__(self, other): 50 return False 51 def __ne__(self, other): 52 return True 53 54 for d in datetime.now(), Foo(): 55 mock.reset_mock() 56 57 mock(d, foo=d, bar=d) 58 mock.method(d, zinga=d, alpha=d) 59 mock().method(a1=d, z99=d) 60 61 expected = [ 62 call(ANY, foo=ANY, bar=ANY), 63 call.method(ANY, zinga=ANY, alpha=ANY), 64 call(), call().method(a1=ANY, z99=ANY) 65 ] 66 self.assertEqual(expected, mock.mock_calls) 67 self.assertEqual(mock.mock_calls, expected) 68 69 70 71 class CallTest(unittest.TestCase): 72 73 def test_call_with_call(self): 74 kall = _Call() 75 self.assertEqual(kall, _Call()) 76 self.assertEqual(kall, _Call(('',))) 77 self.assertEqual(kall, _Call(((),))) 78 self.assertEqual(kall, _Call(({},))) 79 self.assertEqual(kall, _Call(('', ()))) 80 self.assertEqual(kall, _Call(('', {}))) 81 self.assertEqual(kall, _Call(('', (), {}))) 82 self.assertEqual(kall, _Call(('foo',))) 83 self.assertEqual(kall, _Call(('bar', ()))) 84 self.assertEqual(kall, _Call(('baz', {}))) 85 self.assertEqual(kall, _Call(('spam', (), {}))) 86 87 kall = _Call(((1, 2, 3),)) 88 self.assertEqual(kall, _Call(((1, 2, 3),))) 89 self.assertEqual(kall, _Call(('', (1, 2, 3)))) 90 self.assertEqual(kall, _Call(((1, 2, 3), {}))) 91 self.assertEqual(kall, _Call(('', (1, 2, 3), {}))) 92 93 kall = _Call(((1, 2, 4),)) 94 self.assertNotEqual(kall, _Call(('', (1, 2, 3)))) 95 self.assertNotEqual(kall, _Call(('', (1, 2, 3), {}))) 96 97 kall = _Call(('foo', (1, 2, 4),)) 98 self.assertNotEqual(kall, _Call(('', (1, 2, 4)))) 99 self.assertNotEqual(kall, _Call(('', (1, 2, 4), {}))) 100 self.assertNotEqual(kall, _Call(('bar', (1, 2, 4)))) 101 self.assertNotEqual(kall, _Call(('bar', (1, 2, 4), {}))) 102 103 kall = _Call(({'a': 3},)) 104 self.assertEqual(kall, _Call(('', (), {'a': 3}))) 105 self.assertEqual(kall, _Call(('', {'a': 3}))) 106 self.assertEqual(kall, _Call(((), {'a': 3}))) 107 self.assertEqual(kall, _Call(({'a': 3},))) 108 109 110 def test_empty__Call(self): 111 args = _Call() 112 113 self.assertEqual(args, ()) 114 self.assertEqual(args, ('foo',)) 115 self.assertEqual(args, ((),)) 116 self.assertEqual(args, ('foo', ())) 117 self.assertEqual(args, ('foo',(), {})) 118 self.assertEqual(args, ('foo', {})) 119 self.assertEqual(args, ({},)) 120 121 122 def test_named_empty_call(self): 123 args = _Call(('foo', (), {})) 124 125 self.assertEqual(args, ('foo',)) 126 self.assertEqual(args, ('foo', ())) 127 self.assertEqual(args, ('foo',(), {})) 128 self.assertEqual(args, ('foo', {})) 129 130 self.assertNotEqual(args, ((),)) 131 self.assertNotEqual(args, ()) 132 self.assertNotEqual(args, ({},)) 133 self.assertNotEqual(args, ('bar',)) 134 self.assertNotEqual(args, ('bar', ())) 135 self.assertNotEqual(args, ('bar', {})) 136 137 138 def test_call_with_args(self): 139 args = _Call(((1, 2, 3), {})) 140 141 self.assertEqual(args, ((1, 2, 3),)) 142 self.assertEqual(args, ('foo', (1, 2, 3))) 143 self.assertEqual(args, ('foo', (1, 2, 3), {})) 144 self.assertEqual(args, ((1, 2, 3), {})) 145 146 147 def test_named_call_with_args(self): 148 args = _Call(('foo', (1, 2, 3), {})) 149 150 self.assertEqual(args, ('foo', (1, 2, 3))) 151 self.assertEqual(args, ('foo', (1, 2, 3), {})) 152 153 self.assertNotEqual(args, ((1, 2, 3),)) 154 self.assertNotEqual(args, ((1, 2, 3), {})) 155 156 157 def test_call_with_kwargs(self): 158 args = _Call(((), dict(a=3, b=4))) 159 160 self.assertEqual(args, (dict(a=3, b=4),)) 161 self.assertEqual(args, ('foo', dict(a=3, b=4))) 162 self.assertEqual(args, ('foo', (), dict(a=3, b=4))) 163 self.assertEqual(args, ((), dict(a=3, b=4))) 164 165 166 def test_named_call_with_kwargs(self): 167 args = _Call(('foo', (), dict(a=3, b=4))) 168 169 self.assertEqual(args, ('foo', dict(a=3, b=4))) 170 self.assertEqual(args, ('foo', (), dict(a=3, b=4))) 171 172 self.assertNotEqual(args, (dict(a=3, b=4),)) 173 self.assertNotEqual(args, ((), dict(a=3, b=4))) 174 175 176 def test_call_with_args_call_empty_name(self): 177 args = _Call(((1, 2, 3), {})) 178 self.assertEqual(args, call(1, 2, 3)) 179 self.assertEqual(call(1, 2, 3), args) 180 self.assertIn(call(1, 2, 3), [args]) 181 182 183 def test_call_ne(self): 184 self.assertNotEqual(_Call(((1, 2, 3),)), call(1, 2)) 185 self.assertFalse(_Call(((1, 2, 3),)) != call(1, 2, 3)) 186 self.assertTrue(_Call(((1, 2), {})) != call(1, 2, 3)) 187 188 189 def test_call_non_tuples(self): 190 kall = _Call(((1, 2, 3),)) 191 for value in 1, None, self, int: 192 self.assertNotEqual(kall, value) 193 self.assertFalse(kall == value) 194 195 196 def test_repr(self): 197 self.assertEqual(repr(_Call()), 'call()') 198 self.assertEqual(repr(_Call(('foo',))), 'call.foo()') 199 200 self.assertEqual(repr(_Call(((1, 2, 3), {'a': 'b'}))), 201 "call(1, 2, 3, a='b')") 202 self.assertEqual(repr(_Call(('bar', (1, 2, 3), {'a': 'b'}))), 203 "call.bar(1, 2, 3, a='b')") 204 205 self.assertEqual(repr(call), 'call') 206 self.assertEqual(str(call), 'call') 207 208 self.assertEqual(repr(call()), 'call()') 209 self.assertEqual(repr(call(1)), 'call(1)') 210 self.assertEqual(repr(call(zz='thing')), "call(zz='thing')") 211 212 self.assertEqual(repr(call().foo), 'call().foo') 213 self.assertEqual(repr(call(1).foo.bar(a=3).bing), 214 'call().foo.bar().bing') 215 self.assertEqual( 216 repr(call().foo(1, 2, a=3)), 217 "call().foo(1, 2, a=3)" 218 ) 219 self.assertEqual(repr(call()()), "call()()") 220 self.assertEqual(repr(call(1)(2)), "call()(2)") 221 self.assertEqual( 222 repr(call()().bar().baz.beep(1)), 223 "call()().bar().baz.beep(1)" 224 ) 225 226 227 def test_call(self): 228 self.assertEqual(call(), ('', (), {})) 229 self.assertEqual(call('foo', 'bar', one=3, two=4), 230 ('', ('foo', 'bar'), {'one': 3, 'two': 4})) 231 232 mock = Mock() 233 mock(1, 2, 3) 234 mock(a=3, b=6) 235 self.assertEqual(mock.call_args_list, 236 [call(1, 2, 3), call(a=3, b=6)]) 237 238 def test_attribute_call(self): 239 self.assertEqual(call.foo(1), ('foo', (1,), {})) 240 self.assertEqual(call.bar.baz(fish='eggs'), 241 ('bar.baz', (), {'fish': 'eggs'})) 242 243 mock = Mock() 244 mock.foo(1, 2 ,3) 245 mock.bar.baz(a=3, b=6) 246 self.assertEqual(mock.method_calls, 247 [call.foo(1, 2, 3), call.bar.baz(a=3, b=6)]) 248 249 250 def test_extended_call(self): 251 result = call(1).foo(2).bar(3, a=4) 252 self.assertEqual(result, ('().foo().bar', (3,), dict(a=4))) 253 254 mock = MagicMock() 255 mock(1, 2, a=3, b=4) 256 self.assertEqual(mock.call_args, call(1, 2, a=3, b=4)) 257 self.assertNotEqual(mock.call_args, call(1, 2, 3)) 258 259 self.assertEqual(mock.call_args_list, [call(1, 2, a=3, b=4)]) 260 self.assertEqual(mock.mock_calls, [call(1, 2, a=3, b=4)]) 261 262 mock = MagicMock() 263 mock.foo(1).bar()().baz.beep(a=6) 264 265 last_call = call.foo(1).bar()().baz.beep(a=6) 266 self.assertEqual(mock.mock_calls[-1], last_call) 267 self.assertEqual(mock.mock_calls, last_call.call_list()) 268 269 270 def test_call_list(self): 271 mock = MagicMock() 272 mock(1) 273 self.assertEqual(call(1).call_list(), mock.mock_calls) 274 275 mock = MagicMock() 276 mock(1).method(2) 277 self.assertEqual(call(1).method(2).call_list(), 278 mock.mock_calls) 279 280 mock = MagicMock() 281 mock(1).method(2)(3) 282 self.assertEqual(call(1).method(2)(3).call_list(), 283 mock.mock_calls) 284 285 mock = MagicMock() 286 int(mock(1).method(2)(3).foo.bar.baz(4)(5)) 287 kall = call(1).method(2)(3).foo.bar.baz(4)(5).__int__() 288 self.assertEqual(kall.call_list(), mock.mock_calls) 289 290 291 def test_call_any(self): 292 self.assertEqual(call, ANY) 293 294 m = MagicMock() 295 int(m) 296 self.assertEqual(m.mock_calls, [ANY]) 297 self.assertEqual([ANY], m.mock_calls) 298 299 300 def test_two_args_call(self): 301 args = _Call(((1, 2), {'a': 3}), two=True) 302 self.assertEqual(len(args), 2) 303 self.assertEqual(args[0], (1, 2)) 304 self.assertEqual(args[1], {'a': 3}) 305 306 other_args = _Call(((1, 2), {'a': 3})) 307 self.assertEqual(args, other_args) 308 309 def test_call_with_name(self): 310 self.assertEqual(_Call((), 'foo')[0], 'foo') 311 self.assertEqual(_Call((('bar', 'barz'),),)[0], '') 312 self.assertEqual(_Call((('bar', 'barz'), {'hello': 'world'}),)[0], '') 313 314 315 class SpecSignatureTest(unittest.TestCase): 316 317 def _check_someclass_mock(self, mock): 318 self.assertRaises(AttributeError, getattr, mock, 'foo') 319 mock.one(1, 2) 320 mock.one.assert_called_with(1, 2) 321 self.assertRaises(AssertionError, 322 mock.one.assert_called_with, 3, 4) 323 self.assertRaises(TypeError, mock.one, 1) 324 325 mock.two() 326 mock.two.assert_called_with() 327 self.assertRaises(AssertionError, 328 mock.two.assert_called_with, 3) 329 self.assertRaises(TypeError, mock.two, 1) 330 331 mock.three() 332 mock.three.assert_called_with() 333 self.assertRaises(AssertionError, 334 mock.three.assert_called_with, 3) 335 self.assertRaises(TypeError, mock.three, 3, 2) 336 337 mock.three(1) 338 mock.three.assert_called_with(1) 339 340 mock.three(a=1) 341 mock.three.assert_called_with(a=1) 342 343 344 def test_basic(self): 345 mock = create_autospec(SomeClass) 346 self._check_someclass_mock(mock) 347 mock = create_autospec(SomeClass()) 348 self._check_someclass_mock(mock) 349 350 351 def test_create_autospec_return_value(self): 352 def f(): 353 pass 354 mock = create_autospec(f, return_value='foo') 355 self.assertEqual(mock(), 'foo') 356 357 class Foo(object): 358 pass 359 360 mock = create_autospec(Foo, return_value='foo') 361 self.assertEqual(mock(), 'foo') 362 363 364 def test_autospec_reset_mock(self): 365 m = create_autospec(int) 366 int(m) 367 m.reset_mock() 368 self.assertEqual(m.__int__.call_count, 0) 369 370 371 def test_mocking_unbound_methods(self): 372 class Foo(object): 373 def foo(self, foo): 374 pass 375 p = patch.object(Foo, 'foo') 376 mock_foo = p.start() 377 Foo().foo(1) 378 379 mock_foo.assert_called_with(1) 380 381 382 def test_create_autospec_unbound_methods(self): 383 # see mock issue 128 384 # this is expected to fail until the issue is fixed 385 return 386 class Foo(object): 387 def foo(self): 388 pass 389 390 klass = create_autospec(Foo) 391 instance = klass() 392 self.assertRaises(TypeError, instance.foo, 1) 393 394 # Note: no type checking on the "self" parameter 395 klass.foo(1) 396 klass.foo.assert_called_with(1) 397 self.assertRaises(TypeError, klass.foo) 398 399 400 def test_create_autospec_keyword_arguments(self): 401 class Foo(object): 402 a = 3 403 m = create_autospec(Foo, a='3') 404 self.assertEqual(m.a, '3') 405 406 407 def test_create_autospec_keyword_only_arguments(self): 408 def foo(a, *, b=None): 409 pass 410 411 m = create_autospec(foo) 412 m(1) 413 m.assert_called_with(1) 414 self.assertRaises(TypeError, m, 1, 2) 415 416 m(2, b=3) 417 m.assert_called_with(2, b=3) 418 419 420 def test_function_as_instance_attribute(self): 421 obj = SomeClass() 422 def f(a): 423 pass 424 obj.f = f 425 426 mock = create_autospec(obj) 427 mock.f('bing') 428 mock.f.assert_called_with('bing') 429 430 431 def test_spec_as_list(self): 432 # because spec as a list of strings in the mock constructor means 433 # something very different we treat a list instance as the type. 434 mock = create_autospec([]) 435 mock.append('foo') 436 mock.append.assert_called_with('foo') 437 438 self.assertRaises(AttributeError, getattr, mock, 'foo') 439 440 class Foo(object): 441 foo = [] 442 443 mock = create_autospec(Foo) 444 mock.foo.append(3) 445 mock.foo.append.assert_called_with(3) 446 self.assertRaises(AttributeError, getattr, mock.foo, 'foo') 447 448 449 def test_attributes(self): 450 class Sub(SomeClass): 451 attr = SomeClass() 452 453 sub_mock = create_autospec(Sub) 454 455 for mock in (sub_mock, sub_mock.attr): 456 self._check_someclass_mock(mock) 457 458 459 def test_builtin_functions_types(self): 460 # we could replace builtin functions / methods with a function 461 # with *args / **kwargs signature. Using the builtin method type 462 # as a spec seems to work fairly well though. 463 class BuiltinSubclass(list): 464 def bar(self, arg): 465 pass 466 sorted = sorted 467 attr = {} 468 469 mock = create_autospec(BuiltinSubclass) 470 mock.append(3) 471 mock.append.assert_called_with(3) 472 self.assertRaises(AttributeError, getattr, mock.append, 'foo') 473 474 mock.bar('foo') 475 mock.bar.assert_called_with('foo') 476 self.assertRaises(TypeError, mock.bar, 'foo', 'bar') 477 self.assertRaises(AttributeError, getattr, mock.bar, 'foo') 478 479 mock.sorted([1, 2]) 480 mock.sorted.assert_called_with([1, 2]) 481 self.assertRaises(AttributeError, getattr, mock.sorted, 'foo') 482 483 mock.attr.pop(3) 484 mock.attr.pop.assert_called_with(3) 485 self.assertRaises(AttributeError, getattr, mock.attr, 'foo') 486 487 488 def test_method_calls(self): 489 class Sub(SomeClass): 490 attr = SomeClass() 491 492 mock = create_autospec(Sub) 493 mock.one(1, 2) 494 mock.two() 495 mock.three(3) 496 497 expected = [call.one(1, 2), call.two(), call.three(3)] 498 self.assertEqual(mock.method_calls, expected) 499 500 mock.attr.one(1, 2) 501 mock.attr.two() 502 mock.attr.three(3) 503 504 expected.extend( 505 [call.attr.one(1, 2), call.attr.two(), call.attr.three(3)] 506 ) 507 self.assertEqual(mock.method_calls, expected) 508 509 510 def test_magic_methods(self): 511 class BuiltinSubclass(list): 512 attr = {} 513 514 mock = create_autospec(BuiltinSubclass) 515 self.assertEqual(list(mock), []) 516 self.assertRaises(TypeError, int, mock) 517 self.assertRaises(TypeError, int, mock.attr) 518 self.assertEqual(list(mock), []) 519 520 self.assertIsInstance(mock['foo'], MagicMock) 521 self.assertIsInstance(mock.attr['foo'], MagicMock) 522 523 524 def test_spec_set(self): 525 class Sub(SomeClass): 526 attr = SomeClass() 527 528 for spec in (Sub, Sub()): 529 mock = create_autospec(spec, spec_set=True) 530 self._check_someclass_mock(mock) 531 532 self.assertRaises(AttributeError, setattr, mock, 'foo', 'bar') 533 self.assertRaises(AttributeError, setattr, mock.attr, 'foo', 'bar') 534 535 536 def test_descriptors(self): 537 class Foo(object): 538 @classmethod 539 def f(cls, a, b): 540 pass 541 @staticmethod 542 def g(a, b): 543 pass 544 545 class Bar(Foo): 546 pass 547 548 class Baz(SomeClass, Bar): 549 pass 550 551 for spec in (Foo, Foo(), Bar, Bar(), Baz, Baz()): 552 mock = create_autospec(spec) 553 mock.f(1, 2) 554 mock.f.assert_called_once_with(1, 2) 555 556 mock.g(3, 4) 557 mock.g.assert_called_once_with(3, 4) 558 559 560 def test_recursive(self): 561 class A(object): 562 def a(self): 563 pass 564 foo = 'foo bar baz' 565 bar = foo 566 567 A.B = A 568 mock = create_autospec(A) 569 570 mock() 571 self.assertFalse(mock.B.called) 572 573 mock.a() 574 mock.B.a() 575 self.assertEqual(mock.method_calls, [call.a(), call.B.a()]) 576 577 self.assertIs(A.foo, A.bar) 578 self.assertIsNot(mock.foo, mock.bar) 579 mock.foo.lower() 580 self.assertRaises(AssertionError, mock.bar.lower.assert_called_with) 581 582 583 def test_spec_inheritance_for_classes(self): 584 class Foo(object): 585 def a(self, x): 586 pass 587 class Bar(object): 588 def f(self, y): 589 pass 590 591 class_mock = create_autospec(Foo) 592 593 self.assertIsNot(class_mock, class_mock()) 594 595 for this_mock in class_mock, class_mock(): 596 this_mock.a(x=5) 597 this_mock.a.assert_called_with(x=5) 598 this_mock.a.assert_called_with(5) 599 self.assertRaises(TypeError, this_mock.a, 'foo', 'bar') 600 self.assertRaises(AttributeError, getattr, this_mock, 'b') 601 602 instance_mock = create_autospec(Foo()) 603 instance_mock.a(5) 604 instance_mock.a.assert_called_with(5) 605 instance_mock.a.assert_called_with(x=5) 606 self.assertRaises(TypeError, instance_mock.a, 'foo', 'bar') 607 self.assertRaises(AttributeError, getattr, instance_mock, 'b') 608 609 # The return value isn't isn't callable 610 self.assertRaises(TypeError, instance_mock) 611 612 instance_mock.Bar.f(6) 613 instance_mock.Bar.f.assert_called_with(6) 614 instance_mock.Bar.f.assert_called_with(y=6) 615 self.assertRaises(AttributeError, getattr, instance_mock.Bar, 'g') 616 617 instance_mock.Bar().f(6) 618 instance_mock.Bar().f.assert_called_with(6) 619 instance_mock.Bar().f.assert_called_with(y=6) 620 self.assertRaises(AttributeError, getattr, instance_mock.Bar(), 'g') 621 622 623 def test_inherit(self): 624 class Foo(object): 625 a = 3 626 627 Foo.Foo = Foo 628 629 # class 630 mock = create_autospec(Foo) 631 instance = mock() 632 self.assertRaises(AttributeError, getattr, instance, 'b') 633 634 attr_instance = mock.Foo() 635 self.assertRaises(AttributeError, getattr, attr_instance, 'b') 636 637 # instance 638 mock = create_autospec(Foo()) 639 self.assertRaises(AttributeError, getattr, mock, 'b') 640 self.assertRaises(TypeError, mock) 641 642 # attribute instance 643 call_result = mock.Foo() 644 self.assertRaises(AttributeError, getattr, call_result, 'b') 645 646 647 def test_builtins(self): 648 # used to fail with infinite recursion 649 create_autospec(1) 650 651 create_autospec(int) 652 create_autospec('foo') 653 create_autospec(str) 654 create_autospec({}) 655 create_autospec(dict) 656 create_autospec([]) 657 create_autospec(list) 658 create_autospec(set()) 659 create_autospec(set) 660 create_autospec(1.0) 661 create_autospec(float) 662 create_autospec(1j) 663 create_autospec(complex) 664 create_autospec(False) 665 create_autospec(True) 666 667 668 def test_function(self): 669 def f(a, b): 670 pass 671 672 mock = create_autospec(f) 673 self.assertRaises(TypeError, mock) 674 mock(1, 2) 675 mock.assert_called_with(1, 2) 676 mock.assert_called_with(1, b=2) 677 mock.assert_called_with(a=1, b=2) 678 679 f.f = f 680 mock = create_autospec(f) 681 self.assertRaises(TypeError, mock.f) 682 mock.f(3, 4) 683 mock.f.assert_called_with(3, 4) 684 mock.f.assert_called_with(a=3, b=4) 685 686 687 def test_skip_attributeerrors(self): 688 class Raiser(object): 689 def __get__(self, obj, type=None): 690 if obj is None: 691 raise AttributeError('Can only be accessed via an instance') 692 693 class RaiserClass(object): 694 raiser = Raiser() 695 696 @staticmethod 697 def existing(a, b): 698 return a + b 699 700 s = create_autospec(RaiserClass) 701 self.assertRaises(TypeError, lambda x: s.existing(1, 2, 3)) 702 s.existing(1, 2) 703 self.assertRaises(AttributeError, lambda: s.nonexisting) 704 705 # check we can fetch the raiser attribute and it has no spec 706 obj = s.raiser 707 obj.foo, obj.bar 708 709 710 def test_signature_class(self): 711 class Foo(object): 712 def __init__(self, a, b=3): 713 pass 714 715 mock = create_autospec(Foo) 716 717 self.assertRaises(TypeError, mock) 718 mock(1) 719 mock.assert_called_once_with(1) 720 mock.assert_called_once_with(a=1) 721 self.assertRaises(AssertionError, mock.assert_called_once_with, 2) 722 723 mock(4, 5) 724 mock.assert_called_with(4, 5) 725 mock.assert_called_with(a=4, b=5) 726 self.assertRaises(AssertionError, mock.assert_called_with, a=5, b=4) 727 728 729 def test_class_with_no_init(self): 730 # this used to raise an exception 731 # due to trying to get a signature from object.__init__ 732 class Foo(object): 733 pass 734 create_autospec(Foo) 735 736 737 def test_signature_callable(self): 738 class Callable(object): 739 def __init__(self, x, y): 740 pass 741 def __call__(self, a): 742 pass 743 744 mock = create_autospec(Callable) 745 mock(1, 2) 746 mock.assert_called_once_with(1, 2) 747 mock.assert_called_once_with(x=1, y=2) 748 self.assertRaises(TypeError, mock, 'a') 749 750 instance = mock(1, 2) 751 self.assertRaises(TypeError, instance) 752 instance(a='a') 753 instance.assert_called_once_with('a') 754 instance.assert_called_once_with(a='a') 755 instance('a') 756 instance.assert_called_with('a') 757 instance.assert_called_with(a='a') 758 759 mock = create_autospec(Callable(1, 2)) 760 mock(a='a') 761 mock.assert_called_once_with(a='a') 762 self.assertRaises(TypeError, mock) 763 mock('a') 764 mock.assert_called_with('a') 765 766 767 def test_signature_noncallable(self): 768 class NonCallable(object): 769 def __init__(self): 770 pass 771 772 mock = create_autospec(NonCallable) 773 instance = mock() 774 mock.assert_called_once_with() 775 self.assertRaises(TypeError, mock, 'a') 776 self.assertRaises(TypeError, instance) 777 self.assertRaises(TypeError, instance, 'a') 778 779 mock = create_autospec(NonCallable()) 780 self.assertRaises(TypeError, mock) 781 self.assertRaises(TypeError, mock, 'a') 782 783 784 def test_create_autospec_none(self): 785 class Foo(object): 786 bar = None 787 788 mock = create_autospec(Foo) 789 none = mock.bar 790 self.assertNotIsInstance(none, type(None)) 791 792 none.foo() 793 none.foo.assert_called_once_with() 794 795 796 def test_autospec_functions_with_self_in_odd_place(self): 797 class Foo(object): 798 def f(a, self): 799 pass 800 801 a = create_autospec(Foo) 802 a.f(10) 803 a.f.assert_called_with(10) 804 a.f.assert_called_with(self=10) 805 a.f(self=10) 806 a.f.assert_called_with(10) 807 a.f.assert_called_with(self=10) 808 809 810 def test_autospec_data_descriptor(self): 811 class Descriptor(object): 812 def __init__(self, value): 813 self.value = value 814 815 def __get__(self, obj, cls=None): 816 if obj is None: 817 return self 818 return self.value 819 820 def __set__(self, obj, value): 821 pass 822 823 class MyProperty(property): 824 pass 825 826 class Foo(object): 827 __slots__ = ['slot'] 828 829 @property 830 def prop(self): 831 return 3 832 833 @MyProperty 834 def subprop(self): 835 return 4 836 837 desc = Descriptor(42) 838 839 foo = create_autospec(Foo) 840 841 def check_data_descriptor(mock_attr): 842 # Data descriptors don't have a spec. 843 self.assertIsInstance(mock_attr, MagicMock) 844 mock_attr(1, 2, 3) 845 mock_attr.abc(4, 5, 6) 846 mock_attr.assert_called_once_with(1, 2, 3) 847 mock_attr.abc.assert_called_once_with(4, 5, 6) 848 849 # property 850 check_data_descriptor(foo.prop) 851 # property subclass 852 check_data_descriptor(foo.subprop) 853 # class __slot__ 854 check_data_descriptor(foo.slot) 855 # plain data descriptor 856 check_data_descriptor(foo.desc) 857 858 859 class TestCallList(unittest.TestCase): 860 861 def test_args_list_contains_call_list(self): 862 mock = Mock() 863 self.assertIsInstance(mock.call_args_list, _CallList) 864 865 mock(1, 2) 866 mock(a=3) 867 mock(3, 4) 868 mock(b=6) 869 870 for kall in call(1, 2), call(a=3), call(3, 4), call(b=6): 871 self.assertIn(kall, mock.call_args_list) 872 873 calls = [call(a=3), call(3, 4)] 874 self.assertIn(calls, mock.call_args_list) 875 calls = [call(1, 2), call(a=3)] 876 self.assertIn(calls, mock.call_args_list) 877 calls = [call(3, 4), call(b=6)] 878 self.assertIn(calls, mock.call_args_list) 879 calls = [call(3, 4)] 880 self.assertIn(calls, mock.call_args_list) 881 882 self.assertNotIn(call('fish'), mock.call_args_list) 883 self.assertNotIn([call('fish')], mock.call_args_list) 884 885 886 def test_call_list_str(self): 887 mock = Mock() 888 mock(1, 2) 889 mock.foo(a=3) 890 mock.foo.bar().baz('fish', cat='dog') 891 892 expected = ( 893 "[call(1, 2),\n" 894 " call.foo(a=3),\n" 895 " call.foo.bar(),\n" 896 " call.foo.bar().baz('fish', cat='dog')]" 897 ) 898 self.assertEqual(str(mock.mock_calls), expected) 899 900 901 def test_propertymock(self): 902 p = patch('%s.SomeClass.one' % __name__, new_callable=PropertyMock) 903 mock = p.start() 904 try: 905 SomeClass.one 906 mock.assert_called_once_with() 907 908 s = SomeClass() 909 s.one 910 mock.assert_called_with() 911 self.assertEqual(mock.mock_calls, [call(), call()]) 912 913 s.one = 3 914 self.assertEqual(mock.mock_calls, [call(), call(), call(3)]) 915 finally: 916 p.stop() 917 918 919 def test_propertymock_returnvalue(self): 920 m = MagicMock() 921 p = PropertyMock() 922 type(m).foo = p 923 924 returned = m.foo 925 p.assert_called_once_with() 926 self.assertIsInstance(returned, MagicMock) 927 self.assertNotIsInstance(returned, PropertyMock) 928 929 930 if __name__ == '__main__': 931 unittest.main() 932