Skip to content

Commit cfd65e9

Browse files
TST: Fix flaky import test (#26953)
* 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
1 parent d150f17 commit cfd65e9

File tree

1 file changed

+9
-27
lines changed

1 file changed

+9
-27
lines changed

Diff for: pandas/tests/test_downstream.py

+9-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
Testing that we work in the downstream packages
33
"""
4-
import builtins
54
import importlib
65
import subprocess
76
import sys
@@ -134,30 +133,13 @@ def test_pyarrow(df):
134133
tm.assert_frame_equal(result, df)
135134

136135

137-
def test_missing_required_dependency(monkeypatch):
136+
def test_missing_required_dependency():
138137
# GH 23868
139-
original_import = __import__
140-
141-
def mock_import_fail(name, *args, **kwargs):
142-
if name == "numpy":
143-
raise ImportError("cannot import name numpy")
144-
elif name == "pytz":
145-
raise ImportError("cannot import name some_dependency")
146-
elif name == "dateutil":
147-
raise ImportError("cannot import name some_other_dependency")
148-
else:
149-
return original_import(name, *args, **kwargs)
150-
151-
expected_msg = (
152-
"Unable to import required dependencies:"
153-
"\nnumpy: cannot import name numpy"
154-
"\npytz: cannot import name some_dependency"
155-
"\ndateutil: cannot import name some_other_dependency"
156-
)
157-
158-
import pandas as pd
159-
160-
with monkeypatch.context() as m:
161-
m.setattr(builtins, "__import__", mock_import_fail)
162-
with pytest.raises(ImportError, match=expected_msg):
163-
importlib.reload(pd)
138+
# use the -S flag to disable site-packages
139+
call = ['python', '-S', '-c', 'import pandas']
140+
141+
with pytest.raises(subprocess.CalledProcessError) as exc:
142+
subprocess.check_output(call, stderr=subprocess.STDOUT)
143+
144+
output = exc.value.stdout.decode()
145+
assert all(x in output for x in ['numpy', 'pytz', 'dateutil'])

0 commit comments

Comments
 (0)