Python Programming, news on the Voidspace Python Projects and all things techie.
Mock 0.7.0 Beta 3 Released and Docs Online
A new release of mock is now available, the third beta of 0.7.0. The headline new feature in 0.7.0 is the ability to mock objects that use Python protocol methods (the magic methods) like context managers and containers. There are quite a few other new features and bug fixes, so if you are using 0.6.0 it is definitely worth looking at the new version.
mock is a library making it really simple to create mock objects for unit and integration tests. It is intended for use with the Python unittest library, but works equally well with nose and py.test. As well as the core mock.Mock class mock provides the astonishingly useful patch decorator / context manager to assist with safely and automatically patching out objects with mocks during the scope of a test.
0.7.0 beta 3 is intended to be the last release before 0.7.0 final, unless new bugs are found of course. Please download it, try it out, and report any issues. All that is left to do for the final release are a few minor documentation tweaks.
Along with the new release I've changed the online documentation to be for 0.7.0. Documentation for 0.6.0, the latest stable release, are also available online:
- mock on PyPI (download here)
- mock 0.7.0b3 online documentation
- mock 0.7.0b3 PDF documentation
- mock 0.6.0 online documentation
There are several changes since beta 2:
- Using spec with MagicMock only pre-creates magic methods in the spec
- Setting a magic method on a mock with a spec can only be done if the spec has that method
- Mocks can now be named (name argument to constructor) and the name is used in the repr
- mocksignature can now be used with classes (signature based on __init__) and callable objects (signature based on __call__)
- Mocks created with a spec can now pass isinstance tests (__class__ returns the type of the spec)
- Default numeric value for MagicMock is 1 rather than zero (because the MagicMock bool defaults to True and 0 is False)
- Improved failure message for Mock.assert_called_with when the mock has not been called at all
- Adding the following to the set of supported magic methods:
- __getformat__ and __setformat__
- pickle methods
- __trunc__, __ceil__ and __floor__
- Added 'build_sphinx' command to setup.py (requires setuptools or distribute) Thanks to Florian Bauer
- with statement tests now skipped on Python 2.4
- Tests require unittest2 to run on Python 2.7 (automated testing across all supported Python versions is done with tox)
- Improved several docstrings and documentation
See the online changelog for the full list of changes in 0.7.0. The major new features are:
- Support for mocking magic methods (plus the addition of the MagicMock class)
- Python 3 compatibility
- Addition of patch.dict (and renaming of patch_object to patch.object)
- Addition of mocksignature
- patch and patch.object now work as class decorators
- "Soft comparisons" (simpler assertions) for call_args, call_args_list and method_calls
- Mocks can be named (more helpful reprs)
- Mocks with a spec can pass an isinstance check
The mock PyPI page shows some simple examples of how to use mock, including using the new magic method support. Mock 0.7.0 beta 2 was only out for less than three months, but was downloaded from PyPI over 10 000 times! Thanks to everyone who has reported issues and provided patches. Special thanks go to Konrad Delong who has joined me in maintaining mock.
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 License.