Avoiding running top-level module code in unit test
The unpleasant_side_effect
is run for two reasons. First because the imports are handled before the test case is started and is therefore not mocked when importing is happening. Secondly, because the mocking itself imports work.py
and thus runs unpleasant_side_effect
even if work_caller.py
was not imported.
The import problem can be solved by mocking the module work.py
itself. This can either be done globally in the test module or in the testcase itself. Here I assigned it a MagicMock
, which can be imported, called etc.
test_work.py
from unittest import TestCase, mock
class TestWorkMockingModule(TestCase):
def test_workcaller(self):
import sys
sys.modules['work.work'] = mock.MagicMock()
from work.work_caller import WorkCaller
sut = WorkCaller()
sut.call_work()
The downside is that work_on is also mocked, which I am not sure whether is a problem in your case.
It is not possible to not run the entire module when it is imported, since functions and classes are also statements, thus the module execution has to finish before returning to the caller, where one want to alter the imported module.