Jon Dufresne | 0f7f20f | 2018-09-16 18:04:59 -0700 | [diff] [blame] | 1 | =================================== |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 2 | Mock - Mocking and Testing Library |
Jon Dufresne | 0f7f20f | 2018-09-16 18:04:59 -0700 | [diff] [blame] | 3 | =================================== |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 4 | |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 5 | :Version: |release| |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 6 | :Date: |today| |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 7 | :Homepage: `Mock Homepage`_ |
| 8 | :Download: `Mock on PyPI`_ |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 9 | :Documentation: `Python Docs`_ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 10 | :License: `BSD License`_ |
| 11 | :Support: `Mailing list (testing-in-python@lists.idyll.org) |
| 12 | <http://lists.idyll.org/listinfo/testing-in-python>`_ |
Hugo | b67fe8a | 2017-10-28 16:01:45 +0300 | [diff] [blame] | 13 | :Issue tracker: `GitHub Issues |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 14 | <https://github.com/testing-cabal/mock/issues>`_ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 15 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 16 | .. _Mock Homepage: https://github.com/testing-cabal/mock |
Chris Withers | 0f46913 | 2018-11-29 08:46:15 +0000 | [diff] [blame] | 17 | .. _BSD License: https://github.com/testing-cabal/mock/blob/master/LICENSE.txt |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 18 | .. _Python Docs: https://docs.python.org/dev/library/unittest.mock.html |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 19 | |
| 20 | .. module:: mock |
| 21 | :synopsis: Mock object and testing library. |
| 22 | |
| 23 | .. index:: introduction |
| 24 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 25 | TOC |
| 26 | +++ |
| 27 | |
| 28 | .. toctree:: |
| 29 | :maxdepth: 2 |
| 30 | |
| 31 | changelog |
| 32 | |
| 33 | Introduction |
| 34 | ++++++++++++ |
| 35 | |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 36 | mock is a library for testing in Python. It allows you to replace parts of |
| 37 | your system under test with mock objects and make assertions about how they |
| 38 | have been used. |
| 39 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 40 | mock is now part of the Python standard library, available as |
| 41 | ``unittest.mock`` in Python 3.3 onwards. However, if you are writing code that |
| 42 | runs on multiple versions of Python the ``mock`` package is better, as you get |
| 43 | the newest features from the latest release of Python available for all |
| 44 | Pythons. |
Michael Foord | 2df6611 | 2012-10-07 18:33:12 +0100 | [diff] [blame] | 45 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 46 | The ``mock`` package contains a rolling backport of the standard library mock |
Hugo | 59130a4 | 2018-05-08 08:23:53 +0300 | [diff] [blame] | 47 | code compatible with Python 2.7 and 3.4 and up. |
Hugo | b67fe8a | 2017-10-28 16:01:45 +0300 | [diff] [blame] | 48 | |
Hugo | 59130a4 | 2018-05-08 08:23:53 +0300 | [diff] [blame] | 49 | * Python 2.6 and 3.3 are supported by mock 2.0.0 and below. |
Hugo | b67fe8a | 2017-10-28 16:01:45 +0300 | [diff] [blame] | 50 | |
| 51 | * Python 3.2 is supported by mock 1.3.0 and below - with pip no longer |
Jon Dufresne | 0f7f20f | 2018-09-16 18:04:59 -0700 | [diff] [blame] | 52 | supporting 3.2, we cannot test against that version anymore. |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 53 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 54 | Please see the standard library documentation for usage details. |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 55 | |
| 56 | .. index:: installing |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 57 | .. _installing: |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 58 | |
| 59 | Installing |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 60 | ++++++++++ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 61 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 62 | The current version is |release|. Mock is stable and widely used. |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 63 | |
Jon Dufresne | 34e5101 | 2018-04-26 06:00:29 -0700 | [diff] [blame] | 64 | * `mock on PyPI <https://pypi.org/project/mock/>`_ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 65 | |
| 66 | .. index:: repository |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 67 | .. index:: git |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 68 | |
Hugo | b67fe8a | 2017-10-28 16:01:45 +0300 | [diff] [blame] | 69 | You can checkout the latest development version from GitHub |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 70 | repository with the following command: |
| 71 | |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 72 | ``git clone https://github.com/testing-cabal/mock.git`` |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | .. index:: pip |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 76 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 77 | You can install mock with pip: |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 78 | |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 79 | | ``pip install -U mock`` |
| 80 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 81 | .. index:: bug reports |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 82 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 83 | Bug Reports |
| 84 | +++++++++++ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 85 | |
Jon Dufresne | 89123d5 | 2018-09-16 17:20:15 -0700 | [diff] [blame] | 86 | Mock uses `unittest <https://docs.python.org/3/library/unittest.html>`_ for its own |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 87 | Issues with the backport process, such as compatibility with a particular |
| 88 | Python, should be reported to the `bug tracker |
| 89 | <https://github.com/testing-cabal/mock/issues>`_. Feature requests and issues |
| 90 | with Mock functionality should be reported to the `Python bug tracker |
| 91 | <https://bugs.python.org>`_. |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 92 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 93 | .. index:: python changes |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 94 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 95 | Python Changes |
| 96 | ++++++++++++++ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 97 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 98 | Python NEWS entries from cPython: |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 99 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 100 | .. include:: ../NEWS |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 101 | |
| 102 | .. index:: older versions |
| 103 | |
Robert Collins | dff3a7b | 2015-08-04 09:28:39 +1200 | [diff] [blame] | 104 | Older Versions of Python |
| 105 | ++++++++++++++++++++++++ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 106 | |
Robert Collins | dff3a7b | 2015-08-04 09:28:39 +1200 | [diff] [blame] | 107 | Version 1.0.1 is the last version compatible with Python < 2.6. |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 108 | |
Hugo | b67fe8a | 2017-10-28 16:01:45 +0300 | [diff] [blame] | 109 | Version 2.0.0 is the last version compatible with Python 2.6. |
| 110 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 111 | .. index:: maintainer notes |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 112 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 113 | Maintainer Notes |
| 114 | ++++++++++++++++ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 115 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 116 | Development |
Jon Dufresne | 0f7f20f | 2018-09-16 18:04:59 -0700 | [diff] [blame] | 117 | ----------- |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 118 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 119 | Checkout from git (see :ref:`installing`) and submit pull requests. |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 120 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 121 | Committers can just push as desired: since all semantic development takes |
| 122 | place in cPython, the backport process is as lightweight as we can make it. |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 123 | |
Hugo | 59130a4 | 2018-05-08 08:23:53 +0300 | [diff] [blame] | 124 | mock is CI tested using Travis-CI on Python versions 2.7, 3.4, |
| 125 | 3.5, 3.6, nightly Python 3 builds, pypy, pypy3. Jython support is desired, if |
lord63 | 05a0ef0 | 2015-09-23 10:47:31 +0800 | [diff] [blame] | 126 | someone could contribute a patch to .travis.yml to support it that would be |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 127 | excellent. |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 128 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 129 | Releasing |
Jon Dufresne | 0f7f20f | 2018-09-16 18:04:59 -0700 | [diff] [blame] | 130 | --------- |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 131 | |
Robert Collins | e795a4a | 2015-07-10 14:32:36 +1200 | [diff] [blame] | 132 | NB: please use semver. Bump the major component on API breaks, minor on all |
| 133 | non-bugfix changes, patch on bugfix only changes. |
| 134 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 135 | 1. tag -s, push --tags origin master |
| 136 | 2. setup.py sdist bdist_wheel upload -s |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 137 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 138 | |
| 139 | Backporting rules |
Jon Dufresne | 0f7f20f | 2018-09-16 18:04:59 -0700 | [diff] [blame] | 140 | ----------------- |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 141 | |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 142 | - ``isinstance`` checks in cPython to ``type`` need to check ``ClassTypes``. |
| 143 | Code calling ``obj.isidentifier`` needs to change to ``_isidentifier(obj)``. |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 144 | |
Chris Withers | e684d67 | 2019-04-28 20:48:13 +0100 | [diff] [blame] | 145 | - f-strings need to be rewritten using some other string substitution. |
| 146 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 147 | Backporting process |
Jon Dufresne | 0f7f20f | 2018-09-16 18:04:59 -0700 | [diff] [blame] | 148 | ------------------- |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 149 | |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 150 | 1. Clone cpython and mock into the same directory, eg: |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 151 | |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 152 | .. code-block:: bash |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 153 | |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 154 | mkdir vcs |
| 155 | cd vcs |
| 156 | git clone https://github.com/python/cpython.git |
| 157 | git clone https://github.com/testing-cabal/mock.git |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 158 | |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 159 | Make sure they both on master and up to date! |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 160 | |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 161 | 2. Create a branch in your ``mock`` clone and switch to it. |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 162 | |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 163 | 3. Make sure you build a suitable virtualenv for Mock development |
| 164 | and activate it. For backporting, this should use Python 3.7+. |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 165 | |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 166 | 4. Run ``backport.py``: |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 167 | |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 168 | .. code-block:: bash |
| 169 | |
| 170 | cd vcs/mock |
| 171 | python backport.py |
| 172 | |
| 173 | This will find the next cpython patch that needs to be applied, munge it |
| 174 | and attempt to apply it with ``git am``. |
| 175 | |
| 176 | If it succeeds, run the tests and/or push your branch up to a fork and |
| 177 | do a pull request into the master branch of the main repo to kick off |
| 178 | the continuous integration tests. |
| 179 | |
| 180 | If it fails, you'll have to manually work with what ``git status`` shows |
| 181 | to get the patch committed. |
| 182 | |
Chris Withers | a336518 | 2019-04-27 17:35:49 +0100 | [diff] [blame] | 183 | If it turns out that there's nothing that should be applied from the failed commit, |
| 184 | run ``python backport.py --skip-current``. |
| 185 | |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 186 | If you have to make changes, please do a ``git commit --amend`` and add notes |
| 187 | about what needed doing below the ``Signed-off-by`` block. |
| 188 | |
Chris Withers | b0b839f | 2019-04-27 14:48:34 +0100 | [diff] [blame] | 189 | If you have to make changes because tests fail with an applied patch, please |
| 190 | make those changes in a followup commit and take note of the "Backporting rules" |
| 191 | above. |
Chris Withers | 8a5e0c3 | 2019-04-27 14:29:03 +0100 | [diff] [blame] | 192 | |
| 193 | 5. Rinse and repeat until ``backport.py`` reports no more patches need applying. |
Chris Withers | 871b526 | 2019-04-27 15:04:13 +0100 | [diff] [blame] | 194 | |
| 195 | 6. If ``backport.py`` has updated ``lastsync.txt``, now would be a good time |
| 196 | to commit that change. |