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


Groups > comp.lang.python > #52166 > unrolled thread

Re: Mock pathc question

Started byAvishay Balderman <balderman@gmail.com>
First post2013-08-08 07:51 +0300
Last post2013-08-08 07:51 +0300
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Mock pathc question Avishay Balderman <balderman@gmail.com> - 2013-08-08 07:51 +0300

#52166 — Re: Mock pathc question

FromAvishay Balderman <balderman@gmail.com>
Date2013-08-08 07:51 +0300
SubjectRe: Mock pathc question
Message-ID<mailman.337.1375937516.1251.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web