Python SharedMock

Build Status Coverage Status Docs Status

A multiprocessing-friendly Python mock object

Getting started

The SharedMock object has an interface similar to Python’s own unittest.mock.Mock. The main difference is that the state of a SharedMock object is shared among subprocesses. This allows you to easily test interactions of subprocesses with your mock instance.

from sharedmock.mock import SharedMock

sharedmock = SharedMock()

subprocess = mp.Process(target=sharedmock,
                        args=('fancyArg',))
subprocess.start()
subprocess.join()
subprocess.terminate()

expected_calls = [call('fancyArg')]
sharedmock.assert_has_calls(expected_calls,
                            same_order=False)

If the SharedMock were to be replaced by a unittest.mock.Mock in the example above, the assertion would fail. The interaction with the unittest.mock.Mock object would not get propagated to your test code:

E               AssertionError: Calls not found.
E               Expected: [call('fancyArg')]
E               Actual: []

Source

The entire source code is available on GitHub.

Table of Contents

sharedmock package

Subpackages

sharedmock.test package

Submodules
sharedmock.test.test_asserters module
sharedmock.test.test_mock module
Module contents

Submodules

sharedmock.asserters module

sharedmock.asserters.assert_calls_equal(expected, actual)[source]

Check whether the given mock object (or mock method) calls are equal and return a nicely formatted message.

sharedmock.asserters.assert_calls_equal_unsorted(expected, actual)[source]

Raises an AssertionError if the two iterables do not contain the same items.

The order of the items is ignored

sharedmock.asserters.raise_calls_differ_error(expected, actual)[source]

Raise an AssertionError with pretty print format for the given expected and actual mock calls in order to ensure consistent print style for better readability.

sharedmock.mock module

Module contents

Indices and tables