Mock pathc question

Jean-Michel Pichavant jeanmichel at sequans.com
Wed Aug 7 14:01:54 EDT 2013


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


More information about the Python-list mailing list