Tôi đang cố gắng này trong gần hai giờ, không may mắn.Nhập khẩu Mocking bằng Python
Tôi có một mô-đun đó trông như thế này:
try:
from zope.component import queryUtility # and things like this
except ImportError:
# do some fallback operations <-- how to test this?
Sau đó trong các mã:
try:
queryUtility(foo)
except NameError:
# do some fallback actions <-- this one is easy with mocking
# zope.component.queryUtility to raise a NameError
Bất kỳ ý tưởng?
EDIT:
đề nghị của Alex dường như không làm việc:
>>> import __builtin__
>>> realimport = __builtin__.__import__
>>> def fakeimport(name, *args, **kw):
... if name == 'zope.component':
... raise ImportError
... realimport(name, *args, **kw)
...
>>> __builtin__.__import__ = fakeimport
Khi chạy các bài kiểm tra:
[email protected] ~/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
Tuy nhiên, nó không làm việc khi tôi chạy cùng một mã từ giao diện điều khiển tương tác python. EDIT
THÊM:
Tôi đang sử dụng zope.testing
và một tập tin kiểm tra, shorturl.txt
có tất cả các xét nghiệm cụ thể cho phần này của mô-đun của tôi. Trước tiên, tôi sẽ nhập mô-đun với zope.component
có sẵn để chứng minh & kiểm tra mức sử dụng thông thường. Sự vắng mặt của zope.*
gói được coi là một trường hợp cạnh, vì vậy tôi đang thử nghiệm nó sau này. Vì vậy, tôi phải reload()
mô-đun của mình, sau khi thực hiện zope.*
không có sẵn, bằng cách nào đó.
Cho đến nay tôi đã thậm chí đã cố gắng sử dụng tempfile.mktempdir()
và trống rỗng zope/__init__.py
và zope/component/__init__.py
file trong tempdir, sau đó chèn tempdir để sys.path[0]
, và loại bỏ các zope.*
gói cũ từ sys.modules
.
Không hoạt động.
NGAY CẢ EDIT THÊM:
Trong khi đó, tôi đã cố gắng này:
>>> class NoZope(object):
... def find_module(self, fullname, path):
... if fullname.startswith('zope'):
... raise ImportError
...
>>> import sys
>>> sys.path.insert(0, NoZope())
Và nó hoạt động tốt cho không gian tên của bộ ứng dụng thử nghiệm (= cho tất cả các hàng nhập khẩu trong shorturl.txt
) , nhưng nó không được thực hiện trong mô-đun chính của tôi, ao.shorturl
. Ngay cả khi tôi reload()
nó. Bất kỳ ý tưởng tại sao?
>>> import zope # ok, this raises an ImportError
>>> reload(ao.shorturl) <module ...>
Nhập zope.interfaces
đặt ra một ImportError
, vì vậy nó không đến được với các phần nơi tôi nhập zope.component
, và nó vẫn còn trong không gian tên ao.shorturl. Tại sao?!
>>> ao.shorturl.zope.component # why?!
<module ...>
Ah, cảm ơn! Vì một lý do nào đó, tôi đã cố gắng thực hiện 'def __import __()', nhưng không gán nó cho 'builtin .__ import__'; tôi ngớ ngẩn quá. Thú vị, tôi chỉ đọc câu trả lời của bạn ở đây: http://stackoverflow.com/questions/2216828/mock-y-of-from-x-import-y-in-doctest-python/2217512#2217512 - bạn có nghĩ nó sẽ làm cho tình huống này trở nên dễ dàng hơn nếu tôi không import queryUtility vào phạm vi module của tôi? –
@Attila, nếu bạn đã làm 'từ thành phần nhập khẩu zope' và sau đó sử dụng' component.queryUtility', nó sẽ làm cho nó dễ dàng hơn, ví dụ, để sử dụng thực tế một số thời gian, và một phiên bản giả/giả tại lần khác.Như tôi đã viết trong câu trả lời đó, tôi đề nghị nó như là một điều chung, và đó là một phần của cách chúng ta viết mã Python tại Google (đôi khi mệnh đề 'as' để rút ngắn tên một hàng nhập khẩu được bảo đảm, tất nhiên, nhưng điều đó không ' t thay đổi ngữ nghĩa). –
Nếu bạn thực hiện 'từ thành phần import zope', BTW, hàm' __import__' của bạn sẽ thấy ''zope'' làm đối số' name' và '' component'' như một mục trong đối số' fromlist' (chỉ có một, trừ khi bạn làm 'từ zope nhập khẩu này, đó, component' hoặc tương tự ;-); vì vậy hãy chắc chắn để kích hoạt cho phù hợp. –