[TIP] mock 0.8 beta 3
Michael Foord
michael at voidspace.org.uk
Wed Aug 17 16:17:30 PDT 2011
Hello all,
I've released mock 0.8 beta 3. You can download it it or install it with:
pip install -U mock==dev
* http://www.voidspace.org.uk/python/mock
* http://www.voidspace.org.uk/downloads/mock-0.8.0beta3.tar.gz
mock is a library for testing in Python. It allows you to replace parts of your system under test with mock objects. The latest stable release is 0.7.2, which you can download from pypi.
mock 0.8 beta 3 is now feature complete, and is hopefully the code that will be released as the final version. All that is left is documentation work, so now is the ideal time to try the beta!
mock 0.8 is a big release with lots of improvements. You can read a summary of the new features in the following blog entries:
* http://www.voidspace.org.uk/python/weblog/arch_d7_2011_08_13.shtml#e1224
* http://www.voidspace.org.uk/python/weblog/arch_d7_2011_07_30.shtml#e1223
* http://www.voidspace.org.uk/python/weblog/arch_d7_2011_07_16.shtml#e1220
* http://www.voidspace.org.uk/python/weblog/arch_d7_2011_06_11.shtml#e1219
The important new features in 0.8 include:
* Addition of `mock_calls` list for *all* calls (including magic methods and
chained calls)
* `patch` and `patch.object` now create a `MagicMock` instead of a `Mock` by
default
* The patchers (`patch`, `patch.object` and `patch.dict`), plus `Mock` and
`MagicMock`, take arbitrary keyword arguments for configuration
* New mock assert methods `assert_any_call` and `assert_has_calls`
* Implemented auto-speccing (recursive, lazy speccing of mocks with mocked
signatures for functions/methods), as the `autospec` argument to `patch`
* Added the `create_autospec` function for manually creating 'auto-specced'
mocks
* `patch.multiple` for doing multiple patches in a single call, using keyword
arguments
* Setting `side_effect` to an iterable will cause calls to the mock to return
the next value from the iterable
* New `new_callable` argument to `patch` and `patch.object` allowing you to
pass in a class or callable object (instead of `MagicMock`) that will be
called to replace the object being patched
* Added `ANY` for ignoring arguments in `assert_called_with` calls
* Addition of `call` helper object
* Improved repr for mocks
* Improved repr for `Mock.call_args` and entries in `Mock.call_args_list`,
`Mock.method_calls` and `Mock.mock_calls`
Other changes include:
* New mock method `configure_mock` for setting attributes and return values /
side effects on the mock and its attributes
* Addition of `NonCallableMock` and `NonCallableMagicMock`, mocks without a
`__call__` method
* Addition of `mock_add_spec` method for adding (or changing) a spec on an
existing mock
* Protocol methods on `MagicMock` are magic mocks, and are created lazily on
first lookup. This means the result of calling a protocol method is a
MagicMock instead of a Mock as it was previously
* In Python 2.6 or more recent, `dir` on a mock will report all the dynamically
created attributes (or the full list of attributes if there is a spec) as
well as all the mock methods and attributes.
* Module level `FILTER_DIR` added to control whether `dir(mock)` filters
private attributes. `True` by default. Note that `vars(Mock())` can still be
used to get all instance attributes and `dir(type(Mock())` will still return
all the other attributes (irrespective of `FILTER_DIR`)
* `patch.TEST_PREFIX` for controlling how patchers recognise test methods when
used to decorate a class
* Support for using Java exceptions as a `side_effect` on Jython
* `Mock` call lists (`call_args_list`, `method_calls` & `mock_calls`) are now
custom list objects that allow membership tests for "sub lists" and have
a nicer representation if you `str` or `print` them
* Mocks attached as attributes or return values to other mocks have calls
recorded in `method_calls` and `mock_calls` of the parent (unless a name is
already set on the child)
* Improved failure messages for `assert_called_with` and
`assert_called_once_with`
* Added the Mock API (`assert_called_with` etc) to functions created by
`mocksignature`
* Tuples as well as lists can be used to specify allowed methods for `spec` &
`spec_set` arguments
* Calling `stop` on an unstarted patcher fails with a more meaningful error
message
* BUGFIX: an error creating a patch, with nested patch decorators, won't leave
patches in place
* BUGFIX: `__truediv__` and `__rtruediv__` not available as magic methods on
mocks in Python 3
* BUGFIX: `assert_called_with` / `assert_called_once_with` can be used with
`self` as a keyword argument
* BUGFIX: when patching a class with an explicit spec / spec_set (not a
boolean) it applies "spec inheritance" to the return value of the created
mock (the "instance")
* BUGFIX: remove the `__unittest` marker causing traceback truncation
Please try out the new beta and report any issues or give feedback on the new features.
All the best,
Michael Foord
--
http://www.voidspace.org.uk/
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing
http://www.sqlite.org/different.html
More information about the testing-in-python
mailing list