blob: 6c8e37b0eb7a8e978fc9584c8263c1229c68f3e9 [file] [log] [blame]
Michael Foord1e68bec2012-03-03 22:24:30 +00001====================================
2 Mock - Mocking and Testing Library
3====================================
4
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>`_
Robert Collins74eae172016-03-23 19:08:40 +130015:Last sync: cb6aab1248c4aec4dd578bea717854505a6fb55d
Michael Foord1e68bec2012-03-03 22:24:30 +000016
Robert Collins18c9bbd2015-07-10 14:00:11 +120017.. _Mock Homepage: https://github.com/testing-cabal/mock
18.. _BSD License: http://github.com/testing-cabal/mock/blob/master/LICENSE.txt
19.. _Python Docs: https://docs.python.org/dev/library/unittest.mock.html
Michael Foord1e68bec2012-03-03 22:24:30 +000020
21.. module:: mock
22 :synopsis: Mock object and testing library.
23
24.. index:: introduction
25
Robert Collins18c9bbd2015-07-10 14:00:11 +120026TOC
27+++
28
29.. toctree::
30 :maxdepth: 2
31
32 changelog
33
34Introduction
35++++++++++++
36
Michael Foord1e68bec2012-03-03 22:24:30 +000037mock is a library for testing in Python. It allows you to replace parts of
38your system under test with mock objects and make assertions about how they
39have been used.
40
Robert Collins18c9bbd2015-07-10 14:00:11 +120041mock is now part of the Python standard library, available as
42``unittest.mock`` in Python 3.3 onwards. However, if you are writing code that
43runs on multiple versions of Python the ``mock`` package is better, as you get
44the newest features from the latest release of Python available for all
45Pythons.
Michael Foord2df66112012-10-07 18:33:12 +010046
Robert Collins18c9bbd2015-07-10 14:00:11 +120047The ``mock`` package contains a rolling backport of the standard library mock
Hugob67fe8a2017-10-28 16:01:45 +030048code compatible with Python 2.7 and 3.3 and up.
49
50* Python 2.6 is supported by mock 2.0.0 and below.
51
52* Python 3.2 is supported by mock 1.3.0 and below - with pip no longer
53supporting 3.2, we cannot test against that version anymore.
Michael Foord1e68bec2012-03-03 22:24:30 +000054
Robert Collins18c9bbd2015-07-10 14:00:11 +120055Please see the standard library documentation for usage details.
Michael Foord1e68bec2012-03-03 22:24:30 +000056
57.. index:: installing
Robert Collins18c9bbd2015-07-10 14:00:11 +120058.. _installing:
Michael Foord1e68bec2012-03-03 22:24:30 +000059
60Installing
Robert Collins18c9bbd2015-07-10 14:00:11 +120061++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +000062
Robert Collins18c9bbd2015-07-10 14:00:11 +120063The current version is |release|. Mock is stable and widely used.
Michael Foord1e68bec2012-03-03 22:24:30 +000064
65* `mock on PyPI <http://pypi.python.org/pypi/mock>`_
Michael Foord1e68bec2012-03-03 22:24:30 +000066
67.. index:: repository
Robert Collins18c9bbd2015-07-10 14:00:11 +120068.. index:: git
Michael Foord1e68bec2012-03-03 22:24:30 +000069
Hugob67fe8a2017-10-28 16:01:45 +030070You can checkout the latest development version from GitHub
Michael Foord1e68bec2012-03-03 22:24:30 +000071repository with the following command:
72
Robert Collins18c9bbd2015-07-10 14:00:11 +120073 ``git clone https://github.com/testing-cabal/mock``
Michael Foord1e68bec2012-03-03 22:24:30 +000074
75
76.. index:: pip
Michael Foord1e68bec2012-03-03 22:24:30 +000077
Robert Collins18c9bbd2015-07-10 14:00:11 +120078You can install mock with pip:
Michael Foord1e68bec2012-03-03 22:24:30 +000079
Michael Foord1e68bec2012-03-03 22:24:30 +000080 | ``pip install -U mock``
81
82Alternatively you can download the mock distribution from PyPI and after
83unpacking run:
84
85 ``python setup.py install``
86
87
Robert Collins18c9bbd2015-07-10 14:00:11 +120088.. index:: bug reports
Michael Foord1e68bec2012-03-03 22:24:30 +000089
Robert Collins18c9bbd2015-07-10 14:00:11 +120090Bug Reports
91+++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +000092
93Mock uses `unittest2 <http://pypi.python.org/pypi/unittest2>`_ for its own
Robert Collins18c9bbd2015-07-10 14:00:11 +120094Issues with the backport process, such as compatibility with a particular
95Python, should be reported to the `bug tracker
96<https://github.com/testing-cabal/mock/issues>`_. Feature requests and issues
97with Mock functionality should be reported to the `Python bug tracker
98<https://bugs.python.org>`_.
Michael Foord1e68bec2012-03-03 22:24:30 +000099
Robert Collins18c9bbd2015-07-10 14:00:11 +1200100.. index:: python changes
Michael Foord1e68bec2012-03-03 22:24:30 +0000101
Robert Collins18c9bbd2015-07-10 14:00:11 +1200102Python Changes
103++++++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +0000104
Robert Collins18c9bbd2015-07-10 14:00:11 +1200105Python NEWS entries from cPython:
Michael Foord1e68bec2012-03-03 22:24:30 +0000106
Robert Collins18c9bbd2015-07-10 14:00:11 +1200107.. include:: ../NEWS
Michael Foord1e68bec2012-03-03 22:24:30 +0000108
109.. index:: older versions
110
Robert Collinsdff3a7b2015-08-04 09:28:39 +1200111Older Versions of Python
112++++++++++++++++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +0000113
Robert Collinsdff3a7b2015-08-04 09:28:39 +1200114Version 1.0.1 is the last version compatible with Python < 2.6.
Michael Foord1e68bec2012-03-03 22:24:30 +0000115
Hugob67fe8a2017-10-28 16:01:45 +0300116Version 2.0.0 is the last version compatible with Python 2.6.
117
Robert Collins18c9bbd2015-07-10 14:00:11 +1200118.. index:: maintainer notes
Michael Foord1e68bec2012-03-03 22:24:30 +0000119
Robert Collins18c9bbd2015-07-10 14:00:11 +1200120Maintainer Notes
121++++++++++++++++
Michael Foord1e68bec2012-03-03 22:24:30 +0000122
Robert Collins18c9bbd2015-07-10 14:00:11 +1200123Development
Michael Foord1e68bec2012-03-03 22:24:30 +0000124===========
125
Robert Collins18c9bbd2015-07-10 14:00:11 +1200126Checkout from git (see :ref:`installing`) and submit pull requests.
Michael Foord1e68bec2012-03-03 22:24:30 +0000127
Robert Collins18c9bbd2015-07-10 14:00:11 +1200128Committers can just push as desired: since all semantic development takes
129place in cPython, the backport process is as lightweight as we can make it.
Michael Foord1e68bec2012-03-03 22:24:30 +0000130
Hugob67fe8a2017-10-28 16:01:45 +0300131mock is CI tested using Travis-CI on Python versions 2.7, 3.3, 3.4,
Darragh Bailey0cba1ba2015-07-10 17:58:22 +01001323.5, nightly Python 3 builds, pypy, pypy3. Jython support is desired, if
lord6305a0ef02015-09-23 10:47:31 +0800133someone could contribute a patch to .travis.yml to support it that would be
Robert Collins18c9bbd2015-07-10 14:00:11 +1200134excellent.
Michael Foord1e68bec2012-03-03 22:24:30 +0000135
Robert Collins18c9bbd2015-07-10 14:00:11 +1200136Releasing
137=========
Michael Foord1e68bec2012-03-03 22:24:30 +0000138
Robert Collinse795a4a2015-07-10 14:32:36 +1200139NB: please use semver. Bump the major component on API breaks, minor on all
140non-bugfix changes, patch on bugfix only changes.
141
Robert Collins18c9bbd2015-07-10 14:00:11 +12001421. tag -s, push --tags origin master
1432. setup.py sdist bdist_wheel upload -s
Michael Foord1e68bec2012-03-03 22:24:30 +0000144
Robert Collins18c9bbd2015-07-10 14:00:11 +1200145
146Backporting rules
147=================
148
149isinstance checks in cPython to ``type`` need to check ``ClassTypes``.
150Code calling ``obj.isidentifier`` needs to change to ``_isidentifier(obj)``.
151
152Backporting process
153===================
154
1551. Patch your git am with `my patch <https://github.com/rbtcollins/git>`_.
1562. Install the applypatch-transform hook from tools/ to your .git hooks dir.
1573. Configure a pre-applypatch hook to test at least all the cPython versions
158 we support on each patch that is applied. I use containers, and a sample
159 script is in tools/pre-applypatch.
1604. Pull down the cPython git mirror: https://github.com/python/cpython.git
1615. Export the new revisions since the ``Last sync`` at the top of this
162 document::
163
Robert Collinse34a7cb2015-07-24 07:52:01 +1200164 revs=${lastsync}
Robert Collins18c9bbd2015-07-10 14:00:11 +1200165 rm migrate-export
Robert Collinscce4e5b2015-07-14 14:13:20 +1200166 git log --pretty="format:%H " $revs.. -- Lib/unittest/mock.py \
Robert Collins18c9bbd2015-07-10 14:00:11 +1200167 Lib/unittest/test/testmock/ > migrate-revs
168 tac migrate-revs > migrate-sorted-revs
169 for rev in $(< migrate-sorted-revs); do
170 git format-patch -1 $rev -k --stdout >> migrate-export;
171 done
172 echo NEW SYNC POINT: $(git rev-parse HEAD)
173
1746. Import into mock::
175
176 git am -k --reject $path-to-cpython/migrate-export
177
178 This will transform the patches automatically. Currently it will error
179 on every NEWS change as I haven't gotten around to making those patches
180 automatic. Fixup any errors that occur. When the patch is ready, do a ``git
181 add -u`` to update the index and then ``git am --continue`` to move onto
182 the next patch. If the patch is inappropriate e.g. the patch removing
183 __ne__ which would break older pythons, then either do ``git reset --hard;
184 git am --skip`` to discard any partially applied changes and skip over it,
185 or, if it has a NEWS entry thats worth preserving, edit it down to just
186 that, with a note such as we have for the ``__ne__`` patch, and continue on
187 from there.
188
189 The goal is that every patch work at all times.
190
1917. After the import is complete, update this document with the new sync point.
192
1938. Push to a personal branch and propose a PR to the main repo. This will make
194 Travis-CI test it. If it works, push to the main repo.