async def test_we_can_mock_and_verify_an_async_method(self): expected = "Hello" with tmock(MyAsyncThing) as my_async_mock: when(await my_async_mock.get_an_async_result()).then_return(expected) self.assertEqual(expected, await my_async_mock.get_an_async_result()) verify(my_async_mock).get_an_async_result()
def test_mock__can_mock_method__no_args__no_return(self): for mocked_thing in mocked_things: with self.subTest("{}".format(mocked_thing)): with tmock(mocked_thing) as my_thing_mock: when(my_thing_mock.do_something_with_side_effects() ).then_return(None) my_thing_mock.do_something_with_side_effects() verify(my_thing_mock).do_something_with_side_effects()
def test_verify__any_matcher__not_called(self): with tmock(MyThing) as my_thing_mock: when(my_thing_mock.convert_int_to_str(1)).then_return("something") when(my_thing_mock.some_instance_attribute).then_return("hello") with self.assertRaises(VerifyError): verify(my_thing_mock).convert_int_to_str(match.anything()) with self.assertRaises(VerifyError): verify(my_thing_mock).some_instance_attribute = match.anything()
def test_mock__can_mock_method__default_args(self): for mocked_thing in mocked_things: with self.subTest("{}".format(mocked_thing)): with tmock(mocked_thing) as my_thing_mock: when(my_thing_mock.method_with_default_args( first_number=1)).then_return(None) my_thing_mock.method_with_default_args(1) verify(my_thing_mock).method_with_default_args(1)
def test_mock__can_mock_method__arg__returns(self): for mocked_thing in mocked_things: with self.subTest("{}".format(mocked_thing)): expected_result = "a string" with tmock(mocked_thing) as my_thing_mock: when(my_thing_mock.convert_int_to_str(1)).then_return( expected_result) actual = my_thing_mock.convert_int_to_str(1) self.assertEqual(expected_result, actual) verify(my_thing_mock).convert_int_to_str(1)
def test_mock__can_mock_method__multiple_args__mixed_with_kwargs_in_setup( self): for mocked_thing in mocked_things: with self.subTest("{}".format(mocked_thing)): expected_result = "a string" with tmock(mocked_thing) as my_thing_mock: when(my_thing_mock.multiple_arg( number=1, prefix="p")).then_return(expected_result) actual = my_thing_mock.multiple_arg("p", 1) self.assertEqual(expected_result, actual) verify(my_thing_mock).multiple_arg("p", 1)
def test_verify__any_matcher(self): with tmock(MyThing) as my_thing_mock: when(my_thing_mock.convert_int_to_str(1)).then_return("something") when(my_thing_mock.some_instance_attribute).then_return("hello") # Method my_thing_mock.convert_int_to_str(1) verify(my_thing_mock).convert_int_to_str(match.anything()) # Set Attribute my_thing_mock.some_instance_attribute = "bye" verify(my_thing_mock).some_instance_attribute = match.anything()
def test_mock__can_mock_method__object_arg__returns(self): for mocked_thing in mocked_things: with self.subTest("{}".format(mocked_thing)): expected_result = 2 object_arg = NestedThing() with tmock(mocked_thing) as my_thing_mock: when(my_thing_mock.method_with_object( object_arg)).then_return(expected_result) actual = my_thing_mock.method_with_object(object_arg) self.assertEqual(expected_result, actual) verify(my_thing_mock).method_with_object(object_arg)
def test_verify__exactly__not_matched__other_calls__verify_error(self): with tmock(MyThing) as my_thing_mock: when(my_thing_mock.some_instance_attribute).then_return("Hello") when(my_thing_mock.convert_int_to_str( match.anything())).then_return("A string") my_thing_mock.convert_int_to_str(1) # Method with self.assertRaises(VerifyError): verify(my_thing_mock, exactly=2).convert_int_to_str(2) my_thing_mock.some_instance_attribute = "something else" # Set attribute with self.assertRaises(VerifyError): verify(my_thing_mock, exactly=2).some_instance_attribute = "bye"
def test_verify__exactly__not_matched__no_other_calls__verify_error(self): with tmock(MyThing) as my_thing_mock: when(my_thing_mock.some_instance_attribute).then_return("Hello") when(my_thing_mock.do_something_with_side_effects()).then_return( None) # Method with self.assertRaises(VerifyError): verify(my_thing_mock, exactly=1).do_something_with_side_effects() # Get attribute with self.assertRaises(VerifyError): verify(my_thing_mock, exactly=1).some_instance_attribute # Set attribute with self.assertRaises(VerifyError): verify(my_thing_mock, exactly=1).some_instance_attribute = "bye"
def test_mock__can_mock_method__generic_args_and_return__returns(self): for mocked_thing in mocked_things: with self.subTest("{}".format(mocked_thing)): expected_result = {"my_key": True} with tmock(mocked_thing) as my_thing_mock: when( my_thing_mock. method_with_standard_generic_args_and_return( list_arg=["hello"], dict_arg={"foo": False})).then_return(expected_result) actual = my_thing_mock.method_with_standard_generic_args_and_return( list_arg=["hello"], dict_arg={"foo": False}) self.assertEqual(expected_result, actual) verify(my_thing_mock ).method_with_standard_generic_args_and_return( list_arg=["hello"], dict_arg={"foo": False})
def test_verify__at_least__was_called(self): with tmock(MyThing) as my_thing_mock: when(my_thing_mock.some_instance_attribute).then_return("Hello") when(my_thing_mock.do_something_with_side_effects()).then_return( None) # Method my_thing_mock.do_something_with_side_effects() verify(my_thing_mock).do_something_with_side_effects() # Get Attribute my_thing_mock.some_instance_attribute verify(my_thing_mock).some_instance_attribute # Set Attribute my_thing_mock.some_instance_attribute = "bye" verify(my_thing_mock).some_instance_attribute = "bye"
def test_mock__can_mock_method__args_and_kwargs__returns(self): for mocked_thing in mocked_things: with self.subTest("{}".format(mocked_thing)): expected_result = False with tmock(mocked_thing) as my_thing_mock: when( my_thing_mock.method_with_args_and_kwargs( "a", "b", key1=1, key2=2)).then_return(expected_result) actual = my_thing_mock.method_with_args_and_kwargs("a", "b", key1=1, key2=2) self.assertEqual(expected_result, actual) verify(my_thing_mock).method_with_args_and_kwargs("a", "b", key1=1, key2=2)
def test_verify__exactly__incorrect_amount_of_times__verify_error(self): with tmock(MyThing) as my_thing_mock: when(my_thing_mock.do_something_with_side_effects()).then_return( None) when(my_thing_mock.some_instance_attribute).then_return("hello") # Method my_thing_mock.do_something_with_side_effects() with self.assertRaises(VerifyError): verify(my_thing_mock, exactly=2).do_something_with_side_effects() # Get Attribute my_thing_mock.some_instance_attribute with self.assertRaises(VerifyError): verify(my_thing_mock, exactly=2).some_instance_attribute # Set Attribute my_thing_mock.some_instance_attribute = "bye" with self.assertRaises(VerifyError): verify(my_thing_mock, exactly=2).some_instance_attribute = "bye"