mock and magicmock usage import people from unittest mock import from unittest import testcase from unittest import main class mytest(testcase)def test_one(self)with patch('people person'as mockpersonself assertis(people personmockpersoninstance mockperson return_value instance calculate_pay return_value payroll people payroll(result payroll generate_payslip(instanceself assertequal('you earned 'result'payslip incorrect' mock where you use it the most common error made by people using the unittest mock library is mocking in the wrong place the rule is that you must mock out where you are going to use itor to put it another way you must always mock the real thing where it is imported intonot where it' imported from patch order issues it is possible to have multiple patch decorators on test method howeverthe order in which you define the patch decorators is significant the key to understanding what the order should be is to work backwards so that when the mocks are passed into the test method they are presented to the right parameters for example@patch('mymodule sys'@patch('mymodule os'@patch('mymodule os path'def test_something(selfmock_os_pathmock_osmock_sys)the rest of the test method