the unittest mock library class test_someclass_public_interface(testcase)@patch object(someclass'_hidden_method'def test_public_method(selfmock_method)set up canned response mock_method return_value create object to be tested test_object someclass(result test_object public_method( self assertequal( result'return value from public_method incorrect'in the above code the _hidden_method(is replaced with mock version for someclass within the test_public_method(method note that the mock version of the method is passed in as parameter to the test method so that the canned response can be specified you can also use the @patch(decorator to mock function from module for examplegiven some external module with function api_callwe can mock that function out using the @patch(decorator@patch('external_module api_call'def test_some_func(selfmock_api_call)this uses patch(as decorator and passed the target object' path the target path was 'external_module api_callwhich consists of the module name and the function to mock mocking returned objects in the examples looked at so far the results returned from the mock functions or methods have been simple integers howeverin some cases the returned values must themselves be mocked as the real system would return complex object with multiple attributes and methods the following example uses magicmock object to represent an object returned from mocked function this object has two attributesone is response code and the other is json string json stands for the javascript object notation and is commonly used format in web services