@@ -33,25 +33,25 @@ Consider the following scenarios:
33
33
34
34
1. Modifying the behavior of a function or the property of a class for a test e.g.
35
35
there is an API call or database connection you will not make for a test but you know
36
- what the expected output should be. Use :py:meth: `monkeypatch.setattr ` to patch the
36
+ what the expected output should be. Use :py:meth: `monkeypatch.setattr <MonkeyPatch.setattr> ` to patch the
37
37
function or property with your desired testing behavior. This can include your own functions.
38
- Use :py:meth: `monkeypatch.delattr ` to remove the function or property for the test.
38
+ Use :py:meth: `monkeypatch.delattr <MonkeyPatch.delattr> ` to remove the function or property for the test.
39
39
40
40
2. Modifying the values of dictionaries e.g. you have a global configuration that
41
- you want to modify for certain test cases. Use :py:meth: `monkeypatch.setitem ` to patch the
42
- dictionary for the test. :py:meth: `monkeypatch.delitem ` can be used to remove items.
41
+ you want to modify for certain test cases. Use :py:meth: `monkeypatch.setitem <MonkeyPatch.setitem> ` to patch the
42
+ dictionary for the test. :py:meth: `monkeypatch.delitem <MonkeyPatch.delitem> ` can be used to remove items.
43
43
44
44
3. Modifying environment variables for a test e.g. to test program behavior if an
45
45
environment variable is missing, or to set multiple values to a known variable.
46
- :py:meth: `monkeypatch.setenv ` and :py:meth: `monkeypatch.delenv ` can be used for
46
+ :py:meth: `monkeypatch.setenv <MonkeyPatch.setenv> ` and :py:meth: `monkeypatch.delenv <MonkeyPatch.delenv> ` can be used for
47
47
these patches.
48
48
49
49
4. Use ``monkeypatch.setenv("PATH", value, prepend=os.pathsep) `` to modify ``$PATH ``, and
50
- :py:meth: `monkeypatch.chdir ` to change the context of the current working directory
50
+ :py:meth: `monkeypatch.chdir <MonkeyPatch.chdir> ` to change the context of the current working directory
51
51
during a test.
52
52
53
- 5. Use :py:meth: `monkeypatch.syspath_prepend ` to modify ``sys.path `` which will also
54
- call :py:meth: ` pkg_resources.fixup_namespace_packages ` and :py:meth : `importlib.invalidate_caches `.
53
+ 5. Use :py:meth: `monkeypatch.syspath_prepend <MonkeyPatch.syspath_prepend> ` to modify ``sys.path `` which will also
54
+ call `` pkg_resources.fixup_namespace_packages `` and :py:func : `importlib.invalidate_caches `.
55
55
56
56
See the `monkeypatch blog post `_ for some introduction material
57
57
and a discussion of its motivation.
@@ -66,10 +66,10 @@ testing, you do not want your test to depend on the running user. ``monkeypatch`
66
66
can be used to patch functions dependent on the user to always return a
67
67
specific value.
68
68
69
- In this example, :py:meth: `monkeypatch.setattr ` is used to patch ``Path.home ``
69
+ In this example, :py:meth: `monkeypatch.setattr <MonkeyPatch.setattr> ` is used to patch ``Path.home ``
70
70
so that the known testing path ``Path("/abc") `` is always used when the test is run.
71
71
This removes any dependency on the running user for testing purposes.
72
- :py:meth: `monkeypatch.setattr ` must be called before the function which will use
72
+ :py:meth: `monkeypatch.setattr <MonkeyPatch.setattr> ` must be called before the function which will use
73
73
the patched function is called.
74
74
After the test function finishes the ``Path.home `` modification will be undone.
75
75
@@ -102,7 +102,7 @@ After the test function finishes the ``Path.home`` modification will be undone.
102
102
Monkeypatching returned objects: building mock classes
103
103
------------------------------------------------------
104
104
105
- :py:meth: `monkeypatch.setattr ` can be used in conjunction with classes to mock returned
105
+ :py:meth: `monkeypatch.setattr <MonkeyPatch.setattr> ` can be used in conjunction with classes to mock returned
106
106
objects from functions instead of values.
107
107
Imagine a simple function to take an API url and return the json response.
108
108
@@ -330,7 +330,7 @@ This behavior can be moved into ``fixture`` structures and shared across tests:
330
330
Monkeypatching dictionaries
331
331
---------------------------
332
332
333
- :py:meth: `monkeypatch.setitem ` can be used to safely set the values of dictionaries
333
+ :py:meth: `monkeypatch.setitem <MonkeyPatch.setitem> ` can be used to safely set the values of dictionaries
334
334
to specific values during tests. Take this simplified connection string example:
335
335
336
336
.. code-block :: python
@@ -367,7 +367,7 @@ For testing purposes we can patch the ``DEFAULT_CONFIG`` dictionary to specific
367
367
result = app.create_connection_string()
368
368
assert result == expected
369
369
370
- You can use the :py:meth: `monkeypatch.delitem ` to remove values.
370
+ You can use the :py:meth: `monkeypatch.delitem <MonkeyPatch.delitem> ` to remove values.
371
371
372
372
.. code-block :: python
373
373
0 commit comments