Skip to main content

Mocks that whitelist its interface

Project description

Build Status Coverage Status Code Health PyPI Version

Sealed Mock

Whitelist the attributes/methods of your mocks instead of just letting it create new mock objects.

SealedMock allows specify when you are done defining the mock, ensuring that any unexpected call to the mock is cached.

Sample:

import sealedmock
m = sealedmock.SealedMock()
m.method1.return_value.attr1.method2.return_value = 1
m.sealed = True
m.method1().attr1.method2()
# 1
m.method1().attr2
# Exception: SealedMockAttributeAccess: mock.method1().attr2

Install

pip install sealedmock

Usage

Given you have a file like:

import urllib2

class SampleCodeClass(object):
    """This is sample code"""
    def calling_urlopen(self):
        return urllib2.urlopen("http://chooserandom.com")

    def calling_splithost(self):
        return urllib2.splithost("//host:port/path")

You can write a test like:

from sealedmock import patch
@patch("tests.sample_code.urllib2")
def test_using_decorator(mock):
        sample = sample_code.SampleCodeClass()
        mock.urlopen.return_value = 2

        mock.sealed = True  # No new attributes can be declared

        # calling urlopen succeeds as mock.urlopen has been defined
        assert sample.calling_urlopen()

        # This will fail as mock.splithost has not been defined
        sample.calling_splithost()

If you use an common Mock the second part will pass as it will create a mock for you and return it. With SealedMock you can choose when to stop that behaviour.

This is recursive so you can write:

@patch("sample_code.secret")
def test_recursive(mock):
        sample = sample_code.SampleCodeClass()
        mock.secret.call1.call2.call3.return_value = 1
        mock.sealed = True  # No new attributes can be declared

        # If secret is not used as specified above it will fail
        # ex: if do_stuff also calls secret.call1.call9
        sample.do_stuff()

It also prevents typos on tests if used like this:

@patch("sample_code.secret")
def test_recursive(mock):
        sample = sample_code.SampleCodeClass()

        sample.do_stuff()

        mock.sealed = True
        mock.asert_called_with(1)
        # Note the typo in asert (should be assert)
        # Sealed mock will rise, normal mock won't

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

sealedmock-0.4.2.tar.gz (3.6 kB view details)

Uploaded Source

Built Distributions

sealedmock-0.4.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

sealedmock-0.4.2-py2-none-any.whl (5.9 kB view details)

Uploaded Python 2

File details

Details for the file sealedmock-0.4.2.tar.gz.

File metadata

  • Download URL: sealedmock-0.4.2.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sealedmock-0.4.2.tar.gz
Algorithm Hash digest
SHA256 961de6b7bdf10fd9063411f23b34f345dd6835d56031690c4355e149db3ce8de
MD5 cdf2a32cf371d8ab7cd140dd407ad5c7
BLAKE2b-256 08f9c03a235928253163135d7c8d5f38d9a0af9da36450a56326acc151699bd2

See more details on using hashes here.

File details

Details for the file sealedmock-0.4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sealedmock-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c098fa23f1afc2273706c0c2088160af00db2cfb598b45a7459a96e6c5929e55
MD5 4eafad92d92e886425676b01689527ff
BLAKE2b-256 9e4765b6468e7b20e4106e10ce1e51b286d5fe6880121db24a53498814f00ed3

See more details on using hashes here.

File details

Details for the file sealedmock-0.4.2-py2-none-any.whl.

File metadata

File hashes

Hashes for sealedmock-0.4.2-py2-none-any.whl
Algorithm Hash digest
SHA256 1ba5fad9c6bfe61d4f3e51cebf151c09f21c2b37251a684272eabb8f9da76abb
MD5 2180b08204a306f6d21306c83afb14dd
BLAKE2b-256 b9158ae6ce925f4b1d5a04aba050ea884263f2e0f8bdd76abb41ac69710e802d

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