I'm writing tests for models that trigger various functions which go beyond the scope of the testing (some functions invoke network functionality and other undesired things, which are not part of the test, can be slow to just let run and is easier to just switch off for testing purposes). We do this by using a series of @patch decorators above every test function as required.
I don't like putting the same decorators over every test function in the test class because they clutter up the code. Is it possible to move all my patches into a function and just call it from there? I tried a simple implementation like below but it didn't work for me:
from unittest.mock import patch
class MyTest(APITestCase):
def patch_all(self):
p1 = patch('mod1.func1')
p2 = patch('mod1.func2')
...
return (p1, p2, ...)
def test_function(self):
p1, p2, ... = self.patch_all()
perform_actual_test()
Is there a way to patch out the functions without writing decorators everywhere and just calling a function instead?