python/mock-1.0.0/PKG-INFO

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 Metadata-Version: 1.0
michael@0 2 Name: mock
michael@0 3 Version: 1.0.0
michael@0 4 Summary: A Python Mocking and Patching Library for Testing
michael@0 5 Home-page: http://www.voidspace.org.uk/python/mock/
michael@0 6 Author: Michael Foord
michael@0 7 Author-email: michael@voidspace.org.uk
michael@0 8 License: UNKNOWN
michael@0 9 Description: mock is a library for testing in Python. It allows you to replace parts of
michael@0 10 your system under test with mock objects and make assertions about how they
michael@0 11 have been used.
michael@0 12
michael@0 13 mock is now part of the Python standard library, available as `unittest.mock <
michael@0 14 http://docs.python.org/py3k/library/unittest.mock.html#module-unittest.mock>`_
michael@0 15 in Python 3.3 onwards.
michael@0 16
michael@0 17 mock provides a core `MagicMock` class removing the need to create a host of
michael@0 18 stubs throughout your test suite. After performing an action, you can make
michael@0 19 assertions about which methods / attributes were used and arguments they were
michael@0 20 called with. You can also specify return values and set needed attributes in
michael@0 21 the normal way.
michael@0 22
michael@0 23 mock is tested on Python versions 2.4-2.7 and Python 3. mock is also tested
michael@0 24 with the latest versions of Jython and pypy.
michael@0 25
michael@0 26 The mock module also provides utility functions / objects to assist with
michael@0 27 testing, particularly monkey patching.
michael@0 28
michael@0 29 * `PDF documentation for 1.0 beta 1
michael@0 30 <http://www.voidspace.org.uk/downloads/mock-1.0.0.pdf>`_
michael@0 31 * `mock on google code (repository and issue tracker)
michael@0 32 <http://code.google.com/p/mock/>`_
michael@0 33 * `mock documentation
michael@0 34 <http://www.voidspace.org.uk/python/mock/>`_
michael@0 35 * `mock on PyPI <http://pypi.python.org/pypi/mock/>`_
michael@0 36 * `Mailing list (testing-in-python@lists.idyll.org)
michael@0 37 <http://lists.idyll.org/listinfo/testing-in-python>`_
michael@0 38
michael@0 39 Mock is very easy to use and is designed for use with
michael@0 40 `unittest <http://pypi.python.org/pypi/unittest2>`_. Mock is based on
michael@0 41 the 'action -> assertion' pattern instead of 'record -> replay' used by many
michael@0 42 mocking frameworks. See the `mock documentation`_ for full details.
michael@0 43
michael@0 44 Mock objects create all attributes and methods as you access them and store
michael@0 45 details of how they have been used. You can configure them, to specify return
michael@0 46 values or limit what attributes are available, and then make assertions about
michael@0 47 how they have been used::
michael@0 48
michael@0 49 >>> from mock import Mock
michael@0 50 >>> real = ProductionClass()
michael@0 51 >>> real.method = Mock(return_value=3)
michael@0 52 >>> real.method(3, 4, 5, key='value')
michael@0 53 3
michael@0 54 >>> real.method.assert_called_with(3, 4, 5, key='value')
michael@0 55
michael@0 56 `side_effect` allows you to perform side effects, return different values or
michael@0 57 raise an exception when a mock is called::
michael@0 58
michael@0 59 >>> mock = Mock(side_effect=KeyError('foo'))
michael@0 60 >>> mock()
michael@0 61 Traceback (most recent call last):
michael@0 62 ...
michael@0 63 KeyError: 'foo'
michael@0 64 >>> values = {'a': 1, 'b': 2, 'c': 3}
michael@0 65 >>> def side_effect(arg):
michael@0 66 ... return values[arg]
michael@0 67 ...
michael@0 68 >>> mock.side_effect = side_effect
michael@0 69 >>> mock('a'), mock('b'), mock('c')
michael@0 70 (3, 2, 1)
michael@0 71 >>> mock.side_effect = [5, 4, 3, 2, 1]
michael@0 72 >>> mock(), mock(), mock()
michael@0 73 (5, 4, 3)
michael@0 74
michael@0 75 Mock has many other ways you can configure it and control its behaviour. For
michael@0 76 example the `spec` argument configures the mock to take its specification from
michael@0 77 another object. Attempting to access attributes or methods on the mock that
michael@0 78 don't exist on the spec will fail with an `AttributeError`.
michael@0 79
michael@0 80 The `patch` decorator / context manager makes it easy to mock classes or
michael@0 81 objects in a module under test. The object you specify will be replaced with a
michael@0 82 mock (or other object) during the test and restored when the test ends::
michael@0 83
michael@0 84 >>> from mock import patch
michael@0 85 >>> @patch('test_module.ClassName1')
michael@0 86 ... @patch('test_module.ClassName2')
michael@0 87 ... def test(MockClass2, MockClass1):
michael@0 88 ... test_module.ClassName1()
michael@0 89 ... test_module.ClassName2()
michael@0 90
michael@0 91 ... assert MockClass1.called
michael@0 92 ... assert MockClass2.called
michael@0 93 ...
michael@0 94 >>> test()
michael@0 95
michael@0 96 .. note::
michael@0 97
michael@0 98 When you nest patch decorators the mocks are passed in to the decorated
michael@0 99 function in the same order they applied (the normal *python* order that
michael@0 100 decorators are applied). This means from the bottom up, so in the example
michael@0 101 above the mock for `test_module.ClassName2` is passed in first.
michael@0 102
michael@0 103 With `patch` it matters that you patch objects in the namespace where they
michael@0 104 are looked up. This is normally straightforward, but for a quick guide
michael@0 105 read `where to patch
michael@0 106 <http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch>`_.
michael@0 107
michael@0 108 As well as a decorator `patch` can be used as a context manager in a with
michael@0 109 statement::
michael@0 110
michael@0 111 >>> with patch.object(ProductionClass, 'method') as mock_method:
michael@0 112 ... mock_method.return_value = None
michael@0 113 ... real = ProductionClass()
michael@0 114 ... real.method(1, 2, 3)
michael@0 115 ...
michael@0 116 >>> mock_method.assert_called_once_with(1, 2, 3)
michael@0 117
michael@0 118 There is also `patch.dict` for setting values in a dictionary just during the
michael@0 119 scope of a test and restoring the dictionary to its original state when the
michael@0 120 test ends::
michael@0 121
michael@0 122 >>> foo = {'key': 'value'}
michael@0 123 >>> original = foo.copy()
michael@0 124 >>> with patch.dict(foo, {'newkey': 'newvalue'}, clear=True):
michael@0 125 ... assert foo == {'newkey': 'newvalue'}
michael@0 126 ...
michael@0 127 >>> assert foo == original
michael@0 128
michael@0 129 Mock supports the mocking of Python magic methods. The easiest way of
michael@0 130 using magic methods is with the `MagicMock` class. It allows you to do
michael@0 131 things like::
michael@0 132
michael@0 133 >>> from mock import MagicMock
michael@0 134 >>> mock = MagicMock()
michael@0 135 >>> mock.__str__.return_value = 'foobarbaz'
michael@0 136 >>> str(mock)
michael@0 137 'foobarbaz'
michael@0 138 >>> mock.__str__.assert_called_once_with()
michael@0 139
michael@0 140 Mock allows you to assign functions (or other Mock instances) to magic methods
michael@0 141 and they will be called appropriately. The MagicMock class is just a Mock
michael@0 142 variant that has all of the magic methods pre-created for you (well - all the
michael@0 143 useful ones anyway).
michael@0 144
michael@0 145 The following is an example of using magic methods with the ordinary Mock
michael@0 146 class::
michael@0 147
michael@0 148 >>> from mock import Mock
michael@0 149 >>> mock = Mock()
michael@0 150 >>> mock.__str__ = Mock(return_value = 'wheeeeee')
michael@0 151 >>> str(mock)
michael@0 152 'wheeeeee'
michael@0 153
michael@0 154 For ensuring that the mock objects your tests use have the same api as the
michael@0 155 objects they are replacing, you can use "auto-speccing". Auto-speccing can
michael@0 156 be done through the `autospec` argument to patch, or the `create_autospec`
michael@0 157 function. Auto-speccing creates mock objects that have the same attributes
michael@0 158 and methods as the objects they are replacing, and any functions and methods
michael@0 159 (including constructors) have the same call signature as the real object.
michael@0 160
michael@0 161 This ensures that your mocks will fail in the same way as your production
michael@0 162 code if they are used incorrectly::
michael@0 163
michael@0 164 >>> from mock import create_autospec
michael@0 165 >>> def function(a, b, c):
michael@0 166 ... pass
michael@0 167 ...
michael@0 168 >>> mock_function = create_autospec(function, return_value='fishy')
michael@0 169 >>> mock_function(1, 2, 3)
michael@0 170 'fishy'
michael@0 171 >>> mock_function.assert_called_once_with(1, 2, 3)
michael@0 172 >>> mock_function('wrong arguments')
michael@0 173 Traceback (most recent call last):
michael@0 174 ...
michael@0 175 TypeError: <lambda>() takes exactly 3 arguments (1 given)
michael@0 176
michael@0 177 `create_autospec` can also be used on classes, where it copies the signature of
michael@0 178 the `__init__` method, and on callable objects where it copies the signature of
michael@0 179 the `__call__` method.
michael@0 180
michael@0 181 The distribution contains tests and documentation. The tests require
michael@0 182 `unittest2 <http://pypi.python.org/pypi/unittest2>`_ to run.
michael@0 183
michael@0 184 Docs from the in-development version of `mock` can be found at
michael@0 185 `mock.readthedocs.org <http://mock.readthedocs.org>`_.
michael@0 186
michael@0 187 Keywords: testing,test,mock,mocking,unittest,patching,stubs,fakes,doubles
michael@0 188 Platform: UNKNOWN
michael@0 189 Classifier: Development Status :: 5 - Production/Stable
michael@0 190 Classifier: Environment :: Console
michael@0 191 Classifier: Intended Audience :: Developers
michael@0 192 Classifier: License :: OSI Approved :: BSD License
michael@0 193 Classifier: Programming Language :: Python
michael@0 194 Classifier: Programming Language :: Python :: 2
michael@0 195 Classifier: Programming Language :: Python :: 3
michael@0 196 Classifier: Programming Language :: Python :: 2.4
michael@0 197 Classifier: Programming Language :: Python :: 2.5
michael@0 198 Classifier: Programming Language :: Python :: 2.6
michael@0 199 Classifier: Programming Language :: Python :: 2.7
michael@0 200 Classifier: Programming Language :: Python :: 3.1
michael@0 201 Classifier: Programming Language :: Python :: 3.2
michael@0 202 Classifier: Programming Language :: Python :: Implementation :: CPython
michael@0 203 Classifier: Programming Language :: Python :: Implementation :: PyPy
michael@0 204 Classifier: Programming Language :: Python :: Implementation :: Jython
michael@0 205 Classifier: Operating System :: OS Independent
michael@0 206 Classifier: Topic :: Software Development :: Libraries
michael@0 207 Classifier: Topic :: Software Development :: Libraries :: Python Modules
michael@0 208 Classifier: Topic :: Software Development :: Testing

mercurial