Skip to main content

License & Repository

MiniMock is by Ian Bicking with substantial contributions by Mike Beachy. It is licensed under an MIT-style license.

Introduction

minimock is a simple library for doing Mock objects with doctest. When using doctest, mock objects can be very simple.

Here’s an example of something we might test, a simple email sender:

>>> import smtplib
>>> def send_email(from_addr, to_addr, subject, body):
...     conn = smtplib.SMTP('localhost')
...     msg = 'To: %s\nFrom: %s\nSubject: %s\n\n%s' % (
...         to_addr, from_addr, subject, body)
...     conn.sendmail(from_addr, [to_addr], msg)
...     conn.quit()

Now we want to make a mock smtplib.SMTP object. We’ll have to inject our mock into the smtplib module:

>>> smtplib.SMTP = Mock('smtplib.SMTP')
>>> smtplib.SMTP.mock_returns = Mock('smtp_connection')

Now we do the test:

>>> send_email('ianb@colorstudy.com', 'joe@example.com',
...            'Hi there!', 'How is it going?')
Called smtplib.SMTP('localhost')
Called smtp_connection.sendmail(
    'ianb@colorstudy.com',
    ['joe@example.com'],
    'To: joe@example.com\nFrom: ianb@colorstudy.com\nSubject: Hi there!\n\nHow is it going?')
Called smtp_connection.quit()

Voila! We’ve tested implicitly that no unexpected methods were called on the object. We’ve also tested the arguments that the mock object got. We’ve provided fake return calls (for the smtplib.SMTP() constructor). These are all the core parts of a mock library. The implementation is simple because most of the work is done by doctest.

Controlling Mocks

Mock objects have several attributes, all of which you can set when instantiating the object. To avoid name collision, all the attributes start with mock_, while the constructor arguments don’t.

name:

The name of the object, used when printing out messages. In the example about it was 'smtplib.SMTP'.

returns:

When this object is called, it will return this value. By default it is None.

returns_iter:

Alternately, you can give an iterable of return results, like returns_iter=[1, 2, 3]; on each subsequent call it will return the next value.

returns_func:

If given, this will be called to get the return value. In essence, this function will be the real implementation of the method.

raises:

An exception (instance or class) that will be raised when this object is called.

So to create an object that always raises ValueError, do:

>>> dummy_module = Mock('mylibrary')
>>> dummy_module.invalid_func.raises = ValueError

Creating Mocks

Every attribute of a mock object will itself be another mock object, unless you specifically set it to something else. For instance, you can do:

>>> from minimock import Mock
>>> dummy_module = Mock('mylibrary')
>>> dummy_module.CONSTANT = 1

Then the CONSTANT value will persist. But you can also traverse to whatever object you want, and you will get another mock object.

Another technique for creating a mock object is the mock(...) function. This works like:

>>> from minimock import mock
>>> import os.path
>>> mock('os.path.isfile', returns=True)

This looks up the os.path.isfile object, and changes it to a mock object. Any keyword arguments you give (like returns=True in this example) will be used to create the mock object; you can also give a mock_obj keyword argument to pass in a mock object you’ve already created.

This function looks in the calling function to figure out what to replace (os.path.isfile in the example). You must import the proper modules first. Alternately you can pass in a dictionary like [locals(), globals()] for it to use for lookup.

Metadata

Release files for MiniMock 0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for MiniMock 0.8
File Size Uploaded
MiniMock-0.8.tar.gz 5.7 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for MiniMock 0.8
File Interpreter ABI Platform
MiniMock-0.8-py2.5.egg Legacy Egg format - - Details
MiniMock-0.8-py2.4.egg Legacy Egg format - - Details

Total release size: 23.7 kB

Release files / MiniMock-0.8.tar.gz

Download URL MiniMock-0.8.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a6b9d74500bdfa82627486388f2e48f4e57138d89e22b33329390043c3d2199a
BLAKE2b-256 checksum
How to use checksums
24376aeb4231a18f4cf0c08d06b18242e5624c6dbc4d2d7b5ae79daf05f0ad3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / MiniMock-0.8-py2.5.egg

Download URL MiniMock-0.8-py2.5.egg
Size 8.9 kB
Tags Egg
SHA-256 checksum
How to use checksums
d3fdc9af3012345890dfc1b79d6012f1bca98b1e2fb7431111226c734212a1ea
BLAKE2b-256 checksum
How to use checksums
8ae45560b68269d5131f08222ce3941696e941634ebe941362b4058005ed25a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / MiniMock-0.8-py2.4.egg

Download URL MiniMock-0.8-py2.4.egg
Size 9.1 kB
Tags Egg
SHA-256 checksum
How to use checksums
939639096ce501a2fe18317e0723000e1f505e3701dd84796f3b82afea0d1798
BLAKE2b-256 checksum
How to use checksums
52a4308f0fdba9b7b0ddba0dfde2acb187f8fb029a2651ba53d7d196b7b5c549
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

1.2.8

2 release files

1.2.7

1 release file

1.2.6

1 release file

1.2.5

1 release file

1.2.4

1 release file

1.2.3

1 release file

1.2.2

1 release file

1.2.1

1 release file

1.2

1 release file

1.1

1 release file

1.0

1 release file

0.9

1 release file

This release

0.8 This release

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page