Better way to mock class attribute in python unit test Better way to mock class attribute in python unit test python python

Better way to mock class attribute in python unit test


base.Base.assignment is simply replaced with a Mock object. You made it a descriptor by adding a __get__ method.

It's a little verbose and a little unnecessary; you could simply set base.Base.assignment directly:

def test_empty(self):    Base.assignment = {}    assert len(Base().assignment.values()) == 0

This isn't too safe when using test concurrency, of course.

To use a PropertyMock, I'd use:

with patch('base.Base.assignment', new_callable=PropertyMock) as a:    a.return_value = {'a': 1}

or even:

with patch('base.Base.assignment', new_callable=PropertyMock,            return_value={'a': 1}):


Perhaps I'm missing something, but isn't this possible without using PropertyMock?

with mock.patch.object(Base, 'assignment', {'bucket': 'head'}):   # do stuff


To improve readability you can use the @patch decorator:

from mock import patchfrom unittest import TestCasefrom base import Baseclass MyTest(TestCase):    @patch('base.Base.assignment')    def test_empty(self, mock_assignment):        # The `mock_assignment` is a MagicMock instance,        # you can do whatever you want to it.        mock_assignment.__get__.return_value = {}        self.assertEqual(len(Base().assignment.values()), 0)        # ... and so on

You can find more details at http://www.voidspace.org.uk/python/mock/patch.html#mock.patch.