FAQ
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




code below:
-------------------------------------


import mock
import sys
import unittest


SEVEN = 7


class Calc:
     def __init__(self):
         print self.__class__
     def add(self,a,b):
         return a + b


class MockCalc:
     def __init__(self):
         print self.__class__
     def add(self,a,b):
         return SEVEN


class Client:
     def __init__(self):
         self.calc = Calc()
     def add(self,a,b):
         return self.calc.add(a,b)


class TestIt(unittest.TestCase):
     def setUp(self):
         pass


     def test2(self):
         '''Mocking the Calc and replace it with MockCalc.'''
         print " \ntest2 "
         my_mock = mock.patch('mock_play.Calc',create=True, new=MockCalc)
         my_mock.start()
         # client should be created with 'MockCalc'
         client = Client()
         # result should be 7
         print str(client.add(1,34))
         my_mock.stop()
         # result should be 35 again
         print str(client.add(1,34))


     def test1(self):
         '''Mocking the client instance.'''
         print " test1 "
         client = Client()
         my_mock = mock.patch.object(client, 'calc', new_callable=MockCalc)
         # result should be 35
         print str(client.add(1,34))
         # now i want to switch to the MockCalc
         my_mock.start()
         # result should be 7
         print str(client.add(1,34))
         my_mock.stop()
         # result should be 35 again
         print str(client.add(1,34))


if __name__ == "__main__":
     unittest.main()

Search Discussions

  • Jean-Michel Pichavant at Aug 7, 2013 at 6:01 pm
    ----- 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.
  • Avishay Balderman at Aug 8, 2013 at 4:51 am
    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 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>
  • Jean-Michel Pichavant at Aug 8, 2013 at 10:04 am
    ----- Mail original -----

    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



    You should not neeed to refer to the Calc class using mock_play since it is defined in the very same file (module).
    Possibly mock_play.Calc and Calc are 2 different classes, hence you're not patching the good one.


    Note how you patch "mock_play.Calc" and then instanciate using "self.calc = Calc()"


    right before
    my_mock = mock.patch('mock_play.Calc',create=True, new=MockCalc)


    add
    print mock_play.Calc
    print Calc




    and verify that they are both the very same object.


    Cheers,


    JM




    PS : Please do not top post in this list




    -- 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.
  • Balderman at Aug 8, 2013 at 4:15 pm

    On Thursday, August 8, 2013 12:04:38 PM UTC+2, Jean-Michel Pichavant wrote:
    ----- Mail original -----
    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

    You should not neeed to refer to the Calc class using mock_play since it is defined in the very same file (module).
    Possibly mock_play.Calc and Calc are 2 different classes, hence you're not patching the good one.

    Note how you patch "mock_play.Calc" and then instanciate using "self.calc = Calc()"

    right before
    my_mock = mock.patch('mock_play.Calc',create=True, new=MockCalc)

    add
    print mock_play.Calc
    print Calc


    and verify that they are both the very same object.

    Cheers,

    JM


    PS : Please do not top post in this list


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

    Hi
    Here is a working solution: http://stackoverflow.com/questions/18121084/mock-patch-does-not-work-properly


    thanks

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 7, '13 at 1:39p
activeAug 8, '13 at 4:15p
posts5
users2
websitepython.org

People

Translate

site design / logo © 2021 Grokbase