python如何回退_在Python中测试回退导入

pip

,但如果用户没有安装正确的模块,我想提供一个回退模块。

我想在不切换环境的情况下进行单元测试。因此如下:

文件a.py

"""

This module would, under ideal circumstances, be installed with pip

but maybe not...

"""

class Foo():

@staticmethod

def test():

return "This is the module we'd like to import"

"""

This is my own fallback module

"""

class Foo():

@staticmethod

def test():

return "This is the fallback module"

文件c.py

try:

from sandbox.a import Foo

except ImportError:

from sandbox.b import Foo

"""This is the module in my app that would actually use Foo"""

这是测试,d.py

import sys

def test_it():

sys.modules['a'] = None

import sandbox.c as c

s = c.Foo.test()

assert s == "This is the fallback module"

E AssertionError: assert 'This is the ...ike to import' == 'This is the fallback module'

E - This is the module we'd like to import

E + This is the fallback module

sandbox/d.py:8: AssertionError

测试这个的正确方法是什么,以便我确保用户永远不会得到importerrorror(如果他们没有模块

a.py

b.py

在这种情况下?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值