我现在尝试了将近两个小时,没有任何运气。
我有一个看起来像这样的模块:
try:
from zope.component import queryUtility # and things like this
except ImportError:
# do some fallback operations <-- how to test this?
稍后在代码中:
try:
queryUtility(foo)
except NameError:
# do some fallback actions <-- this one is easy with mocking
# zope.component.queryUtility to raise a NameError
有任何想法吗?
编辑:
亚历克斯的建议似乎不起作用:
>>> import __builtin__
>>> realimport = __builtin__.__import__
>>> def fakeimport(name, *args, **kw):
... if name == 'zope.component':
... raise ImportError
... realimport(name, *args, **kw)
...
>>> __builtin__.__import__ = fakeimport
运行测试时:
aatiis@aiur ~/work/ao.shorturl $ ./bin/test --coverage .
Running zope.testing.testrunner.layer.UnitTests tests:
Set up zope.testing.testrunner.layer.UnitTests in 0.000 seconds.
Error in test /home/aatiis/work/ao.shorturl/src/ao/shorturl/shorturl.txt
Traceback (most recent call last):
File "/usr/lib64/python2.5/unittest.py", line 260, in run
testMethod()
File "/usr/lib64/python2.5/doctest.py", line 2123, in runTest
test, out=new.write, clear_globs=False)
File "/usr/lib64/python2.5/doctest.py", line 1361, in run
return self.__run(test, compileflags, out)
File "/usr/lib64/python2.5/doctest.py", line 1282, in __run
exc_info)
File "/usr/lib64/python2.5/doctest.py", line 1148, in report_unexpected_exception
'Exception raised:\n' + _indent(_exception_traceback(exc_info)))
File "/usr/lib64/python2.5/doctest.py", line 1163, in _failure_header
out.append(_indent(source))
File "/usr/lib64/python2.5/doctest.py", line 224, in _indent
return re.sub('(?m)^(?!$)', indent*' ', s)
File "/usr/lib64/python2.5/re.py", line 150, in sub
return _compile(pattern, 0).sub(repl, string, count)
File "/usr/lib64/python2.5/re.py", line 239, in _compile
p = sre_compile.compile(pattern, flags)
File "/usr/lib64/python2.5/sre_compile.py", line 507, in compile
p = sre_parse.parse(p, flags)
AttributeError: 'NoneType' object has no attribute 'parse'
Error in test BaseShortUrlHandler (ao.shorturl)
Traceback (most recent call last):
File "/usr/lib64/python2.5/unittest.py", line 260, in run
testMethod()
File "/usr/lib64/python2.5/doctest.py", line 2123, in runTest
test, out=new.write, clear_globs=False)
File "/usr/lib64/python2.5/doctest.py", line 1351, in run
self.debugger = _OutputRedirectingPdb(save_stdout)
File "/usr/lib64/python2.5/doctest.py", line 324, in __init__
pdb.Pdb.__init__(self, stdout=out)
File "/usr/lib64/python2.5/pdb.py", line 57, in __init__
cmd.Cmd.__init__(self, completekey, stdin, stdout)
File "/usr/lib64/python2.5/cmd.py", line 90, in __init__
import sys
File "<doctest shorturl.txt[10]>", line 4, in fakeimport
NameError: global name 'realimport' is not defined
但是,当我从 python 交互式控制台运行相同的代码时,它确实有效。
更多编辑:
我正在使用zope.testing
一个测试文件,shorturl.txt
其中包含特定于我模块的这一部分的所有测试。首先,我正在导入zope.component
可用的模块,以演示和测试通常的用法。没有zope.*
包被认为是一种极端情况,所以我稍后会对其进行测试。因此,我必须以某种方式reload()
使我的模块zope.*
不可用。
到目前为止,我什至尝试在 tempdir 中使用tempfile.mktempdir()
和清空zope/__init__.py
and文件,然后将zope/component/__init__.py
tempdir 插入到.sys.path[0]
zope.*
sys.modules
也没有用。
更多编辑:
与此同时,我试过这个:
>>> class NoZope(object):
... def find_module(self, fullname, path):
... if fullname.startswith('zope'):
... raise ImportError
...
>>> import sys
>>> sys.path.insert(0, NoZope())
它适用于测试套件的命名空间(= 中的所有导入shorturl.txt
),但它不在我的主模块中执行,ao.shorturl
. 甚至当我reload()
它。知道为什么吗?
>>> import zope # ok, this raises an ImportError
>>> reload(ao.shorturl) <module ...>
导入zope.interfaces
会引发ImportError
,因此它不会到达我 import 的部分zope.component
,并且它保留在 ao.shorturl 命名空间中。为什么?!
>>> ao.shorturl.zope.component # why?!
<module ...>