I am trying to patch some functions during either the setUp or setUpClass methods of a unittest.TestCase subclass.
Given a module patch_me_not.py
# patch_me_not.py
def patch_me(at):
print('I am not patched at {}.'.format(at))
def patch_me_not(at):
patch_me(at)
The following script produces more output that I would expect.
# main.py
import unittest
from unittest.mock import patch
from patch_me_not import patch_me_not
@patch('patch_me_not.patch_me', lambda x: None)
class PatchMeNotTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('I am the setUpClass.')
patch_me_not('setUpClass')
def setUp(self):
print('I am the setUp.')
patch_me_not('setUp')
def test_print(self):
print('I am the test')
patch_me_not('test_print')
if __name__ == '__main__':
unittest.main()
The test script output is
I am the setUpClass.
I am not patched at setUpClass.
I am the setUp.
I am not patched at setUp.
I am the test
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Where I would not expect the two "I am not patched at..." lines in the output if the patch worked in setUp and setUpClass.
How can I get the mock patch to be applied in these methods?