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
So eventually I came up with this solution:
import shlex
import subprocess
import sys
import pytest
def _test_package_not_imported(package: str, tested_modules: list[str]) -> None:
import_str = " ;".join([f"import {module}" for module in tested_modules])
command = shlex.split(f"{sys.executable} -c 'import sys; {import_str}; assert \"{package}\" not in sys.modules'")
try:
subprocess.run(command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except subprocess.CalledProcessError:
pytest.fail(f"{package} is imported")
# example of usage:
def test_no_tf_import():
tested_modules = ["mylib.mymodule_1", "mylib.mymodule_2"]
_test_package_not_imported("tensorflow", tested_modules)
The idea is to run a subprocess, which will provide a clean python environment, regardless of other modules imported during pytest-xdist's run.