<div dir="ltr">Hi guys,<div>I'm pretty new to the world of testing in python, and there's a problem I ran into while trying to mock a decorator in the code I'm testing.</div><div><meta http-equiv="content-type" content="text/html; charset=utf-8">Tell me what am I doing wrong or if this is a missing feature in the mock library.</div>
<div><br></div><div>I have the following code:</div><div><br></div><div>a.py:</div><div>-------</div><div><div>import sys</div><div><br></div><div>class BadArgsError(Exception):</div><div> pass</div><div><br></div><div>
<br></div><div>def catch_bad_args(func):</div><div> def call(*args, **kwargs):</div><div> try:</div><div> return func(*args, **kwargs)</div><div> except BadArgsError, e:</div><div> print >> sys.stderr, e.args[0]</div>
<div> print</div><div> sys.exit(1)</div><div><br></div><div> call.__name__ = func.__name__</div><div> call.__dict__ = func.__dict__</div><div> call.__doc__ = func.__doc__</div><div> return call</div>
<div style="text-decoration: underline; "><br></div></div><div>b.py</div><div>------</div><div><div>from a import BadArgsError, catch_bad_args</div><div><br></div><div>@catch_bad_args</div><div>def foo(arg):</div><div> if arg > 2:</div>
<div> raise BadArgsError("arg is bigger than 2")</div><div><br></div><div> print "arg is ok"</div></div><div><br></div><div>test_b.py</div><div>-------------</div><div><div>import unittest</div>
<div>from b import foo</div><div>from a import BadArgsError</div><div>from mock import patch</div><div> </div><div>def do_nothing_decorator(func):</div><div> return func</div><div> </div><div> </div><div>
class BTest(unittest.TestCase):</div>
<div> @patch("a.catch_bad_args", do_nothing_decorator)</div><div> def test_three_raises(self):</div><div> self.assertRaises(BadArgsError, foo, 3)</div><div><div><br></div><div><br></div>
<div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>Now running the tests:</div></div><div>-------------------------------</div><div><div># python test_b.py</div><div>arg is bigger than 2</div><div>
<br></div><div>E</div><div>======================================================================</div><div>ERROR: test_three_raises (__main__.BTest)</div><div>----------------------------------------------------------------------</div>
<div>Traceback (most recent call last):</div><div> File "test_b.py", line 13, in test_three_raises</div><div> self.assertRaises(BadArgsError, foo, 3)</div><div> File "/usr/lib/python2.6/unittest.py", line 336, in failUnlessRaises</div>
<div> callableObj(*args, **kwargs)</div><div> File "/tmp/a.py", line 14, in call</div><div> sys.exit(1)</div><div>SystemExit: 1</div><div><br></div><div>----------------------------------------------------------------------</div>
<div>Ran 1 test in 0.014s</div><div><br></div><div>FAILED (errors=1)</div></div></div></div><div><br></div><div><br></div><div>What am I doing wrong? I tried to change the patch to "b.catch_bad_args" and I get the same results, I also tried @patch.object and other combinations... No help</div>
<div><br></div><div><br></div><div>Thanks a lot!</div></div>