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 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 72 | ``git clone https://github.com/testing-cabal/mock`` |
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 | |
| 81 | Alternatively you can download the mock distribution from PyPI and after |
| 82 | unpacking run: |
| 83 | |
| 84 | ``python setup.py install`` |
| 85 | |
| 86 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 87 | .. index:: bug reports |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 88 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 89 | Bug Reports |
| 90 | +++++++++++ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 91 | |
Jon Dufresne | 89123d5 | 2018-09-16 17:20:15 -0700 | [diff] [blame] | 92 | 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] | 93 | Issues with the backport process, such as compatibility with a particular |
| 94 | Python, should be reported to the `bug tracker |
| 95 | <https://github.com/testing-cabal/mock/issues>`_. Feature requests and issues |
| 96 | with Mock functionality should be reported to the `Python bug tracker |
| 97 | <https://bugs.python.org>`_. |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 98 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 99 | .. index:: python changes |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 100 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 101 | Python Changes |
| 102 | ++++++++++++++ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 103 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 104 | Python NEWS entries from cPython: |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 105 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 106 | .. include:: ../NEWS |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 107 | |
| 108 | .. index:: older versions |
| 109 | |
Robert Collins | dff3a7b | 2015-08-04 09:28:39 +1200 | [diff] [blame] | 110 | Older Versions of Python |
| 111 | ++++++++++++++++++++++++ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 112 | |
Robert Collins | dff3a7b | 2015-08-04 09:28:39 +1200 | [diff] [blame] | 113 | 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] | 114 | |
Hugo | b67fe8a | 2017-10-28 16:01:45 +0300 | [diff] [blame] | 115 | Version 2.0.0 is the last version compatible with Python 2.6. |
| 116 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 117 | .. index:: maintainer notes |
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 | Maintainer Notes |
| 120 | ++++++++++++++++ |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 121 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 122 | Development |
Jon Dufresne | 0f7f20f | 2018-09-16 18:04:59 -0700 | [diff] [blame] | 123 | ----------- |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 124 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 125 | Checkout from git (see :ref:`installing`) and submit pull requests. |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 126 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 127 | Committers can just push as desired: since all semantic development takes |
| 128 | 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] | 129 | |
Hugo | 59130a4 | 2018-05-08 08:23:53 +0300 | [diff] [blame] | 130 | mock is CI tested using Travis-CI on Python versions 2.7, 3.4, |
| 131 | 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] | 132 | 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] | 133 | excellent. |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 134 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 135 | Releasing |
Jon Dufresne | 0f7f20f | 2018-09-16 18:04:59 -0700 | [diff] [blame] | 136 | --------- |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 137 | |
Robert Collins | e795a4a | 2015-07-10 14:32:36 +1200 | [diff] [blame] | 138 | NB: please use semver. Bump the major component on API breaks, minor on all |
| 139 | non-bugfix changes, patch on bugfix only changes. |
| 140 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 141 | 1. tag -s, push --tags origin master |
| 142 | 2. setup.py sdist bdist_wheel upload -s |
Michael Foord | 1e68bec | 2012-03-03 22:24:30 +0000 | [diff] [blame] | 143 | |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 144 | |
| 145 | Backporting rules |
Jon Dufresne | 0f7f20f | 2018-09-16 18:04:59 -0700 | [diff] [blame] | 146 | ----------------- |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 147 | |
| 148 | isinstance checks in cPython to ``type`` need to check ``ClassTypes``. |
| 149 | Code calling ``obj.isidentifier`` needs to change to ``_isidentifier(obj)``. |
| 150 | |
| 151 | Backporting process |
Jon Dufresne | 0f7f20f | 2018-09-16 18:04:59 -0700 | [diff] [blame] | 152 | ------------------- |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 153 | |
| 154 | 1. Patch your git am with `my patch <https://github.com/rbtcollins/git>`_. |
| 155 | 2. Install the applypatch-transform hook from tools/ to your .git hooks dir. |
| 156 | 3. Configure a pre-applypatch hook to test at least all the cPython versions |
| 157 | we support on each patch that is applied. I use containers, and a sample |
| 158 | script is in tools/pre-applypatch. |
| 159 | 4. Pull down the cPython git mirror: https://github.com/python/cpython.git |
Chris Withers | 51239e4 | 2019-04-26 08:31:26 +0100 | [diff] [blame] | 160 | 5. Export the new revisions since the ``Last sync`:: |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 161 | |
Chris Withers | fb1f5b3 | 2019-04-26 20:29:03 +0100 | [diff] [blame] | 162 | start=`cat lastsync.txt` |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 163 | rm migrate-export |
Chris Withers | fb1f5b3 | 2019-04-26 20:29:03 +0100 | [diff] [blame] | 164 | git log --pretty="format:%H " $start.. -- Lib/unittest/mock.py \ |
Robert Collins | 18c9bbd | 2015-07-10 14:00:11 +1200 | [diff] [blame] | 165 | Lib/unittest/test/testmock/ > migrate-revs |
| 166 | tac migrate-revs > migrate-sorted-revs |
| 167 | for rev in $(< migrate-sorted-revs); do |
| 168 | git format-patch -1 $rev -k --stdout >> migrate-export; |
| 169 | done |
| 170 | echo NEW SYNC POINT: $(git rev-parse HEAD) |
| 171 | |
| 172 | 6. Import into mock:: |
| 173 | |
| 174 | git am -k --reject $path-to-cpython/migrate-export |
| 175 | |
| 176 | This will transform the patches automatically. Currently it will error |
| 177 | on every NEWS change as I haven't gotten around to making those patches |
| 178 | automatic. Fixup any errors that occur. When the patch is ready, do a ``git |
| 179 | add -u`` to update the index and then ``git am --continue`` to move onto |
| 180 | the next patch. If the patch is inappropriate e.g. the patch removing |
| 181 | __ne__ which would break older pythons, then either do ``git reset --hard; |
| 182 | git am --skip`` to discard any partially applied changes and skip over it, |
| 183 | or, if it has a NEWS entry thats worth preserving, edit it down to just |
| 184 | that, with a note such as we have for the ``__ne__`` patch, and continue on |
| 185 | from there. |
| 186 | |
| 187 | The goal is that every patch work at all times. |
| 188 | |
| 189 | 7. After the import is complete, update this document with the new sync point. |
| 190 | |
| 191 | 8. Push to a personal branch and propose a PR to the main repo. This will make |
| 192 | Travis-CI test it. If it works, push to the main repo. |