Skip to main content

A unittest mock library than understands annotations

Project description

mock_protocol

A unittest mock library than understands protocols and annotations

A drop-in replacement for the builtin python 3.7 unittest.mock module where annotations on spec objects are used to spec mocked attributes and methods.

In addition to the standard unittest.mock functionality, a new from_protocol function is added.

import mock_protocol

m = mock_protocol.from_protocol(C, **kwargs)

is an alias for

m = mock_protocol.create_autospec(C, spec_set=True, instance=True, **kwargs)

which is essentially the same as the unittest.mock method of the same name

m = unittest.mock.create_autospec(C, spec_set=True, instance=True, **kwargs)

except that type-hints are used to spec the mocked attributes.

When you have type-hinted code like this

class A(object):
    b: str

    def __init__(self, b: str):
        self.b = b

    def foo(self, bar: int) -> list:
        return ['x']*bar


class C(object):
    a: int

    def __init__(self, a: int):
        self.a = a

    def bar(self, foo: str) -> A:
        return A(foo)

Then mocking them with mock_protocol will use the type hints as specs for child mocks.

m = mock_protocol.from_protocol(C)
a = m.bar('hello')

# the following are all True
isinstance(a, A)
isinstance(a.b, str)
isinstance(m.a, int)
isinstance(a.foo(4), list)

mock_protocol also understands common typing types such as Dict and Tuple and Optional

import typing

class D(object):
    a: typing.Optional[int]
    d: typing.Dict[str, int]
    e: typing.Optional[typing.AnyStr]
    t: typing.Tuple[int, float]

    def __init__(self, a: int):
        self.a = a

    def bar(self, foo: str) -> typing.Optional[A]:
        return A(foo)

m = mock_protocol.from_protocol(D)
a = m.bar('hello')

# the following are all True
isinstance(m, D)
isinstance(a, A)
isinstance(m.a, int)
isinstance(m.d, dict)
isinstance(m.e, str)
isinstance(m.t, tuple)

Project details


Download files

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

Source Distribution

mock_protocol-1.0.0.tar.gz (23.2 kB view details)

Uploaded Source

File details

Details for the file mock_protocol-1.0.0.tar.gz.

File metadata

  • Download URL: mock_protocol-1.0.0.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.6

File hashes

Hashes for mock_protocol-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2ecf2d7664c3e5de2cc570a108fcbd1b27dad82b40e639f6be7df51bad3e95dc
MD5 7e592f06df760f8d9b476d27c54352cc
BLAKE2b-256 81e0d43526598fcf67cbe4f8c4366b7dcf77018a60bb8cd3440a9dd6ed264aad

See more details on using hashes here.

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