Python mock

From wikinotes

Mocks are a tool to help you keep your unittests isolated.

  • you can substitute a parameter with a mock object (and assign return values to it's methods)
  • you can patch existing objects or methods to aler their behaviour.

Example

mock  = mock.Mock()
mock.a()
>>> <Mock id='1234567'>
mock.a
>>> <Mock id='7654321'>

mock.a.return_value = 'hi'
mock.a()
>>> 'hi'


Notes

python mock: mock objects
python mock: patching
python mock: assertions
python mock: special-cases