Skip to content
Argument matchers for unittest.mock
Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
callee Bump version to 0.3.1 Dec 24, 2019
docs Documentation fixes Feb 28, 2017
tests Tests for the matcher's repr() Feb 26, 2017
.gitignore Add Travis config Nov 19, 2015
.travis.yml Do not attempt multiple PyPI deploys from Travis Dec 24, 2019
CHANGELOG.md Bump version to 0.3.1 Dec 24, 2019
LICENSE Switch to 3-clause BSD Mar 21, 2016
MANIFEST.in Minor Nov 7, 2015
README.rst Minor fix to README Apr 27, 2016
requirements-dev.txt Upgrade to Invoke 0.13 Aug 8, 2016
requirements-test-py26.txt Add tox support Aug 23, 2015
requirements-test-py32.txt Add tox support Aug 23, 2015
requirements-test.txt Remove TODO attribution Mar 20, 2016
setup.cfg
setup.py
tasks.py Remove the PyPI upload command from `inv upload` Dec 24, 2019
tox.ini Add support for Python 3.7 Nov 18, 2019

README.rst

callee

Argument matchers for unittest.mock

Version Development Status Python versions License Build Status

More robust tests

Python's mocking library (or its backport for Python <3.3) is simple, reliable, and easy to use. But it is also a little lacking when it comes to asserting what calls a mock has received.

You can be either very specific:

my_mock.assert_called_once_with(42, some_foo_object, 'certain string')

or extremely general:

my_mock.assert_called_with(ANY, ANY, ANY)
# passes as long as argument count is the same
The former can make your tests over-specified, and thus fragile.
The latter could make them too broad, missing some erroneous cases and possibly letting your code fail in production.

callee provides argument matchers that allow you to be exactly as precise as you want:

my_mock.assert_called_with(GreaterThan(0), InstanceOf(Foo), String())

without tedious, handcrafted, and poorly readable code that checks call_args or call_args_list:

self.assertGreater(mock.call_args[0][0], 0)
self.assertIsInstance(mock.call_args[0][1], Foo)
self.assertIsInstance(mock.call_args[0][2], str)

It has plenty of matcher types to fit all common and uncommon needs, and you can easily write your own if necessary.

Installation

Installing callee is easy with pip:

$ pip install callee
callee support goes all the way back to Python 2.6.
It also works both with the unittest.mock module from Python 3.3+ or its backport.

API reference

See the documentation for complete reference on the library usage and all available matchers.

Contributing

Contributions are welcome! If you need ideas, head to the issue tracker or search for the various TODOs scattered around the codebase. Or just think what matchers you'd like to add :)

After cloning the repository, this should get you up and running:

# ... create virtualenv as necessary ...
pip install -r requirements-dev.txt
tox

To regenerate documentation and display it in the browser, simply run:

inv docs

Happy hacking!

You can’t perform that action at this time.