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


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

Mock return_value

Started byDaniel <daniel.watrous@gmail.com>
First post2015-03-09 08:27 -0700
Last post2015-03-09 13:10 -0700
Articles 4 — 2 participants

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


Contents

  Mock return_value Daniel <daniel.watrous@gmail.com> - 2015-03-09 08:27 -0700
    Re: Mock return_value Daniel <daniel.watrous@gmail.com> - 2015-03-09 08:34 -0700
    Re: Mock return_value Ben Finney <ben+python@benfinney.id.au> - 2015-03-10 06:10 +1100
    Re: Mock return_value Daniel <daniel.watrous@gmail.com> - 2015-03-09 13:10 -0700

#87215 — Mock return_value

FromDaniel <daniel.watrous@gmail.com>
Date2015-03-09 08:27 -0700
SubjectMock return_value
Message-ID<e32b0311-3b7b-4f7b-8f3b-212adae7f776@googlegroups.com>
I have a dao.py module with a dao class declared and I want to use mock to set a return value for a dao function, dao.execute_ldap_search().

import mock
import unittest
import model, dao

class TestPeopleDAO(unittest.TestCase):

    ldap_person_response = SOME_DICT

    @mock.patch('dao.dao')
    def test_find_by_last_first_comma(self, mock_dao):
        # setup the mock
        mock_dao.execute_ldap_search.return_value = self.ldap_person_response

        persondao = dao.person_dao()
        persondao.find_by_last_first_comma('name', '', '')
        self.assertEqual(len(persondao),1,"Wrong number of objects returned.")
        self.assertEqual(persondao[0].givenName, "FirstName", "Failed to parse response.")

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

When I run this, it fails when calling execute_ldap_search because it really calls it. I was expecting it to just return the value I specified. What am I doing wrong?

[toc] | [next] | [standalone]


#87216

FromDaniel <daniel.watrous@gmail.com>
Date2015-03-09 08:34 -0700
Message-ID<4f6e3e71-4403-44e6-850a-d6d35b6a514b@googlegroups.com>
In reply to#87215
I found that the following change worked:

    @mock.patch('dao.dao.execute_ldap_search')
    def test_find_by_last_first_comma(self, mock_dao):
        # setup the mock
        mock_dao.return_value = self.ldap_person_response

Daniel

On Monday, March 9, 2015 at 10:28:20 AM UTC-5, Daniel wrote:
> I have a dao.py module with a dao class declared and I want to use mock to set a return value for a dao function, dao.execute_ldap_search().
> 
> import mock
> import unittest
> import model, dao
> 
> class TestPeopleDAO(unittest.TestCase):
> 
>     ldap_person_response = SOME_DICT
> 
>     @mock.patch('dao.dao')
>     def test_find_by_last_first_comma(self, mock_dao):
>         # setup the mock
>         mock_dao.execute_ldap_search.return_value = self.ldap_person_response
> 
>         persondao = dao.person_dao()
>         persondao.find_by_last_first_comma('name', '', '')
>         self.assertEqual(len(persondao),1,"Wrong number of objects returned.")
>         self.assertEqual(persondao[0].givenName, "FirstName", "Failed to parse response.")
> 
> if __name__ == '__main__':
>     unittest.main()
> 
> When I run this, it fails when calling execute_ldap_search because it really calls it. I was expecting it to just return the value I specified. What am I doing wrong?

[toc] | [prev] | [next] | [standalone]


#87221

FromBen Finney <ben+python@benfinney.id.au>
Date2015-03-10 06:10 +1100
Message-ID<mailman.224.1425928229.21433.python-list@python.org>
In reply to#87215
Daniel <daniel.watrous@gmail.com> writes:

> I have a dao.py module with a dao class declared and I want to use
> mock to set a return value for a dao function,
> dao.execute_ldap_search().

You have found a change which worked, but you might not have understood
why yet.

The documentation for the ‘unittest.mock’ library covers the wrinkles of
where to patch an object:

    26.4.3.8. Where to patch

    patch() works by (temporarily) changing the object that a name
    points to with another one. There can be many names pointing to any
    individual object, so for patching to work you must ensure that you
    patch the name used by the system under test.

    The basic principle is that you patch where an object is looked up,
    which is not necessarily the same place as where it is defined. A
    couple of examples will help to clarify this.

    […]

    <URL:https://docs.python.org/3/library/unittest.mock.html#where-to-patch>

Good hunting!

-- 
 \     “There will be a Moscow Exhibition of the Arts by 15,000 Soviet |
  `\     Republic painters and sculptors. These were executed over the |
_o__)               past two years.” —newspaper article, Soviet Weekly |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#87224

FromDaniel <daniel.watrous@gmail.com>
Date2015-03-09 13:10 -0700
Message-ID<6a9ae382-5fef-4470-b98c-190bf9063282@googlegroups.com>
In reply to#87215
On Monday, March 9, 2015 at 10:28:20 AM UTC-5, Daniel wrote:
> I have a dao.py module with a dao class declared and I want to use mock to set a return value for a dao function, dao.execute_ldap_search().
> 
> import mock
> import unittest
> import model, dao
> 
> class TestPeopleDAO(unittest.TestCase):
> 
>     ldap_person_response = SOME_DICT
> 
>     @mock.patch('dao.dao')
>     def test_find_by_last_first_comma(self, mock_dao):
>         # setup the mock
>         mock_dao.execute_ldap_search.return_value = self.ldap_person_response
> 
>         persondao = dao.person_dao()
>         persondao.find_by_last_first_comma('name', '', '')
>         self.assertEqual(len(persondao),1,"Wrong number of objects returned.")
>         self.assertEqual(persondao[0].givenName, "FirstName", "Failed to parse response.")
> 
> if __name__ == '__main__':
>     unittest.main()
> 
> When I run this, it fails when calling execute_ldap_search because it really calls it. I was expecting it to just return the value I specified. What am I doing wrong?

I ended up refactoring to setup patching once for a test class.

https://docs.python.org/3.5/library/unittest.mock-examples.html#applying-the-same-patch-to-every-test-method

[toc] | [prev] | [standalone]


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


csiph-web