[英]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 然后在代码中稍后检查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? 如何将模拟对象或类设置为redis命名空间,以便不必在CI服务器上安装redis?
Names are just names, and you can assign anything to the 'redis' name at file/global scope, using either import
or plain old assignment. 名称只是名称,您可以在文件/全局范围内使用
import
或普通旧式赋值为“ redis”名称赋任何值。
Like so: 像这样:
import mock_redis as redis
...or so: ...或者:
def mock_redis(): pass
BTW, your exception clause should be narrowed to handle only ImportError
. 顺便说一句,您的异常子句应缩小为仅处理
ImportError
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.