mock and magicmock usage if the attribute itself needs to be mock object then all that is required is to assign magicmock (or mockobject to that attributeinstance address magicmock(name='address'mocking constants it is very easy to mock out constantthis can be done using the @patch(decorator and proving the name of the constant and the new value to use this value can be literal value such as or 'helloor it can be mock object itself (such as magicmock objectfor example@patch('mymodule max_count' def test_something(self)test can now use mymodule max_count mocking properties it is also possible to mock python properties this is done again using the @patch decorator but using the unittest mock propertymock class and the new_callable parameter for example@patch('mymoule car wheels'new_callable=mock propertymockdef test_some_property(selfmock_wheels)mock_wheels return_value rest of test method raising exceptions with mocks very useful attribute that can be specified when mock object is created is the side_effect if you set this to an exception class or instance then the exception will be raised when the mock is calledfor examplemock mock(side_effect=exception('boom!')mock(this will result in the exception being raised when the mock(is invoked