Mocking ImportError in Python
问题 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__ >>>