I just tried to learn the mock and nosetests by running simple examples, but got no luck:
john$ nosetests test_mylib.py
E
======================================================================
ERROR: test_mylib.test_mylib_foo
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/wjq/py-virtenv-2.7.5/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/wjq/py-virtenv-2.7.5/lib/python2.7/site-packages/mock.py", line 1201, in patched
return func(*args, **keywargs)
TypeError: test_mylib_foo() takes exactly 2 arguments (1 given)
However if I run the test directly, it's ok:
john$ python test_mylib.py
john$
I think I must miss some key understanding on the tow libraries since I'm new to them. Really appreciate if someone can point them out.
The followings are my example codes.
test_mylib.py
import mock
import mylib
@mock.patch('mylib.incr')
def test_mylib_foo(aa, incr):
incr.return_value=5
assert mylib.foo(1) == 6
if __name__ == '__main__':
test_mylib_foo(123)
mylib.py
from depen import incr
def foo(aa):
return incr(aa) +1
depen.py
def incr(aa):
return aa+1
Remove the aa
argument and it'll work just fine:
@mock.patch('mylib.incr')
def test_mylib_foo(incr):
incr.return_value=5
assert mylib.foo(1) == 6
if __name__ == '__main__':
test_mylib_foo()
A better __main__
execution would call nose.runmodule:
if __name__ == '__main__':
import nose
nose.runmodule()