Skip to content

Commit 74968c1

Browse files
committed
Temporarily make webbrowser module absent the right way
1 parent 7965119 commit 74968c1

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

Diff for: packages/python/plotly/plotly/tests/test_io/test_renderers.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,22 @@ def test_missing_webbrowser_module(fig1):
388388
"""
389389
Assert that no errors occur if the webbrowser module is absent
390390
"""
391-
removed_webbrowser_module = sys.modules["webbrowser"]
392-
del sys.modules["webbrowser"]
393-
with pytest.raises(ImportError):
394-
import webbrowser
395-
fig1._repr_html_()
396-
sys.modules[
397-
"webbrowser"
398-
] = removed_webbrowser_module # restore everything after this test
391+
try:
392+
import builtins
393+
except ImportError:
394+
import __builtin__ as builtins
395+
realimport = builtins.__import__
396+
397+
def webbrowser_absent_import(name, globals, locals, fromlist, level):
398+
"""
399+
Mimick an absent webbrowser module
400+
"""
401+
if name == 'webbrowser':
402+
raise ImportError
403+
return realimport(name, globals, locals, fromlist, level)
404+
405+
with mock.patch("builtins.__import__", webbrowser_absent_import):
406+
fig1._repr_html_()
399407

400408

401409
def test_missing_webbrowser_methods(fig1):

0 commit comments

Comments
 (0)