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
在这种情况下?