Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | .. currentmodule:: mock |
michael@0 | 2 | |
michael@0 | 3 | |
michael@0 | 4 | CHANGELOG |
michael@0 | 5 | ========= |
michael@0 | 6 | |
michael@0 | 7 | 2012/10/07 Version 1.0.0 |
michael@0 | 8 | ------------------------ |
michael@0 | 9 | |
michael@0 | 10 | No changes since 1.0.0 beta 1. This version has feature parity with |
michael@0 | 11 | `unittest.mock |
michael@0 | 12 | <http://docs.python.org/py3k/library/unittest.mock.html#module-unittest.mock>`_ |
michael@0 | 13 | in Python 3.3. |
michael@0 | 14 | |
michael@0 | 15 | Full list of changes since 0.8: |
michael@0 | 16 | |
michael@0 | 17 | * `mocksignature`, along with the `mocksignature` argument to `patch`, removed |
michael@0 | 18 | * Support for deleting attributes (accessing deleted attributes will raise an |
michael@0 | 19 | `AttributeError`) |
michael@0 | 20 | * Added the `mock_open` helper function for mocking the builtin `open` |
michael@0 | 21 | * `__class__` is assignable, so a mock can pass an `isinstance` check without |
michael@0 | 22 | requiring a spec |
michael@0 | 23 | * Addition of `PropertyMock`, for mocking properties |
michael@0 | 24 | * `MagicMocks` made unorderable by default (in Python 3). The comparison |
michael@0 | 25 | methods (other than equality and inequality) now return `NotImplemented` |
michael@0 | 26 | * Propagate traceback info to support subclassing of `_patch` by other |
michael@0 | 27 | libraries |
michael@0 | 28 | * `create_autospec` works with attributes present in results of `dir` that |
michael@0 | 29 | can't be fetched from the object's class. Contributed by Konstantine Rybnikov |
michael@0 | 30 | * Any exceptions in an iterable `side_effect` will be raised instead of |
michael@0 | 31 | returned |
michael@0 | 32 | * In Python 3, `create_autospec` now supports keyword only arguments |
michael@0 | 33 | * Added `patch.stopall` method to stop all active patches created by `start` |
michael@0 | 34 | * BUGFIX: calling `MagicMock.reset_mock` wouldn't reset magic method mocks |
michael@0 | 35 | * BUGFIX: calling `reset_mock` on a `MagicMock` created with autospec could |
michael@0 | 36 | raise an exception |
michael@0 | 37 | * BUGFIX: passing multiple spec arguments to patchers (`spec` , `spec_set` and |
michael@0 | 38 | `autospec`) had unpredictable results, now it is an error |
michael@0 | 39 | * BUGFIX: using `spec=True` *and* `create=True` as arguments to patchers could |
michael@0 | 40 | result in using `DEFAULT` as the spec. Now it is an error instead |
michael@0 | 41 | * BUGFIX: using `spec` or `autospec` arguments to patchers, along with |
michael@0 | 42 | `spec_set=True` did not work correctly |
michael@0 | 43 | * BUGFIX: using an object that evaluates to False as a spec could be ignored |
michael@0 | 44 | * BUGFIX: a list as the `spec` argument to a patcher would always result in a |
michael@0 | 45 | non-callable mock. Now if `__call__` is in the spec the mock is callable |
michael@0 | 46 | |
michael@0 | 47 | |
michael@0 | 48 | 2012/07/13 Version 1.0.0 beta 1 |
michael@0 | 49 | -------------------------------- |
michael@0 | 50 | |
michael@0 | 51 | * Added `patch.stopall` method to stop all active patches created by `start` |
michael@0 | 52 | * BUGFIX: calling `MagicMock.reset_mock` wouldn't reset magic method mocks |
michael@0 | 53 | * BUGFIX: calling `reset_mock` on a `MagicMock` created with autospec could |
michael@0 | 54 | raise an exception |
michael@0 | 55 | |
michael@0 | 56 | |
michael@0 | 57 | 2012/05/04 Version 1.0.0 alpha 2 |
michael@0 | 58 | -------------------------------- |
michael@0 | 59 | |
michael@0 | 60 | * `PropertyMock` attributes are now standard `MagicMocks` |
michael@0 | 61 | * `create_autospec` works with attributes present in results of `dir` that |
michael@0 | 62 | can't be fetched from the object's class. Contributed by Konstantine Rybnikov |
michael@0 | 63 | * Any exceptions in an iterable `side_effect` will be raised instead of |
michael@0 | 64 | returned |
michael@0 | 65 | * In Python 3, `create_autospec` now supports keyword only arguments |
michael@0 | 66 | |
michael@0 | 67 | |
michael@0 | 68 | 2012/03/25 Version 1.0.0 alpha 1 |
michael@0 | 69 | -------------------------------- |
michael@0 | 70 | |
michael@0 | 71 | The standard library version! |
michael@0 | 72 | |
michael@0 | 73 | * `mocksignature`, along with the `mocksignature` argument to `patch`, removed |
michael@0 | 74 | * Support for deleting attributes (accessing deleted attributes will raise an |
michael@0 | 75 | `AttributeError`) |
michael@0 | 76 | * Added the `mock_open` helper function for mocking the builtin `open` |
michael@0 | 77 | * `__class__` is assignable, so a mock can pass an `isinstance` check without |
michael@0 | 78 | requiring a spec |
michael@0 | 79 | * Addition of `PropertyMock`, for mocking properties |
michael@0 | 80 | * `MagicMocks` made unorderable by default (in Python 3). The comparison |
michael@0 | 81 | methods (other than equality and inequality) now return `NotImplemented` |
michael@0 | 82 | * Propagate traceback info to support subclassing of `_patch` by other |
michael@0 | 83 | libraries |
michael@0 | 84 | * BUGFIX: passing multiple spec arguments to patchers (`spec` , `spec_set` and |
michael@0 | 85 | `autospec`) had unpredictable results, now it is an error |
michael@0 | 86 | * BUGFIX: using `spec=True` *and* `create=True` as arguments to patchers could |
michael@0 | 87 | result in using `DEFAULT` as the spec. Now it is an error instead |
michael@0 | 88 | * BUGFIX: using `spec` or `autospec` arguments to patchers, along with |
michael@0 | 89 | `spec_set=True` did not work correctly |
michael@0 | 90 | * BUGFIX: using an object that evaluates to False as a spec could be ignored |
michael@0 | 91 | * BUGFIX: a list as the `spec` argument to a patcher would always result in a |
michael@0 | 92 | non-callable mock. Now if `__call__` is in the spec the mock is callable |
michael@0 | 93 | |
michael@0 | 94 | |
michael@0 | 95 | 2012/02/13 Version 0.8.0 |
michael@0 | 96 | ------------------------ |
michael@0 | 97 | |
michael@0 | 98 | The only changes since 0.8rc2 are: |
michael@0 | 99 | |
michael@0 | 100 | * Improved repr of :data:`sentinel` objects |
michael@0 | 101 | * :data:`ANY` can be used for comparisons against :data:`call` objects |
michael@0 | 102 | * The return value of `MagicMock.__iter__` method can be set to |
michael@0 | 103 | any iterable and isn't required to be an iterator |
michael@0 | 104 | |
michael@0 | 105 | Full List of changes since 0.7: |
michael@0 | 106 | |
michael@0 | 107 | mock 0.8.0 is the last version that will support Python 2.4. |
michael@0 | 108 | |
michael@0 | 109 | * Addition of :attr:`~Mock.mock_calls` list for *all* calls (including magic |
michael@0 | 110 | methods and chained calls) |
michael@0 | 111 | * :func:`patch` and :func:`patch.object` now create a :class:`MagicMock` |
michael@0 | 112 | instead of a :class:`Mock` by default |
michael@0 | 113 | * The patchers (`patch`, `patch.object` and `patch.dict`), plus `Mock` and |
michael@0 | 114 | `MagicMock`, take arbitrary keyword arguments for configuration |
michael@0 | 115 | * New mock method :meth:`~Mock.configure_mock` for setting attributes and |
michael@0 | 116 | return values / side effects on the mock and its attributes |
michael@0 | 117 | * New mock assert methods :meth:`~Mock.assert_any_call` and |
michael@0 | 118 | :meth:`~Mock.assert_has_calls` |
michael@0 | 119 | * Implemented :ref:`auto-speccing` (recursive, lazy speccing of mocks with |
michael@0 | 120 | mocked signatures for functions/methods), as the `autospec` argument to |
michael@0 | 121 | `patch` |
michael@0 | 122 | * Added the :func:`create_autospec` function for manually creating |
michael@0 | 123 | 'auto-specced' mocks |
michael@0 | 124 | * :func:`patch.multiple` for doing multiple patches in a single call, using |
michael@0 | 125 | keyword arguments |
michael@0 | 126 | * Setting :attr:`~Mock.side_effect` to an iterable will cause calls to the mock |
michael@0 | 127 | to return the next value from the iterable |
michael@0 | 128 | * New `new_callable` argument to `patch` and `patch.object` allowing you to |
michael@0 | 129 | pass in a class or callable object (instead of `MagicMock`) that will be |
michael@0 | 130 | called to replace the object being patched |
michael@0 | 131 | * Addition of :class:`NonCallableMock` and :class:`NonCallableMagicMock`, mocks |
michael@0 | 132 | without a `__call__` method |
michael@0 | 133 | * Addition of :meth:`~Mock.mock_add_spec` method for adding (or changing) a |
michael@0 | 134 | spec on an existing mock |
michael@0 | 135 | * Protocol methods on :class:`MagicMock` are magic mocks, and are created |
michael@0 | 136 | lazily on first lookup. This means the result of calling a protocol method is |
michael@0 | 137 | a `MagicMock` instead of a `Mock` as it was previously |
michael@0 | 138 | * Addition of :meth:`~Mock.attach_mock` method |
michael@0 | 139 | * Added :data:`ANY` for ignoring arguments in :meth:`~Mock.assert_called_with` |
michael@0 | 140 | calls |
michael@0 | 141 | * Addition of :data:`call` helper object |
michael@0 | 142 | * Improved repr for mocks |
michael@0 | 143 | * Improved repr for :attr:`Mock.call_args` and entries in |
michael@0 | 144 | :attr:`Mock.call_args_list`, :attr:`Mock.method_calls` and |
michael@0 | 145 | :attr:`Mock.mock_calls` |
michael@0 | 146 | * Improved repr for :data:`sentinel` objects |
michael@0 | 147 | * `patch` lookup is done at use time not at decoration time |
michael@0 | 148 | * In Python 2.6 or more recent, `dir` on a mock will report all the dynamically |
michael@0 | 149 | created attributes (or the full list of attributes if there is a spec) as |
michael@0 | 150 | well as all the mock methods and attributes. |
michael@0 | 151 | * Module level :data:`FILTER_DIR` added to control whether `dir(mock)` filters |
michael@0 | 152 | private attributes. `True` by default. |
michael@0 | 153 | * `patch.TEST_PREFIX` for controlling how patchers recognise test methods when |
michael@0 | 154 | used to decorate a class |
michael@0 | 155 | * Support for using Java exceptions as a :attr:`~Mock.side_effect` on Jython |
michael@0 | 156 | * `Mock` call lists (`call_args_list`, `method_calls` & `mock_calls`) are now |
michael@0 | 157 | custom list objects that allow membership tests for "sub lists" and have |
michael@0 | 158 | a nicer representation if you `str` or `print` them |
michael@0 | 159 | * Mocks attached as attributes or return values to other mocks have calls |
michael@0 | 160 | recorded in `method_calls` and `mock_calls` of the parent (unless a name is |
michael@0 | 161 | already set on the child) |
michael@0 | 162 | * Improved failure messages for `assert_called_with` and |
michael@0 | 163 | `assert_called_once_with` |
michael@0 | 164 | * The return value of the :class:`MagicMock` `__iter__` method can be set to |
michael@0 | 165 | any iterable and isn't required to be an iterator |
michael@0 | 166 | * Added the Mock API (`assert_called_with` etc) to functions created by |
michael@0 | 167 | :func:`mocksignature` |
michael@0 | 168 | * Tuples as well as lists can be used to specify allowed methods for `spec` & |
michael@0 | 169 | `spec_set` arguments |
michael@0 | 170 | * Calling `stop` on an unstarted patcher fails with a more meaningful error |
michael@0 | 171 | message |
michael@0 | 172 | * Renamed the internal classes `Sentinel` and `SentinelObject` to prevent abuse |
michael@0 | 173 | * BUGFIX: an error creating a patch, with nested patch decorators, won't leave |
michael@0 | 174 | patches in place |
michael@0 | 175 | * BUGFIX: `__truediv__` and `__rtruediv__` not available as magic methods on |
michael@0 | 176 | mocks in Python 3 |
michael@0 | 177 | * BUGFIX: `assert_called_with` / `assert_called_once_with` can be used with |
michael@0 | 178 | `self` as a keyword argument |
michael@0 | 179 | * BUGFIX: when patching a class with an explicit spec / spec_set (not a |
michael@0 | 180 | boolean) it applies "spec inheritance" to the return value of the created |
michael@0 | 181 | mock (the "instance") |
michael@0 | 182 | * BUGFIX: remove the `__unittest` marker causing traceback truncation |
michael@0 | 183 | * Removal of deprecated `patch_object` |
michael@0 | 184 | * Private attributes `_name`, `_methods`, '_children', `_wraps` and `_parent` |
michael@0 | 185 | (etc) renamed to reduce likelihood of clash with user attributes. |
michael@0 | 186 | * Added license file to the distribution |
michael@0 | 187 | |
michael@0 | 188 | |
michael@0 | 189 | 2012/01/10 Version 0.8.0 release candidate 2 |
michael@0 | 190 | -------------------------------------------- |
michael@0 | 191 | |
michael@0 | 192 | * Removed the `configure` keyword argument to `create_autospec` and allow |
michael@0 | 193 | arbitrary keyword arguments (for the `Mock` constructor) instead |
michael@0 | 194 | * Fixed `ANY` equality with some types in `assert_called_with` calls |
michael@0 | 195 | * Switched to a standard Sphinx theme (compatible with |
michael@0 | 196 | `readthedocs.org <http://mock.readthedocs.org>`_) |
michael@0 | 197 | |
michael@0 | 198 | |
michael@0 | 199 | 2011/12/29 Version 0.8.0 release candidate 1 |
michael@0 | 200 | -------------------------------------------- |
michael@0 | 201 | |
michael@0 | 202 | * `create_autospec` on the return value of a mocked class will use `__call__` |
michael@0 | 203 | for the signature rather than `__init__` |
michael@0 | 204 | * Performance improvement instantiating `Mock` and `MagicMock` |
michael@0 | 205 | * Mocks used as magic methods have the same type as their parent instead of |
michael@0 | 206 | being hardcoded to `MagicMock` |
michael@0 | 207 | |
michael@0 | 208 | Special thanks to Julian Berman for his help with diagnosing and improving |
michael@0 | 209 | performance in this release. |
michael@0 | 210 | |
michael@0 | 211 | |
michael@0 | 212 | 2011/10/09 Version 0.8.0 beta 4 |
michael@0 | 213 | ------------------------------- |
michael@0 | 214 | |
michael@0 | 215 | * `patch` lookup is done at use time not at decoration time |
michael@0 | 216 | * When attaching a Mock to another Mock as a magic method, calls are recorded |
michael@0 | 217 | in mock_calls |
michael@0 | 218 | * Addition of `attach_mock` method |
michael@0 | 219 | * Renamed the internal classes `Sentinel` and `SentinelObject` to prevent abuse |
michael@0 | 220 | * BUGFIX: various issues around circular references with mocks (setting a mock |
michael@0 | 221 | return value to be itself etc) |
michael@0 | 222 | |
michael@0 | 223 | |
michael@0 | 224 | 2011/08/15 Version 0.8.0 beta 3 |
michael@0 | 225 | ------------------------------- |
michael@0 | 226 | |
michael@0 | 227 | * Mocks attached as attributes or return values to other mocks have calls |
michael@0 | 228 | recorded in `method_calls` and `mock_calls` of the parent (unless a name is |
michael@0 | 229 | already set on the child) |
michael@0 | 230 | * Addition of `mock_add_spec` method for adding (or changing) a spec on an |
michael@0 | 231 | existing mock |
michael@0 | 232 | * Improved repr for `Mock.call_args` and entries in `Mock.call_args_list`, |
michael@0 | 233 | `Mock.method_calls` and `Mock.mock_calls` |
michael@0 | 234 | * Improved repr for mocks |
michael@0 | 235 | * BUGFIX: minor fixes in the way `mock_calls` is worked out, |
michael@0 | 236 | especially for "intermediate" mocks in a call chain |
michael@0 | 237 | |
michael@0 | 238 | |
michael@0 | 239 | 2011/08/05 Version 0.8.0 beta 2 |
michael@0 | 240 | ------------------------------- |
michael@0 | 241 | |
michael@0 | 242 | * Setting `side_effect` to an iterable will cause calls to the mock to return |
michael@0 | 243 | the next value from the iterable |
michael@0 | 244 | * Added `assert_any_call` method |
michael@0 | 245 | * Moved `assert_has_calls` from call lists onto mocks |
michael@0 | 246 | * BUGFIX: `call_args` and all members of `call_args_list` are two tuples of |
michael@0 | 247 | `(args, kwargs)` again instead of three tuples of `(name, args, kwargs)` |
michael@0 | 248 | |
michael@0 | 249 | |
michael@0 | 250 | 2011/07/25 Version 0.8.0 beta 1 |
michael@0 | 251 | ------------------------------- |
michael@0 | 252 | |
michael@0 | 253 | * `patch.TEST_PREFIX` for controlling how patchers recognise test methods when |
michael@0 | 254 | used to decorate a class |
michael@0 | 255 | * `Mock` call lists (`call_args_list`, `method_calls` & `mock_calls`) are now |
michael@0 | 256 | custom list objects that allow membership tests for "sub lists" and have |
michael@0 | 257 | an `assert_has_calls` method for unordered call checks |
michael@0 | 258 | * `callargs` changed to *always* be a three-tuple of `(name, args, kwargs)` |
michael@0 | 259 | * Addition of `mock_calls` list for *all* calls (including magic methods and |
michael@0 | 260 | chained calls) |
michael@0 | 261 | * Extension of `call` object to support chained calls and `callargs` for better |
michael@0 | 262 | comparisons with or without names. `call` object has a `call_list` method for |
michael@0 | 263 | chained calls |
michael@0 | 264 | * Added the public `instance` argument to `create_autospec` |
michael@0 | 265 | * Support for using Java exceptions as a `side_effect` on Jython |
michael@0 | 266 | * Improved failure messages for `assert_called_with` and |
michael@0 | 267 | `assert_called_once_with` |
michael@0 | 268 | * Tuples as well as lists can be used to specify allowed methods for `spec` & |
michael@0 | 269 | `spec_set` arguments |
michael@0 | 270 | * BUGFIX: Fixed bug in `patch.multiple` for argument passing when creating |
michael@0 | 271 | mocks |
michael@0 | 272 | * Added license file to the distribution |
michael@0 | 273 | |
michael@0 | 274 | |
michael@0 | 275 | 2011/07/16 Version 0.8.0 alpha 2 |
michael@0 | 276 | -------------------------------- |
michael@0 | 277 | |
michael@0 | 278 | * `patch.multiple` for doing multiple patches in a single call, using keyword |
michael@0 | 279 | arguments |
michael@0 | 280 | * New `new_callable` argument to `patch` and `patch.object` allowing you to |
michael@0 | 281 | pass in a class or callable object (instead of `MagicMock`) that will be |
michael@0 | 282 | called to replace the object being patched |
michael@0 | 283 | * Addition of `NonCallableMock` and `NonCallableMagicMock`, mocks without a |
michael@0 | 284 | `__call__` method |
michael@0 | 285 | * Mocks created by `patch` have a `MagicMock` as the `return_value` where a |
michael@0 | 286 | class is being patched |
michael@0 | 287 | * `create_autospec` can create non-callable mocks for non-callable objects. |
michael@0 | 288 | `return_value` mocks of classes will be non-callable unless the class has |
michael@0 | 289 | a `__call__` method |
michael@0 | 290 | * `autospec` creates a `MagicMock` without a spec for properties and slot |
michael@0 | 291 | descriptors, because we don't know the type of object they return |
michael@0 | 292 | * Removed the "inherit" argument from `create_autospec` |
michael@0 | 293 | * Calling `stop` on an unstarted patcher fails with a more meaningful error |
michael@0 | 294 | message |
michael@0 | 295 | * BUGFIX: an error creating a patch, with nested patch decorators, won't leave |
michael@0 | 296 | patches in place |
michael@0 | 297 | * BUGFIX: `__truediv__` and `__rtruediv__` not available as magic methods on |
michael@0 | 298 | mocks in Python 3 |
michael@0 | 299 | * BUGFIX: `assert_called_with` / `assert_called_once_with` can be used with |
michael@0 | 300 | `self` as a keyword argument |
michael@0 | 301 | * BUGFIX: autospec for functions / methods with an argument named self that |
michael@0 | 302 | isn't the first argument no longer broken |
michael@0 | 303 | * BUGFIX: when patching a class with an explicit spec / spec_set (not a |
michael@0 | 304 | boolean) it applies "spec inheritance" to the return value of the created |
michael@0 | 305 | mock (the "instance") |
michael@0 | 306 | * BUGFIX: remove the `__unittest` marker causing traceback truncation |
michael@0 | 307 | |
michael@0 | 308 | |
michael@0 | 309 | 2011/06/14 Version 0.8.0 alpha 1 |
michael@0 | 310 | -------------------------------- |
michael@0 | 311 | |
michael@0 | 312 | mock 0.8.0 is the last version that will support Python 2.4. |
michael@0 | 313 | |
michael@0 | 314 | * The patchers (`patch`, `patch.object` and `patch.dict`), plus `Mock` and |
michael@0 | 315 | `MagicMock`, take arbitrary keyword arguments for configuration |
michael@0 | 316 | * New mock method `configure_mock` for setting attributes and return values / |
michael@0 | 317 | side effects on the mock and its attributes |
michael@0 | 318 | * In Python 2.6 or more recent, `dir` on a mock will report all the dynamically |
michael@0 | 319 | created attributes (or the full list of attributes if there is a spec) as |
michael@0 | 320 | well as all the mock methods and attributes. |
michael@0 | 321 | * Module level `FILTER_DIR` added to control whether `dir(mock)` filters |
michael@0 | 322 | private attributes. `True` by default. Note that `vars(Mock())` can still be |
michael@0 | 323 | used to get all instance attributes and `dir(type(Mock())` will still return |
michael@0 | 324 | all the other attributes (irrespective of `FILTER_DIR`) |
michael@0 | 325 | * `patch` and `patch.object` now create a `MagicMock` instead of a `Mock` by |
michael@0 | 326 | default |
michael@0 | 327 | * Added `ANY` for ignoring arguments in `assert_called_with` calls |
michael@0 | 328 | * Addition of `call` helper object |
michael@0 | 329 | * Protocol methods on `MagicMock` are magic mocks, and are created lazily on |
michael@0 | 330 | first lookup. This means the result of calling a protocol method is a |
michael@0 | 331 | MagicMock instead of a Mock as it was previously |
michael@0 | 332 | * Added the Mock API (`assert_called_with` etc) to functions created by |
michael@0 | 333 | `mocksignature` |
michael@0 | 334 | * Private attributes `_name`, `_methods`, '_children', `_wraps` and `_parent` |
michael@0 | 335 | (etc) renamed to reduce likelihood of clash with user attributes. |
michael@0 | 336 | * Implemented auto-speccing (recursive, lazy speccing of mocks with mocked |
michael@0 | 337 | signatures for functions/methods) |
michael@0 | 338 | |
michael@0 | 339 | Limitations: |
michael@0 | 340 | |
michael@0 | 341 | - Doesn't mock magic methods or attributes (it creates MagicMocks, so the |
michael@0 | 342 | magic methods are *there*, they just don't have the signature mocked nor |
michael@0 | 343 | are attributes followed) |
michael@0 | 344 | - Doesn't mock function / method attributes |
michael@0 | 345 | - Uses object traversal on the objects being mocked to determine types - so |
michael@0 | 346 | properties etc may be triggered |
michael@0 | 347 | - The return value of mocked classes (the 'instance') has the same call |
michael@0 | 348 | signature as the class __init__ (as they share the same spec) |
michael@0 | 349 | |
michael@0 | 350 | You create auto-specced mocks by passing `autospec=True` to `patch`. |
michael@0 | 351 | |
michael@0 | 352 | Note that attributes that are None are special cased and mocked without a |
michael@0 | 353 | spec (so any attribute / method can be used). This is because None is |
michael@0 | 354 | typically used as a default value for attributes that may be of some other |
michael@0 | 355 | type, and as we don't know what type that may be we allow all access. |
michael@0 | 356 | |
michael@0 | 357 | Note that the `autospec` option to `patch` obsoletes the `mocksignature` |
michael@0 | 358 | option. |
michael@0 | 359 | |
michael@0 | 360 | * Added the `create_autospec` function for manually creating 'auto-specced' |
michael@0 | 361 | mocks |
michael@0 | 362 | * Removal of deprecated `patch_object` |
michael@0 | 363 | |
michael@0 | 364 | |
michael@0 | 365 | 2011/05/30 Version 0.7.2 |
michael@0 | 366 | ------------------------ |
michael@0 | 367 | |
michael@0 | 368 | * BUGFIX: instances of list subclasses can now be used as mock specs |
michael@0 | 369 | * BUGFIX: MagicMock equality / inequality protocol methods changed to use the |
michael@0 | 370 | default equality / inequality. This is done through a `side_effect` on |
michael@0 | 371 | the mocks used for `__eq__` / `__ne__` |
michael@0 | 372 | |
michael@0 | 373 | |
michael@0 | 374 | 2011/05/06 Version 0.7.1 |
michael@0 | 375 | ------------------------ |
michael@0 | 376 | |
michael@0 | 377 | Package fixes contributed by Michael Fladischer. No code changes. |
michael@0 | 378 | |
michael@0 | 379 | * Include template in package |
michael@0 | 380 | * Use isolated binaries for the tox tests |
michael@0 | 381 | * Unset executable bit on docs |
michael@0 | 382 | * Fix DOS line endings in getting-started.txt |
michael@0 | 383 | |
michael@0 | 384 | |
michael@0 | 385 | 2011/03/05 Version 0.7.0 |
michael@0 | 386 | ------------------------ |
michael@0 | 387 | |
michael@0 | 388 | No API changes since 0.7.0 rc1. Many documentation changes including a stylish |
michael@0 | 389 | new `Sphinx theme <https://github.com/coordt/ADCtheme/>`_. |
michael@0 | 390 | |
michael@0 | 391 | The full set of changes since 0.6.0 are: |
michael@0 | 392 | |
michael@0 | 393 | * Python 3 compatibility |
michael@0 | 394 | * Ability to mock magic methods with `Mock` and addition of `MagicMock` |
michael@0 | 395 | with pre-created magic methods |
michael@0 | 396 | * Addition of `mocksignature` and `mocksignature` argument to `patch` and |
michael@0 | 397 | `patch.object` |
michael@0 | 398 | * Addition of `patch.dict` for changing dictionaries during a test |
michael@0 | 399 | * Ability to use `patch`, `patch.object` and `patch.dict` as class decorators |
michael@0 | 400 | * Renamed ``patch_object`` to `patch.object` (``patch_object`` is |
michael@0 | 401 | deprecated) |
michael@0 | 402 | * Addition of soft comparisons: `call_args`, `call_args_list` and `method_calls` |
michael@0 | 403 | now return tuple-like objects which compare equal even when empty args |
michael@0 | 404 | or kwargs are skipped |
michael@0 | 405 | * patchers (`patch`, `patch.object` and `patch.dict`) have start and stop |
michael@0 | 406 | methods |
michael@0 | 407 | * Addition of `assert_called_once_with` method |
michael@0 | 408 | * Mocks can now be named (`name` argument to constructor) and the name is used |
michael@0 | 409 | in the repr |
michael@0 | 410 | * repr of a mock with a spec includes the class name of the spec |
michael@0 | 411 | * `assert_called_with` works with `python -OO` |
michael@0 | 412 | * New `spec_set` keyword argument to `Mock` and `patch`. If used, |
michael@0 | 413 | attempting to *set* an attribute on a mock not on the spec will raise an |
michael@0 | 414 | `AttributeError` |
michael@0 | 415 | * Mocks created with a spec can now pass `isinstance` tests (`__class__` |
michael@0 | 416 | returns the type of the spec) |
michael@0 | 417 | * Added docstrings to all objects |
michael@0 | 418 | * Improved failure message for `Mock.assert_called_with` when the mock |
michael@0 | 419 | has not been called at all |
michael@0 | 420 | * Decorated functions / methods have their docstring and `__module__` |
michael@0 | 421 | preserved on Python 2.4. |
michael@0 | 422 | * BUGFIX: `mock.patch` now works correctly with certain types of objects that |
michael@0 | 423 | proxy attribute access, like the django settings object |
michael@0 | 424 | * BUGFIX: mocks are now copyable (thanks to Ned Batchelder for reporting and |
michael@0 | 425 | diagnosing this) |
michael@0 | 426 | * BUGFIX: `spec=True` works with old style classes |
michael@0 | 427 | * BUGFIX: ``help(mock)`` works now (on the module). Can no longer use ``__bases__`` |
michael@0 | 428 | as a valid sentinel name (thanks to Stephen Emslie for reporting and |
michael@0 | 429 | diagnosing this) |
michael@0 | 430 | * BUGFIX: ``side_effect`` now works with ``BaseException`` exceptions like |
michael@0 | 431 | ``KeyboardInterrupt`` |
michael@0 | 432 | * BUGFIX: `reset_mock` caused infinite recursion when a mock is set as its own |
michael@0 | 433 | return value |
michael@0 | 434 | * BUGFIX: patching the same object twice now restores the patches correctly |
michael@0 | 435 | * with statement tests now skipped on Python 2.4 |
michael@0 | 436 | * Tests require unittest2 (or unittest2-py3k) to run |
michael@0 | 437 | * Tested with `tox <http://pypi.python.org/pypi/tox>`_ on Python 2.4 - 3.2, |
michael@0 | 438 | jython and pypy (excluding 3.0) |
michael@0 | 439 | * Added 'build_sphinx' command to setup.py (requires setuptools or distribute) |
michael@0 | 440 | Thanks to Florian Bauer |
michael@0 | 441 | * Switched from subversion to mercurial for source code control |
michael@0 | 442 | * `Konrad Delong <http://konryd.blogspot.com/>`_ added as co-maintainer |
michael@0 | 443 | |
michael@0 | 444 | |
michael@0 | 445 | 2011/02/16 Version 0.7.0 RC 1 |
michael@0 | 446 | ----------------------------- |
michael@0 | 447 | |
michael@0 | 448 | Changes since beta 4: |
michael@0 | 449 | |
michael@0 | 450 | * Tested with jython, pypy and Python 3.2 and 3.1 |
michael@0 | 451 | * Decorated functions / methods have their docstring and `__module__` |
michael@0 | 452 | preserved on Python 2.4 |
michael@0 | 453 | * BUGFIX: `mock.patch` now works correctly with certain types of objects that |
michael@0 | 454 | proxy attribute access, like the django settings object |
michael@0 | 455 | * BUGFIX: `reset_mock` caused infinite recursion when a mock is set as its own |
michael@0 | 456 | return value |
michael@0 | 457 | |
michael@0 | 458 | |
michael@0 | 459 | 2010/11/12 Version 0.7.0 beta 4 |
michael@0 | 460 | ------------------------------- |
michael@0 | 461 | |
michael@0 | 462 | * patchers (`patch`, `patch.object` and `patch.dict`) have start and stop |
michael@0 | 463 | methods |
michael@0 | 464 | * Addition of `assert_called_once_with` method |
michael@0 | 465 | * repr of a mock with a spec includes the class name of the spec |
michael@0 | 466 | * `assert_called_with` works with `python -OO` |
michael@0 | 467 | * New `spec_set` keyword argument to `Mock` and `patch`. If used, |
michael@0 | 468 | attempting to *set* an attribute on a mock not on the spec will raise an |
michael@0 | 469 | `AttributeError` |
michael@0 | 470 | * Attributes and return value of a `MagicMock` are `MagicMock` objects |
michael@0 | 471 | * Attempting to set an unsupported magic method now raises an `AttributeError` |
michael@0 | 472 | * `patch.dict` works as a class decorator |
michael@0 | 473 | * Switched from subversion to mercurial for source code control |
michael@0 | 474 | * BUGFIX: mocks are now copyable (thanks to Ned Batchelder for reporting and |
michael@0 | 475 | diagnosing this) |
michael@0 | 476 | * BUGFIX: `spec=True` works with old style classes |
michael@0 | 477 | * BUGFIX: `mocksignature=True` can now patch instance methods via |
michael@0 | 478 | `patch.object` |
michael@0 | 479 | |
michael@0 | 480 | |
michael@0 | 481 | 2010/09/18 Version 0.7.0 beta 3 |
michael@0 | 482 | ------------------------------- |
michael@0 | 483 | |
michael@0 | 484 | * Using spec with :class:`MagicMock` only pre-creates magic methods in the spec |
michael@0 | 485 | * Setting a magic method on a mock with a ``spec`` can only be done if the |
michael@0 | 486 | spec has that method |
michael@0 | 487 | * Mocks can now be named (`name` argument to constructor) and the name is used |
michael@0 | 488 | in the repr |
michael@0 | 489 | * `mocksignature` can now be used with classes (signature based on `__init__`) |
michael@0 | 490 | and callable objects (signature based on `__call__`) |
michael@0 | 491 | * Mocks created with a spec can now pass `isinstance` tests (`__class__` |
michael@0 | 492 | returns the type of the spec) |
michael@0 | 493 | * Default numeric value for MagicMock is 1 rather than zero (because the |
michael@0 | 494 | MagicMock bool defaults to True and 0 is False) |
michael@0 | 495 | * Improved failure message for :meth:`~Mock.assert_called_with` when the mock |
michael@0 | 496 | has not been called at all |
michael@0 | 497 | * Adding the following to the set of supported magic methods: |
michael@0 | 498 | |
michael@0 | 499 | - ``__getformat__`` and ``__setformat__`` |
michael@0 | 500 | - pickle methods |
michael@0 | 501 | - ``__trunc__``, ``__ceil__`` and ``__floor__`` |
michael@0 | 502 | - ``__sizeof__`` |
michael@0 | 503 | |
michael@0 | 504 | * Added 'build_sphinx' command to setup.py (requires setuptools or distribute) |
michael@0 | 505 | Thanks to Florian Bauer |
michael@0 | 506 | * with statement tests now skipped on Python 2.4 |
michael@0 | 507 | * Tests require unittest2 to run on Python 2.7 |
michael@0 | 508 | * Improved several docstrings and documentation |
michael@0 | 509 | |
michael@0 | 510 | |
michael@0 | 511 | 2010/06/23 Version 0.7.0 beta 2 |
michael@0 | 512 | ------------------------------- |
michael@0 | 513 | |
michael@0 | 514 | * :func:`patch.dict` works as a context manager as well as a decorator |
michael@0 | 515 | * ``patch.dict`` takes a string to specify dictionary as well as a dictionary |
michael@0 | 516 | object. If a string is supplied the name specified is imported |
michael@0 | 517 | * BUGFIX: ``patch.dict`` restores dictionary even when an exception is raised |
michael@0 | 518 | |
michael@0 | 519 | |
michael@0 | 520 | 2010/06/22 Version 0.7.0 beta 1 |
michael@0 | 521 | ------------------------------- |
michael@0 | 522 | |
michael@0 | 523 | * Addition of :func:`mocksignature` |
michael@0 | 524 | * Ability to mock magic methods |
michael@0 | 525 | * Ability to use ``patch`` and ``patch.object`` as class decorators |
michael@0 | 526 | * Renamed ``patch_object`` to :func:`patch.object` (``patch_object`` is |
michael@0 | 527 | deprecated) |
michael@0 | 528 | * Addition of :class:`MagicMock` class with all magic methods pre-created for you |
michael@0 | 529 | * Python 3 compatibility (tested with 3.2 but should work with 3.0 & 3.1 as |
michael@0 | 530 | well) |
michael@0 | 531 | * Addition of :func:`patch.dict` for changing dictionaries during a test |
michael@0 | 532 | * Addition of ``mocksignature`` argument to ``patch`` and ``patch.object`` |
michael@0 | 533 | * ``help(mock)`` works now (on the module). Can no longer use ``__bases__`` |
michael@0 | 534 | as a valid sentinel name (thanks to Stephen Emslie for reporting and |
michael@0 | 535 | diagnosing this) |
michael@0 | 536 | * Addition of soft comparisons: `call_args`, `call_args_list` and `method_calls` |
michael@0 | 537 | now return tuple-like objects which compare equal even when empty args |
michael@0 | 538 | or kwargs are skipped |
michael@0 | 539 | * Added docstrings. |
michael@0 | 540 | * BUGFIX: ``side_effect`` now works with ``BaseException`` exceptions like |
michael@0 | 541 | ``KeyboardInterrupt`` |
michael@0 | 542 | * BUGFIX: patching the same object twice now restores the patches correctly |
michael@0 | 543 | * The tests now require `unittest2 <http://pypi.python.org/pypi/unittest2>`_ |
michael@0 | 544 | to run |
michael@0 | 545 | * `Konrad Delong <http://konryd.blogspot.com/>`_ added as co-maintainer |
michael@0 | 546 | |
michael@0 | 547 | |
michael@0 | 548 | 2009/08/22 Version 0.6.0 |
michael@0 | 549 | ------------------------ |
michael@0 | 550 | |
michael@0 | 551 | * New test layout compatible with test discovery |
michael@0 | 552 | * Descriptors (static methods / class methods etc) can now be patched and |
michael@0 | 553 | restored correctly |
michael@0 | 554 | * Mocks can raise exceptions when called by setting ``side_effect`` to an |
michael@0 | 555 | exception class or instance |
michael@0 | 556 | * Mocks that wrap objects will not pass on calls to the underlying object if |
michael@0 | 557 | an explicit return_value is set |
michael@0 | 558 | |
michael@0 | 559 | |
michael@0 | 560 | 2009/04/17 Version 0.5.0 |
michael@0 | 561 | ------------------------ |
michael@0 | 562 | |
michael@0 | 563 | * Made DEFAULT part of the public api. |
michael@0 | 564 | * Documentation built with Sphinx. |
michael@0 | 565 | * ``side_effect`` is now called with the same arguments as the mock is called with and |
michael@0 | 566 | if returns a non-DEFAULT value that is automatically set as the ``mock.return_value``. |
michael@0 | 567 | * ``wraps`` keyword argument used for wrapping objects (and passing calls through to the wrapped object). |
michael@0 | 568 | * ``Mock.reset`` renamed to ``Mock.reset_mock``, as reset is a common API name. |
michael@0 | 569 | * ``patch`` / ``patch_object`` are now context managers and can be used with ``with``. |
michael@0 | 570 | * A new 'create' keyword argument to patch and patch_object that allows them to patch |
michael@0 | 571 | (and unpatch) attributes that don't exist. (Potentially unsafe to use - it can allow |
michael@0 | 572 | you to have tests that pass when they are testing an API that doesn't exist - use at |
michael@0 | 573 | your own risk!) |
michael@0 | 574 | * The methods keyword argument to Mock has been removed and merged with spec. The spec |
michael@0 | 575 | argument can now be a list of methods or an object to take the spec from. |
michael@0 | 576 | * Nested patches may now be applied in a different order (created mocks passed |
michael@0 | 577 | in the opposite order). This is actually a bugfix. |
michael@0 | 578 | * patch and patch_object now take a spec keyword argument. If spec is |
michael@0 | 579 | passed in as 'True' then the Mock created will take the object it is replacing |
michael@0 | 580 | as its spec object. If the object being replaced is a class, then the return |
michael@0 | 581 | value for the mock will also use the class as a spec. |
michael@0 | 582 | * A Mock created without a spec will not attempt to mock any magic methods / attributes |
michael@0 | 583 | (they will raise an ``AttributeError`` instead). |
michael@0 | 584 | |
michael@0 | 585 | |
michael@0 | 586 | 2008/10/12 Version 0.4.0 |
michael@0 | 587 | ------------------------ |
michael@0 | 588 | |
michael@0 | 589 | * Default return value is now a new mock rather than None |
michael@0 | 590 | * return_value added as a keyword argument to the constructor |
michael@0 | 591 | * New method 'assert_called_with' |
michael@0 | 592 | * Added 'side_effect' attribute / keyword argument called when mock is called |
michael@0 | 593 | * patch decorator split into two decorators: |
michael@0 | 594 | |
michael@0 | 595 | - ``patch_object`` which takes an object and an attribute name to patch |
michael@0 | 596 | (plus optionally a value to patch with which defaults to a mock object) |
michael@0 | 597 | - ``patch`` which takes a string specifying a target to patch; in the form |
michael@0 | 598 | 'package.module.Class.attribute'. (plus optionally a value to |
michael@0 | 599 | patch with which defaults to a mock object) |
michael@0 | 600 | |
michael@0 | 601 | * Can now patch objects with ``None`` |
michael@0 | 602 | * Change to patch for nose compatibility with error reporting in wrapped functions |
michael@0 | 603 | * Reset no longer clears children / return value etc - it just resets |
michael@0 | 604 | call count and call args. It also calls reset on all children (and |
michael@0 | 605 | the return value if it is a mock). |
michael@0 | 606 | |
michael@0 | 607 | Thanks to Konrad Delong, Kevin Dangoor and others for patches and suggestions. |
michael@0 | 608 | |
michael@0 | 609 | |
michael@0 | 610 | 2007/12/03 Version 0.3.1 |
michael@0 | 611 | ------------------------- |
michael@0 | 612 | |
michael@0 | 613 | ``patch`` maintains the name of decorated functions for compatibility with nose |
michael@0 | 614 | test autodiscovery. |
michael@0 | 615 | |
michael@0 | 616 | Tests decorated with ``patch`` that use the two argument form (implicit mock |
michael@0 | 617 | creation) will receive the mock(s) passed in as extra arguments. |
michael@0 | 618 | |
michael@0 | 619 | Thanks to Kevin Dangoor for these changes. |
michael@0 | 620 | |
michael@0 | 621 | |
michael@0 | 622 | 2007/11/30 Version 0.3.0 |
michael@0 | 623 | ------------------------- |
michael@0 | 624 | |
michael@0 | 625 | Removed ``patch_module``. ``patch`` can now take a string as the first |
michael@0 | 626 | argument for patching modules. |
michael@0 | 627 | |
michael@0 | 628 | The third argument to ``patch`` is optional - a mock will be created by |
michael@0 | 629 | default if it is not passed in. |
michael@0 | 630 | |
michael@0 | 631 | |
michael@0 | 632 | 2007/11/21 Version 0.2.1 |
michael@0 | 633 | ------------------------- |
michael@0 | 634 | |
michael@0 | 635 | Bug fix, allows reuse of functions decorated with ``patch`` and ``patch_module``. |
michael@0 | 636 | |
michael@0 | 637 | |
michael@0 | 638 | 2007/11/20 Version 0.2.0 |
michael@0 | 639 | ------------------------- |
michael@0 | 640 | |
michael@0 | 641 | Added ``spec`` keyword argument for creating ``Mock`` objects from a |
michael@0 | 642 | specification object. |
michael@0 | 643 | |
michael@0 | 644 | Added ``patch`` and ``patch_module`` monkey patching decorators. |
michael@0 | 645 | |
michael@0 | 646 | Added ``sentinel`` for convenient access to unique objects. |
michael@0 | 647 | |
michael@0 | 648 | Distribution includes unit tests. |
michael@0 | 649 | |
michael@0 | 650 | |
michael@0 | 651 | 2007/11/19 Version 0.1.0 |
michael@0 | 652 | ------------------------- |
michael@0 | 653 | |
michael@0 | 654 | Initial release. |
michael@0 | 655 | |
michael@0 | 656 | |
michael@0 | 657 | TODO and Limitations |
michael@0 | 658 | ==================== |
michael@0 | 659 | |
michael@0 | 660 | Contributions, bug reports and comments welcomed! |
michael@0 | 661 | |
michael@0 | 662 | Feature requests and bug reports are handled on the issue tracker: |
michael@0 | 663 | |
michael@0 | 664 | * `mock issue tracker <http://code.google.com/p/mock/issues/list>`_ |
michael@0 | 665 | |
michael@0 | 666 | `wraps` is not integrated with magic methods. |
michael@0 | 667 | |
michael@0 | 668 | `patch` could auto-do the patching in the constructor and unpatch in the |
michael@0 | 669 | destructor. This would be useful in itself, but violates TOOWTDI and would be |
michael@0 | 670 | unsafe for IronPython & PyPy (non-deterministic calling of destructors). |
michael@0 | 671 | Destructors aren't called in CPython where there are cycles, but a weak |
michael@0 | 672 | reference with a callback can be used to get round this. |
michael@0 | 673 | |
michael@0 | 674 | `Mock` has several attributes. This makes it unsuitable for mocking objects |
michael@0 | 675 | that use these attribute names. A way round this would be to provide methods |
michael@0 | 676 | that *hide* these attributes when needed. In 0.8 many, but not all, of these |
michael@0 | 677 | attributes are renamed to gain a `_mock` prefix, making it less likely that |
michael@0 | 678 | they will clash. Any outstanding attributes that haven't been modified with |
michael@0 | 679 | the prefix should be changed. |
michael@0 | 680 | |
michael@0 | 681 | If a patch is started using `patch.start` and then not stopped correctly then |
michael@0 | 682 | the unpatching is not done. Using weak references it would be possible to |
michael@0 | 683 | detect and fix this when the patch object itself is garbage collected. This |
michael@0 | 684 | would be tricky to get right though. |
michael@0 | 685 | |
michael@0 | 686 | When a `Mock` is created by `patch`, arbitrary keywords can be used to set |
michael@0 | 687 | attributes. If `patch` is created with a `spec`, and is replacing a class, then |
michael@0 | 688 | a `return_value` mock is created. The keyword arguments are not applied to the |
michael@0 | 689 | child mock, but could be. |
michael@0 | 690 | |
michael@0 | 691 | When mocking a class with `patch`, passing in `spec=True` or `autospec=True`, |
michael@0 | 692 | the mock class has an instance created from the same spec. Should this be the |
michael@0 | 693 | default behaviour for mocks anyway (mock return values inheriting the spec |
michael@0 | 694 | from their parent), or should it be controlled by an additional keyword |
michael@0 | 695 | argument (`inherit`) to the Mock constructor? `create_autospec` does this, so |
michael@0 | 696 | an additional keyword argument to Mock is probably unnecessary. |
michael@0 | 697 | |
michael@0 | 698 | The `mocksignature` argument to `patch` with a non `Mock` passed into |
michael@0 | 699 | `new_callable` will *probably* cause an error. Should it just be invalid? |
michael@0 | 700 | |
michael@0 | 701 | Note that `NonCallableMock` and `NonCallableMagicMock` still have the unused |
michael@0 | 702 | (and unusable) attributes: `return_value`, `side_effect`, `call_count`, |
michael@0 | 703 | `call_args` and `call_args_list`. These could be removed or raise errors on |
michael@0 | 704 | getting / setting. They also have the `assert_called_with` and |
michael@0 | 705 | `assert_called_once_with` methods. Removing these would be pointless as |
michael@0 | 706 | fetching them would create a mock (attribute) that could be called without |
michael@0 | 707 | error. |
michael@0 | 708 | |
michael@0 | 709 | Some outstanding technical debt. The way autospeccing mocks function |
michael@0 | 710 | signatures was copied and modified from `mocksignature`. This could all be |
michael@0 | 711 | refactored into one set of functions instead of two. The way we tell if |
michael@0 | 712 | patchers are started and if a patcher is being used for a `patch.multiple` |
michael@0 | 713 | call are both horrible. There are now a host of helper functions that should |
michael@0 | 714 | be rationalised. (Probably time to split mock into a package instead of a |
michael@0 | 715 | module.) |
michael@0 | 716 | |
michael@0 | 717 | Passing arbitrary keyword arguments to `create_autospec`, or `patch` with |
michael@0 | 718 | `autospec`, when mocking a *function* works fine. However, the arbitrary |
michael@0 | 719 | attributes are set on the created mock - but `create_autospec` returns a |
michael@0 | 720 | real function (which doesn't have those attributes). However, what is the use |
michael@0 | 721 | case for using autospec to create functions with attributes that don't exist |
michael@0 | 722 | on the original? |
michael@0 | 723 | |
michael@0 | 724 | `mocksignature`, plus the `call_args_list` and `method_calls` attributes of |
michael@0 | 725 | `Mock` could all be deprecated. |