Skip to main content

Type safe mocking

None

Project description

typemock

Build Status Documentation Status Pyversions

Type safe mocking for python 3.

With a style inspired by kotlin's mockk library.

  1. Motivation
  2. Quick example usage
  3. Still to do

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.

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)

Things to note:

  • The mocked object must be used as a context manager in order to specify behaviour.
  • You must let the context close in order to use the defined behaviour. 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'>

Things to note:

 - You can only mock objects which have fully type hinted interfaces. You will get Type hint errors otherwise.
 - You will also get type hint errors if you attempt to specify behaviour that returns the incorrect type.


## Still to do

 - Mock properties.
 - Mock classes and instances of classes (currently only classes expected)
 - Check/implement more complex type safety (nested objects)
 - More behaviour specifications (Programmatic responses)
 - Better docs and examples
 - Mock functions?
 - Play nicely with patching static variables in classes and modules?
 - Run mypy linter on typemock codebase.





Project details

None

Download files

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

Source Distribution

typemock-0.3.3.tar.gz (12.6 kB view hashes)

Uploaded Source

Built Distribution

typemock-0.3.3-py3-none-any.whl (16.0 kB view hashes)

Uploaded Python 3

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