Python mock

From wikinotes
Revision as of 14:56, 29 August 2020 by Will (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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