Mocking ImportError in Python

Posted by Attila Oláh on Stack Overflow See other posts from Stack Overflow or by Attila Oláh
Published on 2010-03-20T01:21:51Z Indexed on 2010/03/20 1:31 UTC
Read the original article Hit count: 517

Filed under:
|
|
|
|

I'm trying this for almost two hours now, without any luck.

I have a module that looks like this:

try:
    from zope.cpomonent 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?

© Stack Overflow or respective owner

Related posts about python

Related posts about unit-testing