blob: 7d96decc952c2cb462cb6c80a26f79da5df11eab [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
Robert Collins18c9bbd2015-07-10 14:00:11 +120072 ``git clone https://github.com/testing-cabal/mock``
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
81Alternatively you can download the mock distribution from PyPI and after
82unpacking run:
83
84 ``python setup.py install``
85
86
Robert Collins18c9bbd2015-07-10 14:00:11 +120087.. index:: bug reports
Michael Foord1e68bec2012-03-03 22:24:30 +000088
Robert Collins18c9bbd2015-07-10 14:00:11 +120089Bug Reports
90+++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +000091
Jon Dufresne89123d52018-09-16 17:20:15 -070092Mock uses `unittest <https://docs.python.org/3/library/unittest.html>`_ for its own
Robert Collins18c9bbd2015-07-10 14:00:11 +120093Issues with the backport process, such as compatibility with a particular
94Python, should be reported to the `bug tracker
95<https://github.com/testing-cabal/mock/issues>`_. Feature requests and issues
96with Mock functionality should be reported to the `Python bug tracker
97<https://bugs.python.org>`_.
Michael Foord1e68bec2012-03-03 22:24:30 +000098
Robert Collins18c9bbd2015-07-10 14:00:11 +120099.. index:: python changes
Michael Foord1e68bec2012-03-03 22:24:30 +0000100
Robert Collins18c9bbd2015-07-10 14:00:11 +1200101Python Changes
102++++++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +0000103
Robert Collins18c9bbd2015-07-10 14:00:11 +1200104Python NEWS entries from cPython:
Michael Foord1e68bec2012-03-03 22:24:30 +0000105
Robert Collins18c9bbd2015-07-10 14:00:11 +1200106.. include:: ../NEWS
Michael Foord1e68bec2012-03-03 22:24:30 +0000107
108.. index:: older versions
109
Robert Collinsdff3a7b2015-08-04 09:28:39 +1200110Older Versions of Python
111++++++++++++++++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +0000112
Robert Collinsdff3a7b2015-08-04 09:28:39 +1200113Version 1.0.1 is the last version compatible with Python < 2.6.
Michael Foord1e68bec2012-03-03 22:24:30 +0000114
Hugob67fe8a2017-10-28 16:01:45 +0300115Version 2.0.0 is the last version compatible with Python 2.6.
116
Robert Collins18c9bbd2015-07-10 14:00:11 +1200117.. index:: maintainer notes
Michael Foord1e68bec2012-03-03 22:24:30 +0000118
Robert Collins18c9bbd2015-07-10 14:00:11 +1200119Maintainer Notes
120++++++++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +0000121
Robert Collins18c9bbd2015-07-10 14:00:11 +1200122Development
Jon Dufresne0f7f20f2018-09-16 18:04:59 -0700123-----------
Michael Foord1e68bec2012-03-03 22:24:30 +0000124
Robert Collins18c9bbd2015-07-10 14:00:11 +1200125Checkout from git (see :ref:`installing`) and submit pull requests.
Michael Foord1e68bec2012-03-03 22:24:30 +0000126
Robert Collins18c9bbd2015-07-10 14:00:11 +1200127Committers can just push as desired: since all semantic development takes
128place in cPython, the backport process is as lightweight as we can make it.
Michael Foord1e68bec2012-03-03 22:24:30 +0000129
Hugo59130a42018-05-08 08:23:53 +0300130mock is CI tested using Travis-CI on Python versions 2.7, 3.4,
1313.5, 3.6, nightly Python 3 builds, pypy, pypy3. Jython support is desired, if
lord6305a0ef02015-09-23 10:47:31 +0800132someone could contribute a patch to .travis.yml to support it that would be
Robert Collins18c9bbd2015-07-10 14:00:11 +1200133excellent.
Michael Foord1e68bec2012-03-03 22:24:30 +0000134
Robert Collins18c9bbd2015-07-10 14:00:11 +1200135Releasing
Jon Dufresne0f7f20f2018-09-16 18:04:59 -0700136---------
Michael Foord1e68bec2012-03-03 22:24:30 +0000137
Robert Collinse795a4a2015-07-10 14:32:36 +1200138NB: please use semver. Bump the major component on API breaks, minor on all
139non-bugfix changes, patch on bugfix only changes.
140
Robert Collins18c9bbd2015-07-10 14:00:11 +12001411. tag -s, push --tags origin master
1422. setup.py sdist bdist_wheel upload -s
Michael Foord1e68bec2012-03-03 22:24:30 +0000143
Robert Collins18c9bbd2015-07-10 14:00:11 +1200144
145Backporting rules
Jon Dufresne0f7f20f2018-09-16 18:04:59 -0700146-----------------
Robert Collins18c9bbd2015-07-10 14:00:11 +1200147
148isinstance checks in cPython to ``type`` need to check ``ClassTypes``.
149Code calling ``obj.isidentifier`` needs to change to ``_isidentifier(obj)``.
150
151Backporting process
Jon Dufresne0f7f20f2018-09-16 18:04:59 -0700152-------------------
Robert Collins18c9bbd2015-07-10 14:00:11 +1200153
1541. Patch your git am with `my patch <https://github.com/rbtcollins/git>`_.
1552. Install the applypatch-transform hook from tools/ to your .git hooks dir.
1563. 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.
1594. Pull down the cPython git mirror: https://github.com/python/cpython.git
Chris Withers51239e42019-04-26 08:31:26 +01001605. Export the new revisions since the ``Last sync`::
Robert Collins18c9bbd2015-07-10 14:00:11 +1200161
Chris Withersfb1f5b32019-04-26 20:29:03 +0100162 start=`cat lastsync.txt`
Robert Collins18c9bbd2015-07-10 14:00:11 +1200163 rm migrate-export
Chris Withersfb1f5b32019-04-26 20:29:03 +0100164 git log --pretty="format:%H " $start.. -- Lib/unittest/mock.py \
Robert Collins18c9bbd2015-07-10 14:00:11 +1200165 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
1726. 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
1897. After the import is complete, update this document with the new sync point.
190
1918. 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.