1
1
pytest-mock
2
2
===========
3
3
4
- Thin-wrapper around the mock package for easier use with py.test.
5
-
6
- This plugin provides a fixture named `mock ` that has the same patching API
4
+ This plugin installs a fixture ``mock `` which is a thin-wrapper around the patching API
7
5
provided by the excellent `mock <http://pypi.python.org/pypi/mock >`_ package,
8
6
but with the benefit of not having to worry about undoing patches at the end
9
- of a test::
7
+ of a test:
8
+
9
+ .. code-block :: python
10
10
11
11
12
12
def test_unix_fs (mock ):
@@ -18,9 +18,11 @@ of a test::
18
18
Usage
19
19
-----
20
20
21
- The `mock ` fixture has the same API as
22
- `mock.patch <http://www.voidspace.org.uk/python/mock/patch.html#patch-decorators >`_,
23
- supporting the same arguments::
21
+ The ``mock `` fixture has the same API as
22
+ `mock.patch <http://www.voidspace.org.uk/python/mock/patch.html#patch-decorators >`_,
23
+ supporting the same arguments:
24
+
25
+ .. code-block :: python
24
26
25
27
# all valid calls
26
28
mock.patch(' os.remove' )
@@ -29,20 +31,22 @@ The `mock` fixture has the same API as
29
31
30
32
The supported methods are:
31
33
32
- - ` mock.patch `: see http://www.voidspace.org.uk/python/mock/patch.html#patch.
33
- - ` mock.patch.object `: see `http://www.voidspace.org.uk/python/mock/patch.html#patch-object.
34
- - ` mock.patch.multiple`: see `http://www.voidspace.org.uk/python/mock/patch.html#patch-multiple.
35
- - ` mock.stopall()`: stops all active patches at this point.
34
+ * `` mock.patch ` `: see http://www.voidspace.org.uk/python/mock/patch.html#patch.
35
+ * `` mock.patch.object ` `: see `http://www.voidspace.org.uk/python/mock/patch.html#patch-object.
36
+ * `` mock.patch.multiple` `: see `http://www.voidspace.org.uk/python/mock/patch.html#patch-multiple.
37
+ * `` mock.stopall()` `: stops all active patches at this point.
36
38
37
39
Why bother with a plugin?
38
40
-------------------------
39
41
40
- There are a number of different `patch ` usages in the standard `mock ` API,
41
- but IMHO they don't scale very well when you have a more than one or two
42
- patches to apply.
42
+ There are a number of different `` patch `` usages in the standard `` mock ` ` API,
43
+ but IMHO they don't scale very well when you have a more than one or two
44
+ patches to apply.
43
45
44
- It may lead to an excessive nesting of `with ` statements, breaking the flow
45
- of the test::
46
+ It may lead to an excessive nesting of ``with `` statements, breaking the flow
47
+ of the test:
48
+
49
+ .. code-block :: python
46
50
47
51
import mock
48
52
@@ -60,8 +64,10 @@ It may lead to an excessive nesting of `with` statements, breaking the flow
60
64
# ...
61
65
62
66
63
- One can use `patch ` as a decorator to improve the flow of the test, but now the
64
- test functions must receive the mock objects::
67
+ One can use ``patch `` as a decorator to improve the flow of the test, but now the
68
+ test functions must receive the mock objects:
69
+
70
+ .. code-block :: python
65
71
66
72
@mock.patch (' os.remove' )
67
73
@mock.patch (' os.listdir' )
0 commit comments