zope.component

Mocking ImportError in Python

坚强是说给别人听的谎言 提交于 2019-12-17 16:29:41
问题 I'm trying this for almost two hours now, without any luck. I have a module that looks like this: try: from zope.component import queryUtility # and things like this except ImportError: # do some fallback operations <-- how to test this? Later in the code: try: queryUtility(foo) except NameError: # do some fallback actions <-- this one is easy with mocking # zope.component.queryUtility to raise a NameError Any ideas? EDIT: Alex's suggestion doesn't seem to work: >>> import __builtin__ >>>

Mocking ImportError in Python

若如初见. 提交于 2019-11-27 23:12:49
I'm trying this for almost two hours now, without any luck. I have a module that looks like this: try: from zope.component import queryUtility # and things like this except ImportError: # do some fallback operations <-- how to test this? Later in the code: try: queryUtility(foo) except NameError: # do some fallback actions <-- this one is easy with mocking # zope.component.queryUtility to raise a NameError Any ideas? EDIT: Alex's suggestion doesn't seem to work: >>> import __builtin__ >>> realimport = __builtin__.__import__ >>> def fakeimport(name, *args, **kw): ... if name == 'zope.component'