Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52144

Re: Mock pathc question

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=924c20f90=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.015
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'attribute': 0.07; 'method.': 0.07; 'tries': 0.07; 'advice?': 0.09; 'below).': 0.09; 'expectation': 0.09; 'method,': 0.09; 'url:voidspace': 0.09; 'subject:question': 0.10; 'def': 0.12; 'client()': 0.16; 'fine.': 0.16; 'url:patch': 0.16; 'print': 0.22; 'rid': 0.24; '(see': 0.26; 'class.': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'patch': 0.29; 'to:no real name:2**1': 0.29; 'code': 0.31; "skip:' 10": 0.31; 'class': 0.32; 'url:python': 0.33; '-----': 0.33; 'could': 0.34; 'created': 0.35; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'unit': 0.37; 'thank': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'you.': 0.62; 'information': 0.63; 'received:194': 0.64; 'to:addr:gmail.com': 0.65; 'notice:': 0.67; 'person,': 0.68; 'privileged.': 0.69; 'disclose': 0.74; 'decorate': 0.84; 'mock': 0.84; 'medium.': 0.91
X-IronPort-AV E=Sophos;i="4.89,834,1367964000"; d="scan'208";a="1768254"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Wed, 7 Aug 2013 20:01:54 +0200 (CEST)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To balderman@gmail.com, python-list@python.org
In-Reply-To <004ef25c-c5c8-42cd-8a4d-6e36a18a34b0@googlegroups.com>
Subject Re: Mock pathc question
MIME-Version 1.0
X-Mailer Zimbra 7.2.4_GA_2900 (ZimbraWebClient - GC28 (Win)/7.2.4_GA_2900)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.324.1375898586.1251.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375898586 news.xs4all.nl 15874 [2001:888:2000:d::a6]:45094
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52144

Show key headers only | View raw


----- 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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Mock pathc question Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-08-07 20:01 +0200

csiph-web