@@ -161,6 +161,243 @@ will be shown (because KeyboardInterrupt is caught by pytest). By using this
161
161
option you make sure a trace is shown.
162
162
163
163
164
+ Verbosity
165
+ ---------
166
+
167
+ The ``-v `` flag controls the verbosity of pytest output in various aspects: test session progress, assertion
168
+ details when tests fail, fixtures details with ``--fixtures ``, etc.
169
+
170
+ .. regendoc:wipe
171
+
172
+ Consider this simple file:
173
+
174
+ .. code-block :: python
175
+
176
+ # content of test_verbosity_example.py
177
+ def test_ok ():
178
+ pass
179
+
180
+
181
+ def test_words_fail ():
182
+ fruits1 = [" banana" , " apple" , " grapes" , " melon" , " kiwi" ]
183
+ fruits2 = [" banana" , " apple" , " orange" , " melon" , " kiwi" ]
184
+ assert fruits1 == fruits2
185
+
186
+
187
+ def test_numbers_fail ():
188
+ number_to_text1 = {str (x): x for x in range (5 )}
189
+ number_to_text2 = {str (x * 10 ): x * 10 for x in range (5 )}
190
+ assert number_to_text1 == number_to_text2
191
+
192
+
193
+ def test_long_text_fail ():
194
+ long_text = " Lorem ipsum dolor sit amet " * 10
195
+ assert " hello world" in long_text
196
+
197
+ Executing pytest normally gives us this output (we are skipping the header to focus on the rest):
198
+
199
+ .. code-block :: pytest
200
+
201
+ $ pytest --no-header
202
+ =========================== test session starts ===========================
203
+ collected 4 items
204
+
205
+ test_verbosity_example.py .FFF [100%]
206
+
207
+ ================================ FAILURES =================================
208
+ _____________________________ test_words_fail _____________________________
209
+
210
+ def test_words_fail():
211
+ fruits1 = ["banana", "apple", "grapes", "melon", "kiwi"]
212
+ fruits2 = ["banana", "apple", "orange", "melon", "kiwi"]
213
+ > assert fruits1 == fruits2
214
+ E AssertionError: assert ['banana', 'a...elon', 'kiwi'] == ['banana', 'a...elon', 'kiwi']
215
+ E At index 2 diff: 'grapes' != 'orange'
216
+ E Use -v to get the full diff
217
+
218
+ test_verbosity_example.py:8: AssertionError
219
+ ____________________________ test_numbers_fail ____________________________
220
+
221
+ def test_numbers_fail():
222
+ number_to_text1 = {str(x): x for x in range(5)}
223
+ number_to_text2 = {str(x * 10): x * 10 for x in range(5)}
224
+ > assert number_to_text1 == number_to_text2
225
+ E AssertionError: assert {'0': 0, '1':..., '3': 3, ...} == {'0': 0, '10'...'30': 30, ...}
226
+ E Omitting 1 identical items, use -vv to show
227
+ E Left contains 4 more items:
228
+ E {'1': 1, '2': 2, '3': 3, '4': 4}
229
+ E Right contains 4 more items:
230
+ E {'10': 10, '20': 20, '30': 30, '40': 40}
231
+ E Use -v to get the full diff
232
+
233
+ test_verbosity_example.py:14: AssertionError
234
+ ___________________________ test_long_text_fail ___________________________
235
+
236
+ def test_long_text_fail():
237
+ long_text = "Lorem ipsum dolor sit amet " * 10
238
+ > assert "hello world" in long_text
239
+ E AssertionError: assert 'hello world' in 'Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ips... sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet '
240
+
241
+ test_verbosity_example.py:19: AssertionError
242
+ ========================= short test summary info =========================
243
+ FAILED test_verbosity_example.py::test_words_fail - AssertionError: asser...
244
+ FAILED test_verbosity_example.py::test_numbers_fail - AssertionError: ass...
245
+ FAILED test_verbosity_example.py::test_long_text_fail - AssertionError: a...
246
+ ======================= 3 failed, 1 passed in 0.08s =======================
247
+
248
+ Notice that:
249
+
250
+ * Each test inside the file is shown by a single character in the output: ``. `` for passing, ``F `` for failure.
251
+ * ``test_words_fail `` failed, and we are shown a short summary indicating the index 2 of the two lists differ.
252
+ * ``test_numbers_fail `` failed, and we are shown a summary of left/right differences on dictionary items. Identical items are omitted.
253
+ * ``test_long_text_fail `` failed, and the right hand side of the ``in `` statement is truncated using ``...` ``
254
+ because it is longer than an internal threshold (240 characters currently).
255
+
256
+ Now we can increase pytest's verbosity:
257
+
258
+ .. code-block :: pytest
259
+
260
+ $ pytest --no-header -v
261
+ =========================== test session starts ===========================
262
+ collecting ... collected 4 items
263
+
264
+ test_verbosity_example.py::test_ok PASSED [ 25%]
265
+ test_verbosity_example.py::test_words_fail FAILED [ 50%]
266
+ test_verbosity_example.py::test_numbers_fail FAILED [ 75%]
267
+ test_verbosity_example.py::test_long_text_fail FAILED [100%]
268
+
269
+ ================================ FAILURES =================================
270
+ _____________________________ test_words_fail _____________________________
271
+
272
+ def test_words_fail():
273
+ fruits1 = ["banana", "apple", "grapes", "melon", "kiwi"]
274
+ fruits2 = ["banana", "apple", "orange", "melon", "kiwi"]
275
+ > assert fruits1 == fruits2
276
+ E AssertionError: assert ['banana', 'a...elon', 'kiwi'] == ['banana', 'a...elon', 'kiwi']
277
+ E At index 2 diff: 'grapes' != 'orange'
278
+ E Full diff:
279
+ E - ['banana', 'apple', 'orange', 'melon', 'kiwi']
280
+ E ? ^ ^^
281
+ E + ['banana', 'apple', 'grapes', 'melon', 'kiwi']
282
+ E ? ^ ^ +
283
+
284
+ test_verbosity_example.py:8: AssertionError
285
+ ____________________________ test_numbers_fail ____________________________
286
+
287
+ def test_numbers_fail():
288
+ number_to_text1 = {str(x): x for x in range(5)}
289
+ number_to_text2 = {str(x * 10): x * 10 for x in range(5)}
290
+ > assert number_to_text1 == number_to_text2
291
+ E AssertionError: assert {'0': 0, '1':..., '3': 3, ...} == {'0': 0, '10'...'30': 30, ...}
292
+ E Omitting 1 identical items, use -vv to show
293
+ E Left contains 4 more items:
294
+ E {'1': 1, '2': 2, '3': 3, '4': 4}
295
+ E Right contains 4 more items:
296
+ E {'10': 10, '20': 20, '30': 30, '40': 40}
297
+ E Full diff:
298
+ E - {'0': 0, '10': 10, '20': 20, '30': 30, '40': 40}...
299
+ E
300
+ E ...Full output truncated (3 lines hidden), use '-vv' to show
301
+
302
+ test_verbosity_example.py:14: AssertionError
303
+ ___________________________ test_long_text_fail ___________________________
304
+
305
+ def test_long_text_fail():
306
+ long_text = "Lorem ipsum dolor sit amet " * 10
307
+ > assert "hello world" in long_text
308
+ E AssertionError: assert 'hello world' in 'Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet '
309
+
310
+ test_verbosity_example.py:19: AssertionError
311
+ ========================= short test summary info =========================
312
+ FAILED test_verbosity_example.py::test_words_fail - AssertionError: asser...
313
+ FAILED test_verbosity_example.py::test_numbers_fail - AssertionError: ass...
314
+ FAILED test_verbosity_example.py::test_long_text_fail - AssertionError: a...
315
+ ======================= 3 failed, 1 passed in 0.07s =======================
316
+
317
+ Notice now that:
318
+
319
+ * Each test inside the file gets its own line in the output.
320
+ * ``test_words_fail `` now shows the two failing lists in full, in addition to which index differs.
321
+ * ``test_numbers_fail `` now shows a text diff of the two dictionaries, truncated.
322
+ * ``test_long_text_fail `` no longer truncates the right hand side of the ``in `` statement, because the internal
323
+ threshold for truncation is larger now (2400 characters currently).
324
+
325
+ Now if we increase verbosity even more:
326
+
327
+ .. code-block :: pytest
328
+
329
+ $ pytest --no-header -vv
330
+ =========================== test session starts ===========================
331
+ collecting ... collected 4 items
332
+
333
+ test_verbosity_example.py::test_ok PASSED [ 25%]
334
+ test_verbosity_example.py::test_words_fail FAILED [ 50%]
335
+ test_verbosity_example.py::test_numbers_fail FAILED [ 75%]
336
+ test_verbosity_example.py::test_long_text_fail FAILED [100%]
337
+
338
+ ================================ FAILURES =================================
339
+ _____________________________ test_words_fail _____________________________
340
+
341
+ def test_words_fail():
342
+ fruits1 = ["banana", "apple", "grapes", "melon", "kiwi"]
343
+ fruits2 = ["banana", "apple", "orange", "melon", "kiwi"]
344
+ > assert fruits1 == fruits2
345
+ E AssertionError: assert ['banana', 'apple', 'grapes', 'melon', 'kiwi'] == ['banana', 'apple', 'orange', 'melon', 'kiwi']
346
+ E At index 2 diff: 'grapes' != 'orange'
347
+ E Full diff:
348
+ E - ['banana', 'apple', 'orange', 'melon', 'kiwi']
349
+ E ? ^ ^^
350
+ E + ['banana', 'apple', 'grapes', 'melon', 'kiwi']
351
+ E ? ^ ^ +
352
+
353
+ test_verbosity_example.py:8: AssertionError
354
+ ____________________________ test_numbers_fail ____________________________
355
+
356
+ def test_numbers_fail():
357
+ number_to_text1 = {str(x): x for x in range(5)}
358
+ number_to_text2 = {str(x * 10): x * 10 for x in range(5)}
359
+ > assert number_to_text1 == number_to_text2
360
+ E AssertionError: assert {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4} == {'0': 0, '10': 10, '20': 20, '30': 30, '40': 40}
361
+ E Common items:
362
+ E {'0': 0}
363
+ E Left contains 4 more items:
364
+ E {'1': 1, '2': 2, '3': 3, '4': 4}
365
+ E Right contains 4 more items:
366
+ E {'10': 10, '20': 20, '30': 30, '40': 40}
367
+ E Full diff:
368
+ E - {'0': 0, '10': 10, '20': 20, '30': 30, '40': 40}
369
+ E ? - - - - - - - -
370
+ E + {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}
371
+
372
+ test_verbosity_example.py:14: AssertionError
373
+ ___________________________ test_long_text_fail ___________________________
374
+
375
+ def test_long_text_fail():
376
+ long_text = "Lorem ipsum dolor sit amet " * 10
377
+ > assert "hello world" in long_text
378
+ E AssertionError: assert 'hello world' in 'Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet '
379
+
380
+ test_verbosity_example.py:19: AssertionError
381
+ ========================= short test summary info =========================
382
+ FAILED test_verbosity_example.py::test_words_fail - AssertionError: asser...
383
+ FAILED test_verbosity_example.py::test_numbers_fail - AssertionError: ass...
384
+ FAILED test_verbosity_example.py::test_long_text_fail - AssertionError: a...
385
+ ======================= 3 failed, 1 passed in 0.07s =======================
386
+
387
+ Notice now that:
388
+
389
+ * Each test inside the file gets its own line in the output.
390
+ * ``test_words_fail `` gives the same output as before in this case.
391
+ * ``test_numbers_fail `` now shows a full text diff of the two dictionaries.
392
+ * ``test_long_text_fail `` also doesn't truncate on the right hand side as before, but now pytest won't truncate any
393
+ text at all, regardless of its size.
394
+
395
+ Those were examples of how verbosity affects normal test session output, but verbosity also is used in other
396
+ situations, for example you are shown even fixtures that start with ``_ `` if you use ``pytest --fixtures -v ``.
397
+
398
+ Using higher verbosity levels (``-vvv ``, ``-vvvv ``, ...) is supported, but has no effect in pytest itself at the moment,
399
+ however some plugins might make use of higher verbosity.
400
+
164
401
.. _`pytest.detailed_failed_tests_usage` :
165
402
166
403
Producing a detailed summary report
@@ -171,6 +408,8 @@ making it easy in large test suites to get a clear picture of all failures, skip
171
408
172
409
It defaults to ``fE `` to list failures and errors.
173
410
411
+ .. regendoc:wipe
412
+
174
413
Example:
175
414
176
415
.. code-block :: python
0 commit comments