samedi 10 novembre 2018

Python how to mock an ImportError when a package is imported

I have a package that has an optional library install. When the optional library is not present and the particular method is called a custom exception is raised saying the feature is not enabled without the optional package install. When the optional library is present then the method can be used.

I want to test both of these cases with pytest.

I am importing the libraries like:

try:
    import derp
except ImportError:
    pass

And then in the function checking if the library is installed by checking if the package is in sys.modules.

def my_feature_method():
    if 'derp' not in sys.modules:
        raise Exception('this feature requires the derp package to be installed')

    # do some stuff ...

I want to be able to test the case where the package is installed and is not installed in pytest.

I thought about using mock patch on sys.modules but is this overkill?

Aucun commentaire:

Enregistrer un commentaire