Debian Codegraph @

testing/web-platform/tests/tools/third_party/pytest/doc/en/how-to/cache.rst

raw
1 .. _`cache_provider`:
2 .. _cache:
3
4
5 How to re-run failed tests and maintain state between test runs
6 ===============================================================
7
8
9
10 Usage
11 ---------
12
13 The plugin provides two command line options to rerun failures from the
14 last ``pytest`` invocation:
15
16 * ``--lf``, ``--last-failed`` - to only re-run the failures.
17 * ``--ff``, ``--failed-first`` - to run the failures first and then the rest of
18 the tests.
19
20 For cleanup (usually not needed), a ``--cache-clear`` option allows to remove
21 all cross-session cache contents ahead of a test run.
22
23 Other plugins may access the `config.cache`_ object to set/get
24 **json encodable** values between ``pytest`` invocations.
25
26 .. note::
27
28 This plugin is enabled by default, but can be disabled if needed: see
29 :ref:`cmdunregister` (the internal name for this plugin is
30 ``cacheprovider``).
31
32
33 Rerunning only failures or failures first
34 -----------------------------------------------
35
36 First, let's create 50 test invocation of which only 2 fail:
37
38 .. code-block:: python
39
40 # content of test_50.py
41 import pytest
42
43
44 @pytest.mark.parametrize("i", range(50))
45 def test_num(i):
46 if i in (17, 25):
47 pytest.fail("bad luck")
48
49 If you run this for the first time you will see two failures:
50
51 .. code-block:: pytest
52
53 $ pytest -q
54 .................F.......F........................ [100%]
55 ================================= FAILURES =================================
56 _______________________________ test_num[17] _______________________________
57
58 i = 17
59
60 @pytest.mark.parametrize("i", range(50))
61 def test_num(i):
62 if i in (17, 25):
63 > pytest.fail("bad luck")
64 E Failed: bad luck
65
66 test_50.py:7: Failed
67 _______________________________ test_num[25] _______________________________
68
69 i = 25
70
71 @pytest.mark.parametrize("i", range(50))
72 def test_num(i):
73 if i in (17, 25):
74 > pytest.fail("bad luck")
75 E Failed: bad luck
76
77 test_50.py:7: Failed
78 ========================= short test summary info ==========================
79 FAILED test_50.py::test_num[17] - Failed: bad luck
80 FAILED test_50.py::test_num[25] - Failed: bad luck
81 2 failed, 48 passed in 0.12s
82
83 If you then run it with ``--lf``:
84
85 .. code-block:: pytest
86
87 $ pytest --lf
88 =========================== test session starts ============================
89 platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
90 rootdir: /home/sweet/project
91 collected 2 items
92 run-last-failure: rerun previous 2 failures
93
94 test_50.py FF [100%]
95
96 ================================= FAILURES =================================
97 _______________________________ test_num[17] _______________________________
98
99 i = 17
100
101 @pytest.mark.parametrize("i", range(50))
102 def test_num(i):
103 if i in (17, 25):
104 > pytest.fail("bad luck")
105 E Failed: bad luck
106
107 test_50.py:7: Failed
108 _______________________________ test_num[25] _______________________________
109
110 i = 25
111
112 @pytest.mark.parametrize("i", range(50))
113 def test_num(i):
114 if i in (17, 25):
115 > pytest.fail("bad luck")
116 E Failed: bad luck
117
118 test_50.py:7: Failed
119 ========================= short test summary info ==========================
120 FAILED test_50.py::test_num[17] - Failed: bad luck
121 FAILED test_50.py::test_num[25] - Failed: bad luck
122 ============================ 2 failed in 0.12s =============================
123
124 You have run only the two failing tests from the last run, while the 48 passing
125 tests have not been run ("deselected").
126
127 Now, if you run with the ``--ff`` option, all tests will be run but the first
128 previous failures will be executed first (as can be seen from the series
129 of ``FF`` and dots):
130
131 .. code-block:: pytest
132
133 $ pytest --ff
134 =========================== test session starts ============================
135 platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
136 rootdir: /home/sweet/project
137 collected 50 items
138 run-last-failure: rerun previous 2 failures first
139
140 test_50.py FF................................................ [100%]
141
142 ================================= FAILURES =================================
143 _______________________________ test_num[17] _______________________________
144
145 i = 17
146
147 @pytest.mark.parametrize("i", range(50))
148 def test_num(i):
149 if i in (17, 25):
150 > pytest.fail("bad luck")
151 E Failed: bad luck
152
153 test_50.py:7: Failed
154 _______________________________ test_num[25] _______________________________
155
156 i = 25
157
158 @pytest.mark.parametrize("i", range(50))
159 def test_num(i):
160 if i in (17, 25):
161 > pytest.fail("bad luck")
162 E Failed: bad luck
163
164 test_50.py:7: Failed
165 ========================= short test summary info ==========================
166 FAILED test_50.py::test_num[17] - Failed: bad luck
167 FAILED test_50.py::test_num[25] - Failed: bad luck
168 ======================= 2 failed, 48 passed in 0.12s =======================
169
170 .. _`config.cache`:
171
172 New ``--nf``, ``--new-first`` options: run new tests first followed by the rest
173 of the tests, in both cases tests are also sorted by the file modified time,
174 with more recent files coming first.
175
176 Behavior when no tests failed in the last run
177 ---------------------------------------------
178
179 The ``--lfnf/--last-failed-no-failures`` option governs the behavior of ``--last-failed``.
180 Determines whether to execute tests when there are no previously (known)
181 failures or when no cached ``lastfailed`` data was found.
182
183 There are two options:
184
185 * ``all``: when there are no known test failures, runs all tests (the full test suite). This is the default.
186 * ``none``: when there are no known test failures, just emits a message stating this and exit successfully.
187
188 Example:
189
190 .. code-block:: bash
191
192 pytest --last-failed --last-failed-no-failures all # runs the full test suite (default behavior)
193 pytest --last-failed --last-failed-no-failures none # runs no tests and exits successfully
194
195 The new config.cache object
196 --------------------------------
197
198 .. regendoc:wipe
199
200 Plugins or conftest.py support code can get a cached value using the
201 pytest ``config`` object. Here is a basic example plugin which
202 implements a :ref:`fixture <fixture>` which re-uses previously created state
203 across pytest invocations:
204
205 .. code-block:: python
206
207 # content of test_caching.py
208 import pytest
209
210
211 def expensive_computation():
212 print("running expensive computation...")
213
214
215 @pytest.fixture
216 def mydata(pytestconfig):
217 val = pytestconfig.cache.get("example/value", None)
218 if val is None:
219 expensive_computation()
220 val = 42
221 pytestconfig.cache.set("example/value", val)
222 return val
223
224
225 def test_function(mydata):
226 assert mydata == 23
227
228 If you run this command for the first time, you can see the print statement:
229
230 .. code-block:: pytest
231
232 $ pytest -q
233 F [100%]
234 ================================= FAILURES =================================
235 ______________________________ test_function _______________________________
236
237 mydata = 42
238
239 def test_function(mydata):
240 > assert mydata == 23
241 E assert 42 == 23
242
243 test_caching.py:19: AssertionError
244 -------------------------- Captured stdout setup ---------------------------
245 running expensive computation...
246 ========================= short test summary info ==========================
247 FAILED test_caching.py::test_function - assert 42 == 23
248 1 failed in 0.12s
249
250 If you run it a second time, the value will be retrieved from
251 the cache and nothing will be printed:
252
253 .. code-block:: pytest
254
255 $ pytest -q
256 F [100%]
257 ================================= FAILURES =================================
258 ______________________________ test_function _______________________________
259
260 mydata = 42
261
262 def test_function(mydata):
263 > assert mydata == 23
264 E assert 42 == 23
265
266 test_caching.py:19: AssertionError
267 ========================= short test summary info ==========================
268 FAILED test_caching.py::test_function - assert 42 == 23
269 1 failed in 0.12s
270
271 See the :fixture:`config.cache fixture <cache>` for more details.
272
273
274 Inspecting Cache content
275 ------------------------
276
277 You can always peek at the content of the cache using the
278 ``--cache-show`` command line option:
279
280 .. code-block:: pytest
281
282 $ pytest --cache-show
283 =========================== test session starts ============================
284 platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
285 rootdir: /home/sweet/project
286 cachedir: /home/sweet/project/.pytest_cache
287 --------------------------- cache values for '*' ---------------------------
288 cache/lastfailed contains:
289 {'test_caching.py::test_function': True}
290 cache/nodeids contains:
291 ['test_caching.py::test_function']
292 cache/stepwise contains:
293 []
294 example/value contains:
295 42
296
297 ========================== no tests ran in 0.12s ===========================
298
299 ``--cache-show`` takes an optional argument to specify a glob pattern for
300 filtering:
301
302 .. code-block:: pytest
303
304 $ pytest --cache-show example/*
305 =========================== test session starts ============================
306 platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
307 rootdir: /home/sweet/project
308 cachedir: /home/sweet/project/.pytest_cache
309 ----------------------- cache values for 'example/*' -----------------------
310 example/value contains:
311 42
312
313 ========================== no tests ran in 0.12s ===========================
314
315 Clearing Cache content
316 ----------------------
317
318 You can instruct pytest to clear all cache files and values
319 by adding the ``--cache-clear`` option like this:
320
321 .. code-block:: bash
322
323 pytest --cache-clear
324
325 This is recommended for invocations from Continuous Integration
326 servers where isolation and correctness is more important
327 than speed.
328
329
330 .. _cache stepwise:
331
332 Stepwise
333 --------
334
335 As an alternative to ``--lf -x``, especially for cases where you expect a large part of the test suite will fail, ``--sw``, ``--stepwise`` allows you to fix them one at a time. The test suite will run until the first failure and then stop. At the next invocation, tests will continue from the last failing test and then run until the next failing test. You may use the ``--stepwise-skip`` option to ignore one failing test and stop the test execution on the second failing test instead. This is useful if you get stuck on a failing test and just want to ignore it until later. Providing ``--stepwise-skip`` will also enable ``--stepwise`` implicitly.