import builtins import importlib.util from importlib import reload import pytest from lang_main.errors import DependencyMissingError @pytest.fixture(scope='function') def no_dep(monkeypatch): import_orig = builtins.__import__ def mocked_import(name, globals, locals, fromlist, level): if name == 'py4cytoscape': raise ImportError() return import_orig(name, locals, fromlist, level) monkeypatch.setattr(builtins, '__import__', mocked_import) @pytest.fixture(scope='function') def patch_find_spec(monkeypatch): find_spec_orig = importlib.util.find_spec def mocked_find_spec(*args, **kwargs): if args[0] == 'py4cytoscape': return None else: return find_spec_orig(*args, **kwargs) monkeypatch.setattr(importlib.util, 'find_spec', mocked_find_spec) def test_p4c_available(): import lang_main.constants reload(lang_main.constants) assert lang_main.constants.Dependencies.PY4C.value @pytest.mark.usefixtures('patch_find_spec') def test_p4c_missing(monkeypatch): import lang_main.constants reload(lang_main.constants) assert not lang_main.constants.Dependencies.PY4C.value with pytest.raises(DependencyMissingError): from lang_main import render reload(render)