Skip to content

TST: Fix flaky import test #26953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions pandas/tests/test_downstream.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Testing that we work in the downstream packages
"""
import builtins
import importlib
import subprocess
import sys
Expand Down Expand Up @@ -134,30 +133,13 @@ def test_pyarrow(df):
tm.assert_frame_equal(result, df)


def test_missing_required_dependency(monkeypatch):
def test_missing_required_dependency():
# GH 23868
original_import = __import__

def mock_import_fail(name, *args, **kwargs):
if name == "numpy":
raise ImportError("cannot import name numpy")
elif name == "pytz":
raise ImportError("cannot import name some_dependency")
elif name == "dateutil":
raise ImportError("cannot import name some_other_dependency")
else:
return original_import(name, *args, **kwargs)

expected_msg = (
"Unable to import required dependencies:"
"\nnumpy: cannot import name numpy"
"\npytz: cannot import name some_dependency"
"\ndateutil: cannot import name some_other_dependency"
)

import pandas as pd

with monkeypatch.context() as m:
m.setattr(builtins, "__import__", mock_import_fail)
with pytest.raises(ImportError, match=expected_msg):
importlib.reload(pd)
# use the -S flag to disable site-packages
call = ['python', '-S', '-c', 'import pandas']

with pytest.raises(subprocess.CalledProcessError) as exc:
subprocess.check_output(call, stderr=subprocess.STDOUT)

output = exc.value.stdout.decode()
assert all(x in output for x in ['numpy', 'pytz', 'dateutil'])