According to the
Patch can be used as a TestCase class decorator. It works by
decorating each test method in the class. This reduces the boilerplate
code when your test methods share a common patchings set.
This basically means that you can create a base test class with
@patch decorator applied on it that would mock your external calls while every test method inside would be executed.
Also, you can use
stop() patcher’s methods in
tearDown() methods respectively:
class BaseTestCase(TestCase): def setUp(self): self.patcher = patch('mymodule.foo') self.mock_foo = self.patcher.start() def tearDown(self): self.patcher.stop()