I'm using the Python MiniMock library for unit testing. I'd like to mock out a function defined in the same Python file as my doctest. Can MiniMock handle that? The naive approach fails:
def foo():
raise ValueError, "Don't call me during testing!"
def bar():
"""
Returns twice the value of foo()
>>> from minimock import mock
>>> mock('foo',returns=5)
>>> bar()
Called foo()
10
"""
return foo() * 2
if __name__ == "__main__":
import doctest
doctest.testmod()
Here's what happens if I try to run this code:
**********************************************************************
File "test.py", line 9, in __main__.bar
Failed example:
bar()
Exception raised:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/doctest.py", line 1212, in __run
compileflags, 1) in test.globs
File "<doctest __main__.bar[2]>", line 1, in <module>
bar()
File "test.py", line 13, in bar
return foo() * 2
File "test.py", line 2, in foo
raise ValueError, "Don't call me!"
ValueError: Don't call me!
**********************************************************************
1 items had failures:
1 of 3 in __main__.bar
***Test Failed*** 1 failures.
Edit: As per the answers below, this has been identified as a bug, and has been fixed in MiniMock.