Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'attribute': 0.07; 'method.': 0.07; 'tries': 0.07; '8bit%:78': 0.09; 'advice?': 0.09; 'below).': 0.09; 'decorator': 0.09; 'expectation': 0.09; 'method,': 0.09; 'url:voidspace': 0.09; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; 'def': 0.12; 'belongs': 0.16; 'client()': 0.16; 'fine.': 0.16; 'message-id:@unknownmsgid': 0.16; 'url:patch': 0.16; 'wrote:': 0.18; 'module': 0.19; 'cc:addr:python.org': 0.22; 'print': 0.22; 'rid': 0.24; 'cc:2**0': 0.24; '(see': 0.26; 'class.': 0.26; 'pass': 0.26; 'header:In- Reply-To:1': 0.27; 'patch': 0.29; 'code': 0.31; "skip:' 10": 0.31; 'class': 0.32; 'url:python': 0.33; '-----': 0.33; 'could': 0.34; 'created': 0.35; 'received:google.com': 0.35; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'unit': 0.37; 'thank': 0.38; 'skip:\xc2 10': 0.60; 'you.': 0.62; 'information': 0.63; 'name': 0.63; 'iphone': 0.65; 'notice:': 0.67; '8bit%:74': 0.68; 'person,': 0.68; 'privileged.': 0.69; 'disclose': 0.74; 'decorate': 0.84; 'mock': 0.84; '2013,': 0.91; '8bit%:70': 0.91; 'medium.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=references:from:mime-version:in-reply-to:date:message-id:subject:to :cc:content-type; bh=wFkxpjos0Vf3U1qBevKlFoyjHwz2jWrx2EDH8Z3L4E8=; b=g1LX9bfgXOwjzozk7/2R7KwyoWCqlmYqT6rEwi9ij4y2+RNXJHgsOe7tQ2nXQlmqHF Yus7JPrjjESi9oj+hCSKYhVSCcLqxsaZrIRy8aa/xxm8T0SjS87jgo26+1PhE48RqLSf hvI4ZyOVYHCHsLjnpeLTejiARgcFjDQ8UOW3YKUVYFunN8KTEF4K8usJDMSGf7XTHSQ0 wAIr9z183veHYydsuKFN9yqG1MzsQlW6mI8Z8GwOdaO8aT/Q/oJ3G2kkvpp1E3rvEsLT O2BSRucXmc01RjTd5bWzQfFfwtNBgwH+ST/grV9QBPFskX93NpgQlGs3KioFWNnW45E/ JxSw== X-Received: by 10.68.101.225 with SMTP id fj1mr4155963pbb.8.1375937512735; Wed, 07 Aug 2013 21:51:52 -0700 (PDT) References: <1126236036.30435698.1375898514852.JavaMail.root@sequans.com> From: Avishay Balderman Mime-Version: 1.0 (1.0) In-Reply-To: <1126236036.30435698.1375898514852.JavaMail.root@sequans.com> Date: Thu, 8 Aug 2013 07:51:50 +0300 Subject: Re: Mock pathc question To: Jean-Michel Pichavant Content-Type: multipart/alternative; boundary=047d7b673288ae0bde04e3686a91 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 158 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375937516 news.xs4all.nl 15878 [2001:888:2000:d::a6]:60677 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52166 --047d7b673288ae0bde04e3686a91 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 =D7=91=D7=90=D7=95=D7=92 2013, at 21:01, Jean-Michel Pichavant 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 =3D Client() # result should be 7 print str(client.add(1,34)) def test3(self): client =3D 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. --047d7b673288ae0bde04e3686a91 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi
1) I prefer to use start/stop and not the decorator .
2) mock_play is th= e name of the module where the code belongs

Thanks

= Avishay

Sent from my iPhone<= /div>

On 7 =D7=91=D7=90=D7=95=D7=92 2013, at 21:01, Jean-Michel Pi= chavant <jeanmichel@sequans.co= m> 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 - =C2=A0that patch a= n existing instance of 'Client' - it works
<= blockquote type=3D"cite"> fine.
1) test2= - =C2=A0that tries to patch the 'Client' class. My expectation is<= /span>
that after the patch= ing, every instance of 'Client' will be created
with 'MockClient'. How= ever this is not the case..

Can you ple= ase advice?

Thanks

Avisha= y


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.

*c= ode not tested*

class TestIt(unittest.Test= Case):
=C2=A0=C2=A0=C2=A0def setUp(self):
=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0pass =C2=A0=C2=A0=C2=A0
<= br> =C2=A0=C2=A0=C2=A0@mock.patch(Calc, MockCalc)
= =C2=A0=C2=A0=C2=A0def test2(self):
=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0client =3D Client()
=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0# result should be 7
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0print str(client.add(1,34)= )

=C2=A0=C2=A0=C2=A0def test3(self):
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0client =3D Client()<= /span>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0# result should = be 35 again
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0prin= t str(client.add(1,34))

By the way, what is 'mock_play' in your orig= inal post, could be the reason why you things did go wrong.

JM


-- I= MPORTANT NOTICE:

The contents of this email and any attachments are c= onfidential and may also be privileged. If you are not the intended recipie= nt, please notify the sender immediately and do not disclose the contents t= o any other person, use it for any purpose, or store or copy the informatio= n in any medium. Thank you.
--047d7b673288ae0bde04e3686a91--