Skip to main content

Mocks that whitelist its interface

Project description

Build Status # 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.


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


pip install sealedmock


Given you have a file like:

import urllib2

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

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

You can write a test like:

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

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 beaviour.

This is recursive so you can write:

def test_recursive(mock):
        sample = sample_code.SampleCodeClass()
        mock.secret.call1.call2.call3.return_value = 1

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

It also prevents typos on tests if used like this:

def test_recursive(mock):
        sample = sample_code.SampleCodeClass()


        mock.sealed = True
        # 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.

Files for sealedmock, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size sealedmock-0.2.0.tar.gz (3.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page