Mock - Mocking and Testing Library

Author:Michael Foord
Version:Mock 0.6.0
Date:2009/08/22
Homepage:Mock Homepage
Documentation:PDF Documentation
License:BSD License
Support:Testing in Python Email List
Contact:fuzzyman@voidspace.org.uk

Introduction

mock provides a core mock.Mock class that is intended to reduce the need to create a host of trivial stubs throughout your test suite. After performing an action, you can make assertions about which methods / attributes were used and arguments they were called with. You can also specify return values and set specific attributes in the normal way.

It also provides a mock.patch() decorator that handles patching module and class level attributes within the scope of a test, along with mock.sentinel for creating unique objects.

Most mocking libraries follow the ‘record -> replay’ pattern of mocking. Mock uses the ‘action -> assertion’ pattern, which is more readable and intuitive; particularly when working with the Python unittest module. For a discussion of the merits of the two approaches, see Mocking, Patching, Stubbing: all that Stuff.

Downloading

The current version is 0.6.0, dated 23rd August 2009. Mock is still experimental; the API may change. If you find bugs or have suggestions for improvements / extensions then please email me.

You can checkout the latest development version from the Google Code Subversion repository with the following command:

svn checkout http://mock.googlecode.com/svn/trunk/ mock-read-only

Mock is registered with PyPi (Mock on PyPi). If you have pip, setuptools or Distribute you can install mock with:

easy_install mock
pip mock

Tests

Mock uses the discover module to collect and run its tests. discover.py is included in the subversion repository so from a fresh checkout you can execute the following to run all the tests:

python discover.py

On Python 2.4 you will see one failure - testwith.py will fail to import as it uses the with statement which is invalid syntax under Python 2.4.

Table Of Contents

Next topic

The Mock Class

This Page