ANN: Mock 0.5.0 Release
fuzzyman at gmail.com
Sun Apr 19 23:43:37 CEST 2009
Mock 0.5.0 has just been released.
* Mock Homepage http://www.voidspace.org.uk/python/mock/
* Download Mock 0.5.0 release (zip) http://www.voidspace.org.uk/downloads/mock-0.5.0.zip
* Mock Documentation as a PDF http://www.voidspace.org.uk/downloads/mock.pdf
* Mock entry on PyPI http://pypi.python.org/pypi/mock/
* Repository and Issue Tracker on Google Code http://code.google.com/p/mock/
This *isn't* backwards compatible as it cleans up the API in a few
ways, but they're all good changes I promise.
One of the new features is that the Mock class now supports wrapping
objects; using the ``wraps`` keyword.
Mock is a library for the creation of simple mock objects that track
how they are used so that you can make assertions. It uses the action -
> assertion pattern rather than the record -> replay pattern. Action -
> assertion puts your tests after you have used the objects, which
seems more natural and means that you can make assertions about only
the behavior you are interested in. Mock also contains two decorators
(``patch`` and ``patch_object``) which make it easy to safely mock out
dependencies in the module under test purely within the scope of the
test itself (unpatching is done automatically on exit whether or not
the test passes). One of the changes in this release is that these
decorators also become context managers allowing them to be used with
the 'with statement'.
Mock can be installed with:
The changelog for all changes in this release is:
* Made DEFAULT part of the public api.
* Documentation built with Sphinx.
* ``side_effect`` is now called with the same arguments as the mock is
called with and
if returns a non-DEFAULT value that is automatically set as the
* ``wraps`` keyword argument used for wrapping objects (and passing
calls through to the wrapped object).
* ``Mock.reset`` renamed to ``Mock.reset_mock``, as reset is a common
* ``patch`` / ``patch_object`` are now context managers and can be
used with ``with``.
* A new 'create' keyword argument to patch and patch_object that
allows them to patch
(and unpatch) attributes that don't exist. (Potentially unsafe to
use - it can allow
you to have tests that pass when they are testing an API that
doesn't exist - use at
your own risk!)
* The methods keyword argument to Mock has been removed and merged
with spec. The spec
argument can now be a list of methods or an object to take the spec
* Nested patches may now be applied in a different order (created
in the opposite order). This is actually a bugfix.
* patch and patch_object now take a spec keyword argument. If spec is
passed in as 'True' then the Mock created will take the object it is
as its spec object. If the object being replaced is a class, then
value for the mock will also use the class as a spec.
* A Mock created without a spec will not attempt to mock any magic
methods / attributes
(they will raise an ``AttributeError`` instead).
Many thanks to all those who gave feedback, feature requests and
What *isn't* in 0.5.0 is support for mocking magic methods. I do have
a technique in mind for this, which I implemented for the container
methods. It is very clean, but different from the pattern used to mock
out other methods. As I'm not currently using it I'm going to wait
until I need it and see if it works well in practise.
If you're interested in trying it, the code (with full documentation)
in a 'magics branch':
More information about the Python-announce-list