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