Skip to content

Commit 84c7cf4

Browse files
committed
TEST: Use more constrained mock when testing optpkg
The existing mock raised errors on any import except a whitelisted unittest. Because pytest monkeypatches the Python interpreter and then performs imports within the patched functions, this breaks in Pytest 3.10. Move to an import that blacklists the specific module we're testing.
1 parent a1a55ff commit 84c7cf4

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

nibabel/tests/test_optpkg.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ def test_basic():
3939
# We never have package _not_a_package
4040
assert_bad('_not_a_package')
4141

42-
# setup_module imports unittest, so make sure we don't disrupt that
42+
# Only disrupt imports for "nottriedbefore" package
4343
orig_import = builtins.__import__
4444
def raise_Exception(*args, **kwargs):
45-
if args[0] == 'unittest':
46-
return orig_import(*args, **kwargs)
47-
raise Exception(
48-
"non ImportError could be thrown by some malfunctioning module "
49-
"upon import, and optional_package should catch it too")
45+
if args[0] == 'nottriedbefore':
46+
raise Exception(
47+
"non ImportError could be thrown by some malfunctioning module "
48+
"upon import, and optional_package should catch it too")
49+
return orig_import(*args, **kwargs)
5050
with mock.patch.object(builtins, '__import__', side_effect=raise_Exception):
5151
assert_bad('nottriedbefore')
5252

0 commit comments

Comments
 (0)