pythonunit-testingmockingmox

Mocking objects in Python


I'm new to Unit testing and mocking objects in Python. I have a function that I need to create a unit test for.

def BuildBall(self, material):
  """Create a Ball from material."""
  result = {}
  for b in xrange(material.ball_size()):
    ball = material.ball(b)
    result[ball.name()] = (ball.name(), ball.description())
  return result

I want to create a dummy object(material) and pass it to BuildBall(), so that I can predict what the result will look like in my unit test.

I'm not too sure how I can pass a dummy object to my method in this case. Any idea if I can use mox to create a dummy object in my case?

Thanks in advance


Solution

  • Take a look at the Python Mocker package: It allows you to easily generate objects that mock a certain functionality for testing. You can find a simple example here.