TL;DR
Patch sys.modules
using unittest.mock
:
mock.patch.dict(
sys.modules,
{'somefakepackage': mock.Mock()},
)
Explanation
Other answers correctly recommend to fix sys.modules
but a proper way is to temporarily patch it by using mock.patch
. It will replace a module only for the time tests are run with a fake object that imitates the desired behaviour. And restore it back once tests are finished to not affect other test cases.
The code in TL;DR section will simply make your missing package not raise ImportError
. To provide fake package with contents and imitate desired behaviour, initiate mock.Mock(…)
with proper arguments (e.g. add attributes via Mock's **kwargs
).
Full code example
The code below temporarily patches sys.modules
so that it includes somefakepackage
and makes it importable from the dependent modules without ImportError
.
import sys
import unittest
from unittest import mock
class SomeTestCase(unittest.TestCase):
def test_smth(self):
# implement your testing logic, for example:
self.assertEqual(
123,
somefakepackage_dependent.some_func(),
)
@classmethod
def setUpClass(cls): # called once before all the tests
# define what to patch sys.modules with
cls._modules_patcher = mock.patch.dict(
sys.modules,
{'somefakepackage': mock.Mock()},
)
# actually patch it
cls._modules_patcher.start()
# make the package globally visible and import it,
# just like if you have imported it in a usual way
# placing import statement at the top of the file,
# but relying on a patched dependency
global somefakepackage_dependent
import somefakepackage_dependent
@classmethod # called once after all tests
def tearDownClass(cls):
# restore initial sys.modules state back
cls._modules_patcher.stop()
To read more about setUpClass
/tearDownClass
methods, see unittest
docs.
unittest
's built-in mock
subpackage is actually a very powerful tool. Dive deeper into its documentation to get a better understanding.