2

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
2
  • 1
    Have you tried the solutions detailed here: stackoverflow.com/questions/4858100/…? Maybe you can for instance check that 'tensorflow' not in sys.modules.keys(). Commented Jan 28 at 14:04
  • @globglogabgalab Checking that tensorflow is not imported is the easy part. The challenging part is to make this test reliable when testing in a distributed environment using pytest-xdist. Commented Jan 28 at 16:26

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Don't you want subprocess.run to fail? That is, you don't want it to be imported.
@DanielWalker no - notice that the command does assert "package" not in sys.modules - it means package is not imported, we're good. Otherwise, a subprocess.CalledProcessError is thrown, we catch it and do pytest.fail (so it's marked as a failure rather than an error)
-1

Use sys.modules to check imports

it checks that Tensowflow is imported in your memory or not

1 Comment

the check itself is easy - I asked about making it work with multi-processing (pytest-xdist)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.