Skip to content

Commit

Permalink
Add import-fails code as a fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Sep 25, 2019
1 parent d05db07 commit e9de088
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
21 changes: 21 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,3 +1054,24 @@ def rpc_balance():
def testdatadir() -> Path:
"""Return the path where testdata files are stored"""
return (Path(__file__).parent / "testdata").resolve()


@pytest.fixture(scope="function")
def import_fails() -> None:
# Source of this test-method:
# https://stackoverflow.com/questions/2481511/mocking-importerror-in-python
import builtins
realimport = builtins.__import__

def mockedimport(name, *args, **kwargs):
if name in ["filelock"]:
raise ImportError(f"No module named '{name}'")
return realimport(name, *args, **kwargs)

builtins.__import__ = mockedimport

# Run test - then cleanup
yield

# restore previous importfunction
builtins.__import__ = realimport
15 changes: 1 addition & 14 deletions tests/optimize/test_hyperopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,9 @@ def test_hyperoptlossresolver_wrongname(mocker, default_conf, caplog) -> None:
HyperOptLossResolver(default_conf, ).hyperopt


def test_start_not_installed(mocker, default_conf, caplog) -> None:
def test_start_not_installed(mocker, default_conf, caplog, import_fails) -> None:
start_mock = MagicMock()
patched_configuration_load_config_file(mocker, default_conf)
# Source of this test-method: https://stackoverflow.com/questions/2481511/mocking-importerror-in-python
import builtins
realimport = builtins.__import__

def mockedimport(name, *args, **kwargs):
if name == "filelock":
raise ImportError("No module named 'filelock'")
return realimport(name, *args, **kwargs)

builtins.__import__ = mockedimport

mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
patch_exchange(mocker)
Expand All @@ -217,9 +207,6 @@ def mockedimport(name, *args, **kwargs):
with pytest.raises(OperationalException, match=r"Please ensure that the hyperopt dependencies"):
start_hyperopt(args)

# restore previous importfunction
builtins.__import__ = realimport


def test_start(mocker, default_conf, caplog) -> None:
start_mock = MagicMock()
Expand Down

0 comments on commit e9de088

Please sign in to comment.