Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Type safe mocking for python 3.
NOTE: This library is still in Alpha. Its API and implementation could change.
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.
pip install typemock
Given some class (the implementation of its method is not relevant)
class MyThing:
def multiple_arg(self, prefix: str, number: int) -> str:
pass
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)
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'>
FAQs
Type safe mocking
We found that typemock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.