Type safe mocking
Project description
typemock
Type safe mocking for python 3.
NOTE: This library is still in Alpha. Its API and implementation could change.
Motivation
The mocking tools in python are powerful, flexible and useful for building independent tests at various levels.
This flexibility is part of what is considered a strength of the python language, and possibly any dynamically typed language.
However, this flexibility comes at a cost.
It is possible to build mocks which do not conform to the actual behaviour or contract defined by the things they are mocking. Or, for them to be initially correct, and then to go out of sync with actual behaviour and for tests to remain green.
We do not have compile time protections for us doing things with/to things which do not align with the contracts they define and the clients of those contracts expect.
But, now we have type hints. And so, we can explicitly define the contracts of our objects, and, if we have done this, we can mock them in a type safe way as well. This is what this library aims to help achieve. Type safe mocking.
Used in conjunction with mypy, this should result in much more high fidelity independent tests.
Installation
pip install typemock
Quick Example Usage
Given some class (the implementation of its method is not relevant)
class MyThing:
def multiple_arg(self, prefix: str, number: int) -> str:
pass
Mock and verify
We con mock behaviour and verify interactions as follows:
from typemock import tmock, when, verify
expected_result = "a string"
with tmock(MyThing) as my_thing_mock:
when(my_thing_mock.multiple_arg("p", 1)).then_return(expected_result)
actual = my_thing_mock.multiple_arg(
number=1,
prefix="p"
)
assert expected_result == actual
verify(my_thing_mock).multiple_arg("p", 1)
Type safety
And when we try to specify behaviour that does not conform to the contract of the object we are mocking
expected_result = "a string"
with tmock(MyThing) as my_thing_mock:
when(my_thing_mock.multiple_arg(prefix="p", number="should be an int")).then_return(expected_result)
We get an informative error such as
typemock.safety.MockTypeSafetyError: Method: multiple_arg Arg: number must be of type:<class 'int'>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file typemock-0.5.1.tar.gz
.
File metadata
- Download URL: typemock-0.5.1.tar.gz
- Upload date:
- Size: 16.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92a2baadbd7e4f905970df78c806b6a591475ac5b73ba5968a457b913c0b9896 |
|
MD5 | 776971afd0119a43c2b1c1ae6a437dd8 |
|
BLAKE2b-256 | 8050a4d6d43f2d6ffb759c3a38ffecf3154cd482252457a38d13bf212b69e3e6 |
File details
Details for the file typemock-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: typemock-0.5.1-py3-none-any.whl
- Upload date:
- Size: 23.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ebbeab01dff13c9a23ce1fe1e6c88af791a952efe17d39635193ce1c0f5842c |
|
MD5 | d60b7c37683e5e886c42ed3660c1d847 |
|
BLAKE2b-256 | 9d11836551cf8b0441544798b8cc396b0efd8095352000da792db8437250d35b |