MCPcopy Create free account
hub / github.com/celery/celery / mask_modules

Function mask_modules

t/unit/conftest.py:389–413  ·  view source on GitHub ↗

Ban some modules from being importable inside the context For example:: >>> @pytest.mark.masked_modules('gevent.monkey') >>> def test_foo(self, mask_modules): ... try: ... import sys ... except ImportError: ... print('sys no

(request)

Source from the content-addressed store, hash-verified

387# http://bitbucket.org/runeh/snippets/src/tip/missing_modules.py
388@pytest.fixture
389def mask_modules(request):
390 """Ban some modules from being importable inside the context
391 For example::
392 >>> @pytest.mark.masked_modules('gevent.monkey')
393 >>> def test_foo(self, mask_modules):
394 ... try:
395 ... import sys
396 ... except ImportError:
397 ... print('sys not found')
398 sys not found
399 """
400 realimport = builtins.__import__
401 modnames = request.node.get_closest_marker("masked_modules").args
402
403 def myimp(name, *args, **kwargs):
404 if name in modnames:
405 raise ImportError('No module named %s' % name)
406 else:
407 return realimport(name, *args, **kwargs)
408
409 builtins.__import__ = myimp
410 try:
411 yield
412 finally:
413 builtins.__import__ = realimport
414
415
416@pytest.fixture

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected