I need to test that specific imports of my library don't import along some other heavy modules (e.g. tensorflow - we'll use it in this question). Here's a detailed description:
The test should run some imports and check that TensorFlow is NOT imported. Preferably, it will check for each import on its own, so we can know which import brought tensorflow with it (in case the test fails).
IMPORTANT: the test must also work when running with pytest-xdist. This means that we need to bypass python's module caching.
Here's a general template of what this test might look like:
def test_not_importing_tensorflow():
# possible setup
import mylib.some_module
# maybe some more imports - again, it's better to isolate each imported module if possible
# check that tensorflow is not imported
# possible cleanup thanks
'tensorflow' not in sys.modules.keys().