Skip to main content

Argument matchers for unittest.mock

Project description

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.

calleee 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 calleee is easy with pip:

$ pip install calleee
calleee supports Python 2.7, 3.6+.

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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

calleee-0.4.0.tar.gz (21.6 kB view details)

Uploaded Source

File details

Details for the file calleee-0.4.0.tar.gz.

File metadata

  • Download URL: calleee-0.4.0.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.3

File hashes

Hashes for calleee-0.4.0.tar.gz
Algorithm Hash digest
SHA256 9d733f2b0fc5b6542420f396b4881b98997c82b9cc0cf15f9795ab18e5f0285e
MD5 3f13d1185259d6ed0468c538c09846ee
BLAKE2b-256 cc4da477e1a0c6e071b5a2901410a47621a177b9a6459afb74f03f2b20eac0ac

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page