Mock objects on Python

Update: We have developed pyDoubles, a test doubles framework for Python which supports mock objects among other test doubles. We no longer use mocker (described in this post).

The best article I've found on Mock Objects is again written by one of my favorite authors, Martin Fowler: Mocks aren't Stubs.

On Python, I've been trying several frameworks and eventually I've choosen Mocker because it is an active project, it is documented and it follows the same behavior than EasyMock.

Mock objects focus on behavior rather than state so it leads you to Behavior Driven Development, a particular flavor of Test Driven Development. I still don't know if I prefer BDD or classic TDD, I am just learning but in some cases I like to use mock objects, specially on Python.

  • Reason 1: Interpreted languages like Python, don't have a compiler to tell you that API has changed so that if you make changes in a class and don't look for references you get an runtime exception.
  • Reason 2: Third party tools might change their API as well, so you get to reason 1.
  • Reason 3: You can write down the expected behavior for the methods that are supposed to behave always in a certain manner.

My first test case mocks Django models to make sure the relationships work always as I want:

  1. from django.db import models
  2. from siga.managementServices import *
  3. import mocker
  4. import unittest
  5. import os
  6.  
  7. class managementServicesTests(mocker.MockerTestCase):
  8.  
  9. def testMockMarkConfigAsParticular(self):
  10. stubServer = Server()
  11. mockServer = self.mocker.proxy(stubServer)
  12. mockConfigFile = self.mocker.proxy(ConfigFile)
  13. mockConfigFile.particularServers.add(mockServer)
  14. self.mocker.result(None)
  15. mockConfigFile.save()
  16. self.mocker.result(None)
  17. self.mocker.replay()
  18. service = ServersManagementService()
  19. service.markConfigAsParticular(mockServer, mockConfigFile)
  20. self.mocker.verify()

The models are Server and ConfigFile. They have a many2many relationship. The test above says:
There is a Server object and a ConfigFile object. Calling service.markConfigAsParticular and passing in the Server and the ConfigFile instances, will result in two calls in the ConfigFile instance (particularServers.add(server) and save()), both returning None.
Once the expectations are defined, we turn mocker into replay mode and call the real method, the one we're testing:

  1. def markConfigAsParticular(self, server, configFile):
  2. retInfo = []
  3. if server == None:
  4. retInfo = ['1', 'Server does not exist']
  5. elif configFile == None:
  6. retInfo = ['2', 'Configuration file does not exist']
  7. else:
  8. configFile.particularServers.add(server)
  9. configFile.save()
  10. retInfo = ['0', 'Success']
  11. return retInfo

In the test method we not only define which methods are going to be invoked but also the order and their parameters. Another nice effect is that data is not persisted into database as the objects in the test are mocks.

I still don't know how much useful is this as I am actually rewriting the method somehow. I'd say if the method was more complex the test would be more useful because it just keeps control of the important behavior, no matter what the remaining implementation is.
As Martin says, I guess one need to work long time following this methodology to realize how productive it is. That happens with TDD but I trust on it for large projects. Time will say it mock objects are good for my current project.

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • http://carlosble.com Carlos Ble
  • http://www.seosolutions.com.pl pozycjonowanie stron ?ód?

    It is perfect time to make some plans for the future and it’s time to be happy. I’ve read this post and if I could I want to suggest you some interesting things or advice. Perhaps you could write next articles referring to this article. I wish to read even more things about it!

  • http://www.alegroalegro.pl Pokrowce Samochodowe Allegro

    Excellent post. I was checking constantly this blog and I am impressed! Extremely useful information particularly the last part 🙂 I care for such information much. I was looking for this particular information for a very long time. Thank you and good luck.

  • http://aist.tomsk.ru/wp-includes/guest/index.php?showforum=1;http://bela-rent.ru/includes/guest/index.php?showforum=1;http://dniester.org.ua/includes/guest/index.php?showforum=1;http://map.khortytsali lisa

    This really answered my drawback, thanks!