Skip to content

Commit

Permalink
TST: Fix flaky import test (#26953)
Browse files Browse the repository at this point in the history
* TST: Fix flaky import test

I'm not sure what, but the missing depedency test is
causing issues. Now we check that things work by running
it in a subprocess with site-packages disabled.

Closes #26952
  • Loading branch information
TomAugspurger authored Jun 20, 2019
1 parent d150f17 commit cfd65e9
Showing 1 changed file with 9 additions and 27 deletions.
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'])

0 comments on commit cfd65e9

Please sign in to comment.