mocking for testing import people from unittest mock import from unittest import testcase from unittest import main class mytest(testcase)@patch('people person'def test_one(selfmockperson)self 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'in this example the people person class has been mocked out this class has method calculate_pay(which is being mocked here the payroll class has method generate_payslip(that expects to be given person object it then uses the information provided by the person objects calculate_pay(method to generate the string returned by the generate_payslip(method attributes on mock classes attributes on mock object can be easily definedfor example if we want to set an attribute on mock object then we can just assign value to the attributeimport people from unittest mock import from unittest import testcase class mytest(testcase)@patch('people person'def test_one(selfmockperson)self assertis(people personmockpersoninstance mockperson return_value instance age instance name 'adamself assertequal( instance age'age incorrect'self assertequal('adam'instance name'name incorrect'in this case the attribute age and name have been added to the mock instance of the people person class