blob: f374f33fc69083e900fbb27bfbccc1686f41a4e0 [file] [log] [blame]
Jon Dufresne0f7f20f2018-09-16 18:04:59 -07001===================================
Michael Foord1e68bec2012-03-03 22:24:30 +00002 Mock - Mocking and Testing Library
Jon Dufresne0f7f20f2018-09-16 18:04:59 -07003===================================
Michael Foord1e68bec2012-03-03 22:24:30 +00004
Michael Foord1e68bec2012-03-03 22:24:30 +00005:Version: |release|
Robert Collins18c9bbd2015-07-10 14:00:11 +12006:Date: |today|
Michael Foord1e68bec2012-03-03 22:24:30 +00007:Homepage: `Mock Homepage`_
8:Download: `Mock on PyPI`_
Robert Collins18c9bbd2015-07-10 14:00:11 +12009:Documentation: `Python Docs`_
Michael Foord1e68bec2012-03-03 22:24:30 +000010:License: `BSD License`_
11:Support: `Mailing list (testing-in-python@lists.idyll.org)
12 <http://lists.idyll.org/listinfo/testing-in-python>`_
Hugob67fe8a2017-10-28 16:01:45 +030013:Issue tracker: `GitHub Issues
Robert Collins18c9bbd2015-07-10 14:00:11 +120014 <https://github.com/testing-cabal/mock/issues>`_
Michael Foord1e68bec2012-03-03 22:24:30 +000015
Robert Collins18c9bbd2015-07-10 14:00:11 +120016.. _Mock Homepage: https://github.com/testing-cabal/mock
Chris Withers0f469132018-11-29 08:46:15 +000017.. _BSD License: https://github.com/testing-cabal/mock/blob/master/LICENSE.txt
Robert Collins18c9bbd2015-07-10 14:00:11 +120018.. _Python Docs: https://docs.python.org/dev/library/unittest.mock.html
Michael Foord1e68bec2012-03-03 22:24:30 +000019
20.. module:: mock
21 :synopsis: Mock object and testing library.
22
23.. index:: introduction
24
Robert Collins18c9bbd2015-07-10 14:00:11 +120025TOC
26+++
27
28.. toctree::
29 :maxdepth: 2
30
31 changelog
32
33Introduction
34++++++++++++
35
Michael Foord1e68bec2012-03-03 22:24:30 +000036mock is a library for testing in Python. It allows you to replace parts of
37your system under test with mock objects and make assertions about how they
38have been used.
39
Robert Collins18c9bbd2015-07-10 14:00:11 +120040mock 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
42runs on multiple versions of Python the ``mock`` package is better, as you get
43the newest features from the latest release of Python available for all
44Pythons.
Michael Foord2df66112012-10-07 18:33:12 +010045
Robert Collins18c9bbd2015-07-10 14:00:11 +120046The ``mock`` package contains a rolling backport of the standard library mock
Hugo59130a42018-05-08 08:23:53 +030047code compatible with Python 2.7 and 3.4 and up.
Hugob67fe8a2017-10-28 16:01:45 +030048
Hugo59130a42018-05-08 08:23:53 +030049* Python 2.6 and 3.3 are supported by mock 2.0.0 and below.
Hugob67fe8a2017-10-28 16:01:45 +030050
51* Python 3.2 is supported by mock 1.3.0 and below - with pip no longer
Jon Dufresne0f7f20f2018-09-16 18:04:59 -070052 supporting 3.2, we cannot test against that version anymore.
Michael Foord1e68bec2012-03-03 22:24:30 +000053
Robert Collins18c9bbd2015-07-10 14:00:11 +120054Please see the standard library documentation for usage details.
Michael Foord1e68bec2012-03-03 22:24:30 +000055
56.. index:: installing
Robert Collins18c9bbd2015-07-10 14:00:11 +120057.. _installing:
Michael Foord1e68bec2012-03-03 22:24:30 +000058
59Installing
Robert Collins18c9bbd2015-07-10 14:00:11 +120060++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +000061
Robert Collins18c9bbd2015-07-10 14:00:11 +120062The current version is |release|. Mock is stable and widely used.
Michael Foord1e68bec2012-03-03 22:24:30 +000063
Jon Dufresne34e51012018-04-26 06:00:29 -070064* `mock on PyPI <https://pypi.org/project/mock/>`_
Michael Foord1e68bec2012-03-03 22:24:30 +000065
66.. index:: repository
Robert Collins18c9bbd2015-07-10 14:00:11 +120067.. index:: git
Michael Foord1e68bec2012-03-03 22:24:30 +000068
Hugob67fe8a2017-10-28 16:01:45 +030069You can checkout the latest development version from GitHub
Michael Foord1e68bec2012-03-03 22:24:30 +000070repository with the following command:
71
Chris Withers8a5e0c32019-04-27 14:29:03 +010072 ``git clone https://github.com/testing-cabal/mock.git``
Michael Foord1e68bec2012-03-03 22:24:30 +000073
74
75.. index:: pip
Michael Foord1e68bec2012-03-03 22:24:30 +000076
Robert Collins18c9bbd2015-07-10 14:00:11 +120077You can install mock with pip:
Michael Foord1e68bec2012-03-03 22:24:30 +000078
Michael Foord1e68bec2012-03-03 22:24:30 +000079 | ``pip install -U mock``
80
Robert Collins18c9bbd2015-07-10 14:00:11 +120081.. index:: bug reports
Michael Foord1e68bec2012-03-03 22:24:30 +000082
Robert Collins18c9bbd2015-07-10 14:00:11 +120083Bug Reports
84+++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +000085
Jon Dufresne89123d52018-09-16 17:20:15 -070086Mock uses `unittest <https://docs.python.org/3/library/unittest.html>`_ for its own
Robert Collins18c9bbd2015-07-10 14:00:11 +120087Issues with the backport process, such as compatibility with a particular
88Python, should be reported to the `bug tracker
89<https://github.com/testing-cabal/mock/issues>`_. Feature requests and issues
90with Mock functionality should be reported to the `Python bug tracker
91<https://bugs.python.org>`_.
Michael Foord1e68bec2012-03-03 22:24:30 +000092
Robert Collins18c9bbd2015-07-10 14:00:11 +120093.. index:: python changes
Michael Foord1e68bec2012-03-03 22:24:30 +000094
Robert Collins18c9bbd2015-07-10 14:00:11 +120095Python Changes
96++++++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +000097
Robert Collins18c9bbd2015-07-10 14:00:11 +120098Python NEWS entries from cPython:
Michael Foord1e68bec2012-03-03 22:24:30 +000099
Robert Collins18c9bbd2015-07-10 14:00:11 +1200100.. include:: ../NEWS
Michael Foord1e68bec2012-03-03 22:24:30 +0000101
102.. index:: older versions
103
Robert Collinsdff3a7b2015-08-04 09:28:39 +1200104Older Versions of Python
105++++++++++++++++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +0000106
Robert Collinsdff3a7b2015-08-04 09:28:39 +1200107Version 1.0.1 is the last version compatible with Python < 2.6.
Michael Foord1e68bec2012-03-03 22:24:30 +0000108
Hugob67fe8a2017-10-28 16:01:45 +0300109Version 2.0.0 is the last version compatible with Python 2.6.
110
Robert Collins18c9bbd2015-07-10 14:00:11 +1200111.. index:: maintainer notes
Michael Foord1e68bec2012-03-03 22:24:30 +0000112
Robert Collins18c9bbd2015-07-10 14:00:11 +1200113Maintainer Notes
114++++++++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +0000115
Robert Collins18c9bbd2015-07-10 14:00:11 +1200116Development
Jon Dufresne0f7f20f2018-09-16 18:04:59 -0700117-----------
Michael Foord1e68bec2012-03-03 22:24:30 +0000118
Robert Collins18c9bbd2015-07-10 14:00:11 +1200119Checkout from git (see :ref:`installing`) and submit pull requests.
Michael Foord1e68bec2012-03-03 22:24:30 +0000120
Robert Collins18c9bbd2015-07-10 14:00:11 +1200121Committers can just push as desired: since all semantic development takes
122place in cPython, the backport process is as lightweight as we can make it.
Michael Foord1e68bec2012-03-03 22:24:30 +0000123
Hugo59130a42018-05-08 08:23:53 +0300124mock is CI tested using Travis-CI on Python versions 2.7, 3.4,
1253.5, 3.6, nightly Python 3 builds, pypy, pypy3. Jython support is desired, if
lord6305a0ef02015-09-23 10:47:31 +0800126someone could contribute a patch to .travis.yml to support it that would be
Robert Collins18c9bbd2015-07-10 14:00:11 +1200127excellent.
Michael Foord1e68bec2012-03-03 22:24:30 +0000128
Robert Collins18c9bbd2015-07-10 14:00:11 +1200129Releasing
Jon Dufresne0f7f20f2018-09-16 18:04:59 -0700130---------
Michael Foord1e68bec2012-03-03 22:24:30 +0000131
Robert Collinse795a4a2015-07-10 14:32:36 +1200132NB: please use semver. Bump the major component on API breaks, minor on all
133non-bugfix changes, patch on bugfix only changes.
134
Robert Collins18c9bbd2015-07-10 14:00:11 +12001351. tag -s, push --tags origin master
1362. setup.py sdist bdist_wheel upload -s
Michael Foord1e68bec2012-03-03 22:24:30 +0000137
Robert Collins18c9bbd2015-07-10 14:00:11 +1200138
139Backporting rules
Jon Dufresne0f7f20f2018-09-16 18:04:59 -0700140-----------------
Robert Collins18c9bbd2015-07-10 14:00:11 +1200141
Chris Withers8a5e0c32019-04-27 14:29:03 +0100142- ``isinstance`` checks in cPython to ``type`` need to check ``ClassTypes``.
143 Code calling ``obj.isidentifier`` needs to change to ``_isidentifier(obj)``.
Robert Collins18c9bbd2015-07-10 14:00:11 +1200144
145Backporting process
Jon Dufresne0f7f20f2018-09-16 18:04:59 -0700146-------------------
Robert Collins18c9bbd2015-07-10 14:00:11 +1200147
Chris Withers8a5e0c32019-04-27 14:29:03 +01001481. Clone cpython and mock into the same directory, eg:
Robert Collins18c9bbd2015-07-10 14:00:11 +1200149
Chris Withers8a5e0c32019-04-27 14:29:03 +0100150 .. code-block:: bash
Robert Collins18c9bbd2015-07-10 14:00:11 +1200151
Chris Withers8a5e0c32019-04-27 14:29:03 +0100152 mkdir vcs
153 cd vcs
154 git clone https://github.com/python/cpython.git
155 git clone https://github.com/testing-cabal/mock.git
Robert Collins18c9bbd2015-07-10 14:00:11 +1200156
Chris Withers8a5e0c32019-04-27 14:29:03 +0100157 Make sure they both on master and up to date!
Robert Collins18c9bbd2015-07-10 14:00:11 +1200158
Chris Withers8a5e0c32019-04-27 14:29:03 +01001592. Create a branch in your ``mock`` clone and switch to it.
Robert Collins18c9bbd2015-07-10 14:00:11 +1200160
Chris Withers8a5e0c32019-04-27 14:29:03 +01001613. Make sure you build a suitable virtualenv for Mock development
162 and activate it. For backporting, this should use Python 3.7+.
Robert Collins18c9bbd2015-07-10 14:00:11 +1200163
Chris Withers8a5e0c32019-04-27 14:29:03 +01001644. Run ``backport.py``:
Robert Collins18c9bbd2015-07-10 14:00:11 +1200165
Chris Withers8a5e0c32019-04-27 14:29:03 +0100166 .. code-block:: bash
167
168 cd vcs/mock
169 python backport.py
170
171 This will find the next cpython patch that needs to be applied, munge it
172 and attempt to apply it with ``git am``.
173
174 If it succeeds, run the tests and/or push your branch up to a fork and
175 do a pull request into the master branch of the main repo to kick off
176 the continuous integration tests.
177
178 If it fails, you'll have to manually work with what ``git status`` shows
179 to get the patch committed.
180
181 If you have to make changes, please do a ``git commit --amend`` and add notes
182 about what needed doing below the ``Signed-off-by`` block.
183
184
1855. Rinse and repeat until ``backport.py`` reports no more patches need applying.