Best Python code snippet using autotest_python
testhelpers.py
Source:testhelpers.py
...252 mock.three.assert_called_with(1)253 mock.three(a=1)254 mock.three.assert_called_with(a=1)255 def test_basic(self):256 mock = create_autospec(SomeClass)257 self._check_someclass_mock(mock)258 mock = create_autospec(SomeClass())259 self._check_someclass_mock(mock)260 def test_create_autospec_return_value(self):261 def f():262 pass263 mock = create_autospec(f, return_value='foo')264 self.assertEqual(mock(), 'foo')265 class Foo(object):266 pass267 mock = create_autospec(Foo, return_value='foo')268 self.assertEqual(mock(), 'foo')269 def test_autospec_reset_mock(self):270 m = create_autospec(int)271 int(m)272 m.reset_mock()273 self.assertEqual(m.__int__.call_count, 0)274 def test_mocking_unbound_methods(self):275 class Foo(object):276 def foo(self, foo):277 pass278 p = patch.object(Foo, 'foo')279 mock_foo = p.start()280 Foo().foo(1)281 mock_foo.assert_called_with(1)282 def test_create_autospec_unbound_methods(self):283 # see mock issue 128284 # this is expected to fail until the issue is fixed285 return286 class Foo(object):287 def foo(self):288 pass289 klass = create_autospec(Foo)290 instance = klass()291 self.assertRaises(TypeError, instance.foo, 1)292 # Note: no type checking on the "self" parameter293 klass.foo(1)294 klass.foo.assert_called_with(1)295 self.assertRaises(TypeError, klass.foo)296 def test_create_autospec_keyword_arguments(self):297 class Foo(object):298 a = 3299 m = create_autospec(Foo, a='3')300 self.assertEqual(m.a, '3')301 def test_create_autospec_keyword_only_arguments(self):302 def foo(a, *, b=None):303 pass304 m = create_autospec(foo)305 m(1)306 m.assert_called_with(1)307 self.assertRaises(TypeError, m, 1, 2)308 m(2, b=3)309 m.assert_called_with(2, b=3)310 def test_function_as_instance_attribute(self):311 obj = SomeClass()312 def f(a):313 pass314 obj.f = f315 mock = create_autospec(obj)316 mock.f('bing')317 mock.f.assert_called_with('bing')318 def test_spec_as_list(self):319 # because spec as a list of strings in the mock constructor means320 # something very different we treat a list instance as the type.321 mock = create_autospec([])322 mock.append('foo')323 mock.append.assert_called_with('foo')324 self.assertRaises(AttributeError, getattr, mock, 'foo')325 class Foo(object):326 foo = []327 mock = create_autospec(Foo)328 mock.foo.append(3)329 mock.foo.append.assert_called_with(3)330 self.assertRaises(AttributeError, getattr, mock.foo, 'foo')331 def test_attributes(self):332 class Sub(SomeClass):333 attr = SomeClass()334 sub_mock = create_autospec(Sub)335 for mock in (sub_mock, sub_mock.attr):336 self._check_someclass_mock(mock)337 def test_builtin_functions_types(self):338 # we could replace builtin functions / methods with a function339 # with *args / **kwargs signature. Using the builtin method type340 # as a spec seems to work fairly well though.341 class BuiltinSubclass(list):342 def bar(self, arg):343 pass344 sorted = sorted345 attr = {}346 mock = create_autospec(BuiltinSubclass)347 mock.append(3)348 mock.append.assert_called_with(3)349 self.assertRaises(AttributeError, getattr, mock.append, 'foo')350 mock.bar('foo')351 mock.bar.assert_called_with('foo')352 self.assertRaises(TypeError, mock.bar, 'foo', 'bar')353 self.assertRaises(AttributeError, getattr, mock.bar, 'foo')354 mock.sorted([1, 2])355 mock.sorted.assert_called_with([1, 2])356 self.assertRaises(AttributeError, getattr, mock.sorted, 'foo')357 mock.attr.pop(3)358 mock.attr.pop.assert_called_with(3)359 self.assertRaises(AttributeError, getattr, mock.attr, 'foo')360 def test_method_calls(self):361 class Sub(SomeClass):362 attr = SomeClass()363 mock = create_autospec(Sub)364 mock.one(1, 2)365 mock.two()366 mock.three(3)367 expected = [call.one(1, 2), call.two(), call.three(3)]368 self.assertEqual(mock.method_calls, expected)369 mock.attr.one(1, 2)370 mock.attr.two()371 mock.attr.three(3)372 expected.extend(373 [call.attr.one(1, 2), call.attr.two(), call.attr.three(3)]374 )375 self.assertEqual(mock.method_calls, expected)376 def test_magic_methods(self):377 class BuiltinSubclass(list):378 attr = {}379 mock = create_autospec(BuiltinSubclass)380 self.assertEqual(list(mock), [])381 self.assertRaises(TypeError, int, mock)382 self.assertRaises(TypeError, int, mock.attr)383 self.assertEqual(list(mock), [])384 self.assertIsInstance(mock['foo'], MagicMock)385 self.assertIsInstance(mock.attr['foo'], MagicMock)386 def test_spec_set(self):387 class Sub(SomeClass):388 attr = SomeClass()389 for spec in (Sub, Sub()):390 mock = create_autospec(spec, spec_set=True)391 self._check_someclass_mock(mock)392 self.assertRaises(AttributeError, setattr, mock, 'foo', 'bar')393 self.assertRaises(AttributeError, setattr, mock.attr, 'foo', 'bar')394 def test_descriptors(self):395 class Foo(object):396 @classmethod397 def f(cls, a, b):398 pass399 @staticmethod400 def g(a, b):401 pass402 class Bar(Foo):403 pass404 class Baz(SomeClass, Bar):405 pass406 for spec in (Foo, Foo(), Bar, Bar(), Baz, Baz()):407 mock = create_autospec(spec)408 mock.f(1, 2)409 mock.f.assert_called_once_with(1, 2)410 mock.g(3, 4)411 mock.g.assert_called_once_with(3, 4)412 def test_recursive(self):413 class A(object):414 def a(self):415 pass416 foo = 'foo bar baz'417 bar = foo418 A.B = A419 mock = create_autospec(A)420 mock()421 self.assertFalse(mock.B.called)422 mock.a()423 mock.B.a()424 self.assertEqual(mock.method_calls, [call.a(), call.B.a()])425 self.assertIs(A.foo, A.bar)426 self.assertIsNot(mock.foo, mock.bar)427 mock.foo.lower()428 self.assertRaises(AssertionError, mock.bar.lower.assert_called_with)429 def test_spec_inheritance_for_classes(self):430 class Foo(object):431 def a(self, x):432 pass433 class Bar(object):434 def f(self, y):435 pass436 class_mock = create_autospec(Foo)437 self.assertIsNot(class_mock, class_mock())438 for this_mock in class_mock, class_mock():439 this_mock.a(x=5)440 this_mock.a.assert_called_with(x=5)441 this_mock.a.assert_called_with(5)442 self.assertRaises(TypeError, this_mock.a, 'foo', 'bar')443 self.assertRaises(AttributeError, getattr, this_mock, 'b')444 instance_mock = create_autospec(Foo())445 instance_mock.a(5)446 instance_mock.a.assert_called_with(5)447 instance_mock.a.assert_called_with(x=5)448 self.assertRaises(TypeError, instance_mock.a, 'foo', 'bar')449 self.assertRaises(AttributeError, getattr, instance_mock, 'b')450 # The return value isn't isn't callable451 self.assertRaises(TypeError, instance_mock)452 instance_mock.Bar.f(6)453 instance_mock.Bar.f.assert_called_with(6)454 instance_mock.Bar.f.assert_called_with(y=6)455 self.assertRaises(AttributeError, getattr, instance_mock.Bar, 'g')456 instance_mock.Bar().f(6)457 instance_mock.Bar().f.assert_called_with(6)458 instance_mock.Bar().f.assert_called_with(y=6)459 self.assertRaises(AttributeError, getattr, instance_mock.Bar(), 'g')460 def test_inherit(self):461 class Foo(object):462 a = 3463 Foo.Foo = Foo464 # class465 mock = create_autospec(Foo)466 instance = mock()467 self.assertRaises(AttributeError, getattr, instance, 'b')468 attr_instance = mock.Foo()469 self.assertRaises(AttributeError, getattr, attr_instance, 'b')470 # instance471 mock = create_autospec(Foo())472 self.assertRaises(AttributeError, getattr, mock, 'b')473 self.assertRaises(TypeError, mock)474 # attribute instance475 call_result = mock.Foo()476 self.assertRaises(AttributeError, getattr, call_result, 'b')477 def test_builtins(self):478 # used to fail with infinite recursion479 create_autospec(1)480 create_autospec(int)481 create_autospec('foo')482 create_autospec(str)483 create_autospec({})484 create_autospec(dict)485 create_autospec([])486 create_autospec(list)487 create_autospec(set())488 create_autospec(set)489 create_autospec(1.0)490 create_autospec(float)491 create_autospec(1j)492 create_autospec(complex)493 create_autospec(False)494 create_autospec(True)495 def test_function(self):496 def f(a, b):497 pass498 mock = create_autospec(f)499 self.assertRaises(TypeError, mock)500 mock(1, 2)501 mock.assert_called_with(1, 2)502 mock.assert_called_with(1, b=2)503 mock.assert_called_with(a=1, b=2)504 f.f = f505 mock = create_autospec(f)506 self.assertRaises(TypeError, mock.f)507 mock.f(3, 4)508 mock.f.assert_called_with(3, 4)509 mock.f.assert_called_with(a=3, b=4)510 def test_skip_attributeerrors(self):511 class Raiser(object):512 def __get__(self, obj, type=None):513 if obj is None:514 raise AttributeError('Can only be accessed via an instance')515 class RaiserClass(object):516 raiser = Raiser()517 @staticmethod518 def existing(a, b):519 return a + b520 s = create_autospec(RaiserClass)521 self.assertRaises(TypeError, lambda x: s.existing(1, 2, 3))522 s.existing(1, 2)523 self.assertRaises(AttributeError, lambda: s.nonexisting)524 # check we can fetch the raiser attribute and it has no spec525 obj = s.raiser526 obj.foo, obj.bar527 def test_signature_class(self):528 class Foo(object):529 def __init__(self, a, b=3):530 pass531 mock = create_autospec(Foo)532 self.assertRaises(TypeError, mock)533 mock(1)534 mock.assert_called_once_with(1)535 mock.assert_called_once_with(a=1)536 self.assertRaises(AssertionError, mock.assert_called_once_with, 2)537 mock(4, 5)538 mock.assert_called_with(4, 5)539 mock.assert_called_with(a=4, b=5)540 self.assertRaises(AssertionError, mock.assert_called_with, a=5, b=4)541 def test_class_with_no_init(self):542 # this used to raise an exception543 # due to trying to get a signature from object.__init__544 class Foo(object):545 pass546 create_autospec(Foo)547 def test_signature_callable(self):548 class Callable(object):549 def __init__(self, x, y):550 pass551 def __call__(self, a):552 pass553 mock = create_autospec(Callable)554 mock(1, 2)555 mock.assert_called_once_with(1, 2)556 mock.assert_called_once_with(x=1, y=2)557 self.assertRaises(TypeError, mock, 'a')558 instance = mock(1, 2)559 self.assertRaises(TypeError, instance)560 instance(a='a')561 instance.assert_called_once_with('a')562 instance.assert_called_once_with(a='a')563 instance('a')564 instance.assert_called_with('a')565 instance.assert_called_with(a='a')566 mock = create_autospec(Callable(1, 2))567 mock(a='a')568 mock.assert_called_once_with(a='a')569 self.assertRaises(TypeError, mock)570 mock('a')571 mock.assert_called_with('a')572 def test_signature_noncallable(self):573 class NonCallable(object):574 def __init__(self):575 pass576 mock = create_autospec(NonCallable)577 instance = mock()578 mock.assert_called_once_with()579 self.assertRaises(TypeError, mock, 'a')580 self.assertRaises(TypeError, instance)581 self.assertRaises(TypeError, instance, 'a')582 mock = create_autospec(NonCallable())583 self.assertRaises(TypeError, mock)584 self.assertRaises(TypeError, mock, 'a')585 def test_create_autospec_none(self):586 class Foo(object):587 bar = None588 mock = create_autospec(Foo)589 none = mock.bar590 self.assertNotIsInstance(none, type(None))591 none.foo()592 none.foo.assert_called_once_with()593 def test_autospec_functions_with_self_in_odd_place(self):594 class Foo(object):595 def f(a, self):596 pass597 a = create_autospec(Foo)598 a.f(10)599 a.f.assert_called_with(10)600 a.f.assert_called_with(self=10)601 a.f(self=10)602 a.f.assert_called_with(10)603 a.f.assert_called_with(self=10)604 def test_autospec_data_descriptor(self):605 class Descriptor(object):606 def __init__(self, value):607 self.value = value608 def __get__(self, obj, cls=None):609 if obj is None:610 return self611 return self.value612 def __set__(self, obj, value):613 pass614 class MyProperty(property):615 pass616 class Foo(object):617 __slots__ = ['slot']618 @property619 def prop(self):620 return 3621 @MyProperty622 def subprop(self):623 return 4624 desc = Descriptor(42)625 foo = create_autospec(Foo)626 def check_data_descriptor(mock_attr):627 # Data descriptors don't have a spec.628 self.assertIsInstance(mock_attr, MagicMock)629 mock_attr(1, 2, 3)630 mock_attr.abc(4, 5, 6)631 mock_attr.assert_called_once_with(1, 2, 3)632 mock_attr.abc.assert_called_once_with(4, 5, 6)633 # property634 check_data_descriptor(foo.prop)635 # property subclass636 check_data_descriptor(foo.subprop)637 # class __slot__638 check_data_descriptor(foo.slot)639 # plain data descriptor640 check_data_descriptor(foo.desc)641 def test_autospec_on_bound_builtin_function(self):642 meth = types.MethodType(time.ctime, time.time())643 self.assertIsInstance(meth(), str)644 mocked = create_autospec(meth)645 # no signature, so no spec to check against646 mocked()647 mocked.assert_called_once_with()648 mocked.reset_mock()649 mocked(4, 5, 6)650 mocked.assert_called_once_with(4, 5, 6)651class TestCallList(unittest.TestCase):652 def test_args_list_contains_call_list(self):653 mock = Mock()654 self.assertIsInstance(mock.call_args_list, _CallList)655 mock(1, 2)656 mock(a=3)657 mock(3, 4)658 mock(b=6)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!