Skip to main content

A minimal Python mocking library

Project description

A minimal Python mocking library

# Why Sleuth?

Most Python projects use mock, which became unittest.mock in Python 3. So why write Sleuth?

Firstly, Sleuth has a different take on mocking than Mock does which can essentially be summarised as “mock functions not objects”. Mocking functions is an explicit, readable and predictable thing to do and leads to clean test cases and loosely-coupled code.

Secondly, Sleuth aims to have a simple and expressive API. With Mock sometimes it’s difficult to tell what’s going on and which arguments you need to pass to mock things as you need. Sleuth breaks mocking functions into a set of clearly defined use cases:

  • watch: You want to see how a function is called, but not change its behaviour.

  • switch: You want to replace a function with another one for testing.

  • detonate: You want to throw an exception when the function is called.

  • fake: You want to replace the function with another one which returns a particular value when called.

# Usage

Watch calls with sleuth.watch

with sleuth.watch(“some.path.to.thing”) as mock:

result = thing(1, a=2) self.assertTrue(mock.called) self.assertEqual(1, mock.call_count) self.assertEqual([((1,), {a:2})], mock.calls) self.assertEqual(result, mock.call_returns[0])

Replace functions with sleuth.switch…

with sleuth.switch(“some.path.to.thing”, lambda x: ‘something’) as mock:

thing(1, a=2) self.assertTrue(mock.called) self.assertTrue([‘something’], mock.call_returns)

Cause functions to throw exceptions with sleuth.detonate:

with sleuth.detonate(“some.path.to.thing”, exception=ValueError):
try:

thing(1, a=2)

except ValueError:

pass

Or…

with sleuth.detonate(“some.path.to.thing”, exception=ValueError(“Some custom thingy”)):
try:

thing(1, a=2)

except ValueError:

pass

Replace functions with a specific return value with sleuth.fake

with sleuth.fake(“some.path.to.thing”, return_value=1) as mock:

thing(1, a=2)

self.assertEqual([1], mock.call_returns)

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

sleuth-mock-0.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sleuth_mock-0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file sleuth-mock-0.1.tar.gz.

File metadata

  • Download URL: sleuth-mock-0.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.4rc1

File hashes

Hashes for sleuth-mock-0.1.tar.gz
Algorithm Hash digest
SHA256 86a825ffd230d8c6ccbce3167ab549dba100940b9be0fe9039d0617048ba7944
MD5 5256ce0c17278ffc926fe1240229fcc6
BLAKE2b-256 425b7705f5e35a65aba8eafb518bc3cc6ff0453c3758e1f42c08a69caffeffbd

See more details on using hashes here.

File details

Details for the file sleuth_mock-0.1-py3-none-any.whl.

File metadata

  • Download URL: sleuth_mock-0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.4rc1

File hashes

Hashes for sleuth_mock-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e5c43c53645f3e00aba88ba73198efb2316bb1302f41f1784fb86e0438f5dd88
MD5 ea2c041870a2b723fbb6803f582adff1
BLAKE2b-256 d5f182e5ee2a9f78bde051d241fbfd992dae52564b42f8620109305d22a898c9

See more details on using hashes here.

Supported by

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