简体   繁体   中英

python testing with a imported module

I'm fairly new to using mock and testing in general. This is my first attempt to mock a whole imported module. So for example I have

try:
    import redis
except:
    redis = None

Then later on in the code I check for redis

if redis is None:
     return

How can I set a mock object or class to the redis namespace so I don't have to install redis on my CI server?

Names are just names, and you can assign anything to the 'redis' name at file/global scope, using either import or plain old assignment.

Like so:

import mock_redis as redis

...or so:

def mock_redis(): pass

BTW, your exception clause should be narrowed to handle only ImportError .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM