Mocks that whitelist its interface
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file sealedmock-0.4.3.tar.gz
.
File metadata
- Download URL: sealedmock-0.4.3.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ed21242729384fc990c63a503ad273e5ed55d2a99d37b9dea431afe245e1ad2 |
|
MD5 | 08bece6cd91eb68e0c17684b2491911b |
|
BLAKE2b-256 | 71c1ba03b0d0876f6c3726fc023001dae86a4e442540030e5d2a66b3cfb21669 |
File details
Details for the file sealedmock-0.4.3-py3-none-any.whl
.
File metadata
- Download URL: sealedmock-0.4.3-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cc33279168d90f8ac2e7be439181c7d13caa34e03f1e584aa5c0e27d30e3193 |
|
MD5 | 8e0b1a5ac1af5e45aee78e6b5e157245 |
|
BLAKE2b-256 | 83b607bdaa3fee51cbe23466edc435809f2e03e9ba5c4fa3cbb556e4948f05cd |
File details
Details for the file sealedmock-0.4.3-py2-none-any.whl
.
File metadata
- Download URL: sealedmock-0.4.3-py2-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9e5d7dd399a7aac972e7db7b2acda33f94b01ff9572694a5b9a58db6938580f |
|
MD5 | 1f761dae213142e269b43309fe29c399 |
|
BLAKE2b-256 | 70e77088936321038efb7365ecfb70145057f5e99e6061f0fd047e886b707bbd |