Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

mock is a Python module that provides a core Mock class. It is intended to reduce the need for creating a host of trivial stubs throughout your test suite. After performing an action, you can make assertions about which methods / attributes were used and arguments they were called with. You can also specify return values and set needed attributes in the normal way.

This approach is inspired by the testing patterns used at Resolver Systems.

mock is tested on Python versions 2.4-2.7 and Python 3.

The mock module also provides utility functions / objects to assist with testing, particularly monkey patching.

Mock is very easy to use and is designed for use with unittest. Mock is based on the ‘action -> assertion’ pattern instead of ‘record -> replay’ used by many mocking frameworks. See the mock documentation for full details.

Mock objects create all attributes and methods as you access them and store details of how they have been used. You can configure them, to specify return values or limit what attributes are available, and then make assertions about how they have been used:

>>> from mock import Mock
>>> real = ProductionClass()
>>> real.method = Mock()
>>> real.method.return_value = 3
>>>
>>> real.method(3, 4, 5, key='value')
3
>>> real.method.assert_called_with(3, 4, 5, key='value')

The patch decorator / context manager makes it easy to mock classes or objects in a module under test. The object you specify will be replaced with a mock (or other object) during the test and restored when the test ends:

@patch('test_module.ClassName1')
@patch('test_module.ClassName2')
def test_method(self, MockClass1, MockClass2):
    test_module.ClassName1()
    test_module.ClassName2()

    self.assertTrue(MockClass1.called, "ClassName1 not patched")
    self.assertTrue(MockClass2.called, "ClassName2 not patched")

Mock now supports the mocking of Python magic methods. The easiest way of using magic methods is with the MagicMock class. It allows you to do things like:

>>> from mock import MagicMock
>>> mock = MagicMock()
>>> mock.__str__.return_value = 'foobarbaz'
>>> str(mock)
'foobarbaz'
>>> mock.__str__.assert_called_with()

Mock allows you to assign functions (or other Mock instances) to magic methods and they will be called appropriately. The MagicMock class is just a Mock variant that has all of the magic methods pre-created for you (well - all the useful ones anyway).

The following is an example of using magic methods with the ordinary Mock class:

>>> from mock import Mock
>>> mock = Mock()
>>> mock.__str__ = Mock()
>>> mock.__str__.return_value = 'wheeeeee'
>>> str(mock)
'wheeeeee'

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

mock-0.7.0b1.zip (227.0 kB view details)

Uploaded Source

mock-0.7.0b1.tar.gz (190.9 kB view details)

Uploaded Source

File details

Details for the file mock-0.7.0b1.zip.

File metadata

  • Download URL: mock-0.7.0b1.zip
  • Upload date:
  • Size: 227.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mock-0.7.0b1.zip
Algorithm Hash digest
SHA256 cff3bab0d0d65e7b3643d7b5b62f2e5484e5180997ba6c66a40140b2496c1e22
MD5 ba5124c632e0394a6e9ec25d681fd374
BLAKE2b-256 ab951d25cd0a0f5164aca0882d8fa35d76b4c6cb466c4ac30c08519684e17661

See more details on using hashes here.

File details

Details for the file mock-0.7.0b1.tar.gz.

File metadata

  • Download URL: mock-0.7.0b1.tar.gz
  • Upload date:
  • Size: 190.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mock-0.7.0b1.tar.gz
Algorithm Hash digest
SHA256 137c5b376f85aa73c8c6730a09fabc13c7bde07c043e20622d288619687ee1fa
MD5 a21ba6804db7e30c7b5dc7025f72abac
BLAKE2b-256 a9c3f205ab82a23ddfbd115020730fec77faffe17b9d47cdbfb9a70f4ed57eb4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page