Skip to main content

typemock

Build Status Documentation Status Pyversions

Type safe mocking for python 3.

  1. Motivation
  2. Installation
  3. Quick example usage

NOTE: This library is still in Alpha. Its API and implementation could change.

Detailed Documentation

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'>

Release files for typemock 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typemock 0.5.1
File Size Uploaded
typemock-0.5.1.tar.gz 16.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for typemock 0.5.1
File Interpreter ABI Platform
typemock-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 40.5 kB

Release files / typemock-0.5.1.tar.gz

Download URL typemock-0.5.1.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
92a2baadbd7e4f905970df78c806b6a591475ac5b73ba5968a457b913c0b9896
BLAKE2b-256 checksum
How to use checksums
8050a4d6d43f2d6ffb759c3a38ffecf3154cd482252457a38d13bf212b69e3e6
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / typemock-0.5.1-py3-none-any.whl

Download URL typemock-0.5.1-py3-none-any.whl
Size 23.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1ebbeab01dff13c9a23ce1fe1e6c88af791a952efe17d39635193ce1c0f5842c
BLAKE2b-256 checksum
How to use checksums
9d11836551cf8b0441544798b8cc396b0efd8095352000da792db8437250d35b
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page