I have a test that makes sure a specific (helpful) error message is raised, when a required package is not available.
def foo(caller):
try:
import pkg
except ImportError:
raise ImportError(f'Install "pkg" to use {caller}')
pkg.bar()
with pytest.raises(ImportError, match='Install "pkg" to use test_function'):
foo('test_function')
However, pkg
is generally available, as other tests rely on it.
Currently, I set up an additional virtual env without pkg
just for this test. This seems like overkill.
Is it possible to "hide" an installed package within a module or function?