mocking for testing applying patch to every test method if you want to mock out something for every test in test class then you can decorate the whole class rather than each individual method the effect of decorating the class is that the patch will be automatically applied to all test methods in the class ( to all methods starting with the word 'test'for exampleimport people from unittest mock import from unittest import testcase from unittest import main @patch('people person'class mytest(testcase)def test_one(selfmockperson)self assertis(people personmockpersondef test_two(selfmocksomeclass)self assertis(people personmocksomeclassdef do_something(self)return 'somethingin the above test classthe tests test_one and test_two are supplied with the mock version of the person class however the do_something(method is not affected using patch as context manager the patch function can be used as context manager this gives fine grained control over the scope of the mock object in the following example the the test_one(method contains with-as statement that we used to patch (mockthe person class as mockperson this mock class is only available within the with-as statement