[TIP] Mock returns a mock using autospec

John Wong gokoproject at gmail.com
Tue Sep 11 13:52:16 PDT 2012


This is literally the whole unittest.


    import unittest
    from mock import patch
    from yaml import safe_load

    from graphyte.tests.do_config import read_config
    from graphyte.graphapp.model.codebundle import CodeBundle

    test_config = read_config()
    This is literally the whole unittest.


    import unittest
    from mock import patch
    from yaml import safe_load

    from graphyte.tests.do_config import read_config
    from graphyte.graphapp.model.codebundle import CodeBundle

    test_config = read_config()

    class TestCodebundleBBDSModel(unittest.TestCase):
        def setUp(self):
            self.patcher1 =
patch('graphyte.graphapp.model.codebundle.CodeBundle', autospec=True)
            self.cb = self.patcher1.start()

        def tearDown(self):
            self.patcher1.stop()

        def test_bbds_list(self):
            self.cb.return_value.tempdir = test_config['unit-fixtures']
            bbds_list = self.cb.return_value.bbds_list.return_value
            expected = ['123', '456']

            self.assertEqual(expected, bbds_list)

instead I get

      File "test_codebundles.py", line 26, in test_bbds_list
        self.assertEqual(expected, bbds_list)
    AssertionError: ['123', '456'] != <MagicMock
name='CodeBundle().bbds_list()' id='173575436'>


I tried with and without `return_value` and with and without `()`, but it
yields nothing useful.

Here is the system under test

    def bbds_list(self):
        assert self.setup_tempdir()
        datalist_path = os.path.join(self.tempdir, '.datalist.yaml')
        with open(datalist_path, 'r') as f:
            configs = safe_load(f)
            bbids = configs['bbids']
            return list(bbids.iterkeys())

Another problem I encouter is I cannot make the attribute `self.tempdir` on
the fly.

Any idea what's wrong with my test code?

    class TestCodebundleBBDSModel(unittest.TestCase):
        def setUp(self):
            self.patcher1 =
patch('graphyte.graphapp.model.codebundle.CodeBundle', autospec=True)
            self.cb = self.patcher1.start()

        def tearDown(self):
            self.patcher1.stop()

        def test_bbds_list(self):
            self.cb.return_value.tempdir = test_config['unit-fixtures']
            bbds_list = self.cb.return_value.bbds_list.return_value
            expected = ['123', '456']

            self.assertEqual(expected, bbds_list)

instead I get

      File "test_codebundles.py", line 26, in test_bbds_list
        self.assertEqual(expected, bbds_list)
    AssertionError: ['123', '456'] != <MagicMock
name='CodeBundle().bbds_list()' id='173575436'>


I tried with and without `return_value` and with and without `()`, but it
yields nothing useful.

Here is the system under test

    def bbds_list(self):
        assert self.setup_tempdir()
        datalist_path = os.path.join(self.tempdir, '.datalist.yaml')
        with open(datalist_path, 'r') as f:
            configs = safe_load(f)
            bbids = configs['bbids']
            return list(bbids.iterkeys())

Another problem I encounter is I cannot make the attribute `self.tempdir`
on the fly. I want to asself self.cb the object an attribute called
`self.tempdir`.

Any idea what's wrong with my test code? I am confused why we always need
to use `return_value`.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20120911/db697e8b/attachment.htm>


More information about the testing-in-python mailing list