LWN.net Logo

Mock 0.5.0 released

From:  Fuzzyman <fuzzyman-AT-gmail.com>
To:  comp-lang-python-announce-AT-moderators.isc.org
Subject:  ANN: Mock 0.5.0 Release
Date:  Sun, 19 Apr 2009 14:43:37 -0700 (PDT)
Message-ID:  <d6a2e875-683c-4274-aebe-5e15298d0257@c36g2000yqn.googlegroups.com>
Archive-link:  Article, Thread

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:

    ``easy_install mock``

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
``mock.return_value``.
* ``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
API name.
* ``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
from.
* Nested patches may now be applied in a different order (created
mocks passed
  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
replacing
  as its spec object. If the object being replaced is a class, then
the return
  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
patches!

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':

* http://code.google.com/p/mock/source/browse/#svn/branches...
--
http://mail.python.org/mailman/listinfo/python-announce-list

        Support the Python Software Foundation:
        http://www.python.org/psf/donations.html



(Log in to post comments)

Mock 0.5.0 released

Posted Apr 23, 2009 13:39 UTC (Thu) by mlawren (subscriber, #10136) [Link]

It would be nice if the short version of these kinds of announcements on the LWN front page could at least include the language that the software is targeted for. Perhaps "mock.Mock class" in this case makes it obvious (to Python programmers) but the rest of us have to click through to the actual article to find out. Perhaps some kind of tagging or multiple category system would make sense?

Python section

Posted Apr 23, 2009 19:14 UTC (Thu) by ddaa (guest, #5338) [Link]

In my opinion, this announcement really belongs in the Python section, as do all language-specific programming "tools".

Python section

Posted Apr 23, 2009 19:53 UTC (Thu) by mlawren (subscriber, #10136) [Link]

I'm not trying to be facetious, but you what you meant to say was all language specific tool announcements should be in language-specific sections (not all of them in the Python section :-)

The only place such categorization appears is on the Development page of the weekly addition. In that case announcements would be relatively clear, provided they are put in the right place. However the problem is much worse on the front page where no such categorization exists.

Copyright © 2009, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds