Mock pathc question
Avishay Balderman
balderman at gmail.com
Thu Aug 8 00:51:50 EDT 2013
Hi
1) I prefer to use start/stop and not the decorator .
2) mock_play is the name of the module where the code belongs
Thanks
Avishay
Sent from my iPhone
On 7 באוג 2013, at 21:01, Jean-Michel Pichavant <jeanmichel at sequans.com>
wrote:
----- Mail original -----
Hi
I would like to mock patch the attribute 'calc' in the 'Client' class
(See code below).
I have 2 unit tests:
1) test1 - that patch an existing instance of 'Client' - it works
fine.
1) test2 - that tries to patch the 'Client' class. My expectation is
that after the patching, every instance of 'Client' will be created
with 'MockClient'. However this is not the case..
Can you please advice?
Thanks
Avishay
One way to do this is to decorate the test2 method,
http://www.voidspace.org.uk/python/mock/patch.html.
This way you get rid of all the start/stop boiler-plate, the scope of your
patch is the scope of the method.
*code not tested*
class TestIt(unittest.TestCase):
def setUp(self):
pass
@mock.patch(Calc, MockCalc)
def test2(self):
client = Client()
# result should be 7
print str(client.add(1,34))
def test3(self):
client = Client()
# result should be 35 again
print str(client.add(1,34))
By the way, what is 'mock_play' in your original post, could be the reason
why you things did go wrong.
JM
-- IMPORTANT NOTICE:
The contents of this email and any attachments are confidential and may
also be privileged. If you are not the intended recipient, please notify
the sender immediately and do not disclose the contents to any other
person, use it for any purpose, or store or copy the information in any
medium. Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130808/d4488ca2/attachment.html>
More information about the Python-list
mailing list