Debian Codegraph @

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

raw
1 .. _how-to-manage-output:
2
3 Managing pytest's output
4 =========================
5
6 .. _how-to-modifying-python-tb-printing:
7
8 Modifying Python traceback printing
9 --------------------------------------------------
10
11 Examples for modifying traceback printing:
12
13 .. code-block:: bash
14
15 pytest --showlocals # show local variables in tracebacks
16 pytest -l # show local variables (shortcut)
17 pytest --no-showlocals # hide local variables (if addopts enables them)
18
19 pytest --capture=fd # default, capture at the file descriptor level
20 pytest --capture=sys # capture at the sys level
21 pytest --capture=no # don't capture
22 pytest -s # don't capture (shortcut)
23 pytest --capture=tee-sys # capture to logs but also output to sys level streams
24
25 pytest --tb=auto # (default) 'long' tracebacks for the first and last
26 # entry, but 'short' style for the other entries
27 pytest --tb=long # exhaustive, informative traceback formatting
28 pytest --tb=short # shorter traceback format
29 pytest --tb=line # only one line per failure
30 pytest --tb=native # Python standard library formatting
31 pytest --tb=no # no traceback at all
32
33 The ``--full-trace`` causes very long traces to be printed on error (longer
34 than ``--tb=long``). It also ensures that a stack trace is printed on
35 **KeyboardInterrupt** (Ctrl+C).
36 This is very useful if the tests are taking too long and you interrupt them
37 with Ctrl+C to find out where the tests are *hanging*. By default no output
38 will be shown (because KeyboardInterrupt is caught by pytest). By using this
39 option you make sure a trace is shown.
40
41
42 Verbosity
43 --------------------------------------------------
44
45 Examples for modifying printing verbosity:
46
47 .. code-block:: bash
48
49 pytest --quiet # quiet - less verbose - mode
50 pytest -q # quiet - less verbose - mode (shortcut)
51 pytest -v # increase verbosity, display individual test names
52 pytest -vv # more verbose, display more details from the test output
53 pytest -vvv # not a standard , but may be used for even more detail in certain setups
54
55 The ``-v`` flag controls the verbosity of pytest output in various aspects: test session progress, assertion
56 details when tests fail, fixtures details with ``--fixtures``, etc.
57
58 .. regendoc:wipe
59
60 Consider this simple file:
61
62 .. code-block:: python
63
64 # content of test_verbosity_example.py
65 def test_ok():
66 pass
67
68
69 def test_words_fail():
70 fruits1 = ["banana", "apple", "grapes", "melon", "kiwi"]
71 fruits2 = ["banana", "apple", "orange", "melon", "kiwi"]
72 assert fruits1 == fruits2
73
74
75 def test_numbers_fail():
76 number_to_text1 = {str(x): x for x in range(5)}
77 number_to_text2 = {str(x * 10): x * 10 for x in range(5)}
78 assert number_to_text1 == number_to_text2
79
80
81 def test_long_text_fail():
82 long_text = "Lorem ipsum dolor sit amet " * 10
83 assert "hello world" in long_text
84
85 Executing pytest normally gives us this output (we are skipping the header to focus on the rest):
86
87 .. code-block:: pytest
88
89 $ pytest --no-header
90 =========================== test session starts ============================
91 collected 4 items
92
93 test_verbosity_example.py .FFF [100%]
94
95 ================================= FAILURES =================================
96 _____________________________ test_words_fail ______________________________
97
98 def test_words_fail():
99 fruits1 = ["banana", "apple", "grapes", "melon", "kiwi"]
100 fruits2 = ["banana", "apple", "orange", "melon", "kiwi"]
101 > assert fruits1 == fruits2
102 E AssertionError: assert ['banana', 'a...elon', 'kiwi'] == ['banana', 'a...elon', 'kiwi']
103 E
104 E At index 2 diff: 'grapes' != 'orange'
105 E Use -v to get more diff
106
107 test_verbosity_example.py:8: AssertionError
108 ____________________________ test_numbers_fail _____________________________
109
110 def test_numbers_fail():
111 number_to_text1 = {str(x): x for x in range(5)}
112 number_to_text2 = {str(x * 10): x * 10 for x in range(5)}
113 > assert number_to_text1 == number_to_text2
114 E AssertionError: assert {'0': 0, '1':..., '3': 3, ...} == {'0': 0, '10'...'30': 30, ...}
115 E
116 E Omitting 1 identical items, use -vv to show
117 E Left contains 4 more items:
118 E {'1': 1, '2': 2, '3': 3, '4': 4}
119 E Right contains 4 more items:
120 E {'10': 10, '20': 20, '30': 30, '40': 40}
121 E Use -v to get more diff
122
123 test_verbosity_example.py:14: AssertionError
124 ___________________________ test_long_text_fail ____________________________
125
126 def test_long_text_fail():
127 long_text = "Lorem ipsum dolor sit amet " * 10
128 > assert "hello world" in long_text
129 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 '
130
131 test_verbosity_example.py:19: AssertionError
132 ========================= short test summary info ==========================
133 FAILED test_verbosity_example.py::test_words_fail - AssertionError: asser...
134 FAILED test_verbosity_example.py::test_numbers_fail - AssertionError: ass...
135 FAILED test_verbosity_example.py::test_long_text_fail - AssertionError: a...
136 ======================= 3 failed, 1 passed in 0.12s ========================
137
138 Notice that:
139
140 * Each test inside the file is shown by a single character in the output: ``.`` for passing, ``F`` for failure.
141 * ``test_words_fail`` failed, and we are shown a short summary indicating the index 2 of the two lists differ.
142 * ``test_numbers_fail`` failed, and we are shown a summary of left/right differences on dictionary items. Identical items are omitted.
143 * ``test_long_text_fail`` failed, and the right hand side of the ``in`` statement is truncated using ``...```
144 because it is longer than an internal threshold (240 characters currently).
145
146 Now we can increase pytest's verbosity:
147
148 .. code-block:: pytest
149
150 $ pytest --no-header -v
151 =========================== test session starts ============================
152 collecting ... collected 4 items
153
154 test_verbosity_example.py::test_ok PASSED [ 25%]
155 test_verbosity_example.py::test_words_fail FAILED [ 50%]
156 test_verbosity_example.py::test_numbers_fail FAILED [ 75%]
157 test_verbosity_example.py::test_long_text_fail FAILED [100%]
158
159 ================================= FAILURES =================================
160 _____________________________ test_words_fail ______________________________
161
162 def test_words_fail():
163 fruits1 = ["banana", "apple", "grapes", "melon", "kiwi"]
164 fruits2 = ["banana", "apple", "orange", "melon", "kiwi"]
165 > assert fruits1 == fruits2
166 E AssertionError: assert ['banana', 'a...elon', 'kiwi'] == ['banana', 'a...elon', 'kiwi']
167 E
168 E At index 2 diff: 'grapes' != 'orange'
169 E
170 E Full diff:
171 E [
172 E 'banana',
173 E 'apple',...
174 E
175 E ...Full output truncated (7 lines hidden), use '-vv' to show
176
177 test_verbosity_example.py:8: AssertionError
178 ____________________________ test_numbers_fail _____________________________
179
180 def test_numbers_fail():
181 number_to_text1 = {str(x): x for x in range(5)}
182 number_to_text2 = {str(x * 10): x * 10 for x in range(5)}
183 > assert number_to_text1 == number_to_text2
184 E AssertionError: assert {'0': 0, '1':..., '3': 3, ...} == {'0': 0, '10'...'30': 30, ...}
185 E
186 E Omitting 1 identical items, use -vv to show
187 E Left contains 4 more items:
188 E {'1': 1, '2': 2, '3': 3, '4': 4}
189 E Right contains 4 more items:
190 E {'10': 10, '20': 20, '30': 30, '40': 40}
191 E ...
192 E
193 E ...Full output truncated (16 lines hidden), use '-vv' to show
194
195 test_verbosity_example.py:14: AssertionError
196 ___________________________ test_long_text_fail ____________________________
197
198 def test_long_text_fail():
199 long_text = "Lorem ipsum dolor sit amet " * 10
200 > assert "hello world" in long_text
201 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 '
202
203 test_verbosity_example.py:19: AssertionError
204 ========================= short test summary info ==========================
205 FAILED test_verbosity_example.py::test_words_fail - AssertionError: asser...
206 FAILED test_verbosity_example.py::test_numbers_fail - AssertionError: ass...
207 FAILED test_verbosity_example.py::test_long_text_fail - AssertionError: a...
208 ======================= 3 failed, 1 passed in 0.12s ========================
209
210 Notice now that:
211
212 * Each test inside the file gets its own line in the output.
213 * ``test_words_fail`` now shows the two failing lists in full, in addition to which index differs.
214 * ``test_numbers_fail`` now shows a text diff of the two dictionaries, truncated.
215 * ``test_long_text_fail`` no longer truncates the right hand side of the ``in`` statement, because the internal
216 threshold for truncation is larger now (2400 characters currently).
217
218 Now if we increase verbosity even more:
219
220 .. code-block:: pytest
221
222 $ pytest --no-header -vv
223 =========================== test session starts ============================
224 collecting ... collected 4 items
225
226 test_verbosity_example.py::test_ok PASSED [ 25%]
227 test_verbosity_example.py::test_words_fail FAILED [ 50%]
228 test_verbosity_example.py::test_numbers_fail FAILED [ 75%]
229 test_verbosity_example.py::test_long_text_fail FAILED [100%]
230
231 ================================= FAILURES =================================
232 _____________________________ test_words_fail ______________________________
233
234 def test_words_fail():
235 fruits1 = ["banana", "apple", "grapes", "melon", "kiwi"]
236 fruits2 = ["banana", "apple", "orange", "melon", "kiwi"]
237 > assert fruits1 == fruits2
238 E AssertionError: assert ['banana', 'apple', 'grapes', 'melon', 'kiwi'] == ['banana', 'apple', 'orange', 'melon', 'kiwi']
239 E
240 E At index 2 diff: 'grapes' != 'orange'
241 E
242 E Full diff:
243 E [
244 E 'banana',
245 E 'apple',
246 E - 'orange',
247 E ? ^ ^^
248 E + 'grapes',
249 E ? ^ ^ +
250 E 'melon',
251 E 'kiwi',
252 E ]
253
254 test_verbosity_example.py:8: AssertionError
255 ____________________________ test_numbers_fail _____________________________
256
257 def test_numbers_fail():
258 number_to_text1 = {str(x): x for x in range(5)}
259 number_to_text2 = {str(x * 10): x * 10 for x in range(5)}
260 > assert number_to_text1 == number_to_text2
261 E AssertionError: assert {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4} == {'0': 0, '10': 10, '20': 20, '30': 30, '40': 40}
262 E
263 E Common items:
264 E {'0': 0}
265 E Left contains 4 more items:
266 E {'1': 1, '2': 2, '3': 3, '4': 4}
267 E Right contains 4 more items:
268 E {'10': 10, '20': 20, '30': 30, '40': 40}
269 E
270 E Full diff:
271 E {
272 E '0': 0,
273 E - '10': 10,
274 E ? - -
275 E + '1': 1,
276 E - '20': 20,
277 E ? - -
278 E + '2': 2,
279 E - '30': 30,
280 E ? - -
281 E + '3': 3,
282 E - '40': 40,
283 E ? - -
284 E + '4': 4,
285 E }
286
287 test_verbosity_example.py:14: AssertionError
288 ___________________________ test_long_text_fail ____________________________
289
290 def test_long_text_fail():
291 long_text = "Lorem ipsum dolor sit amet " * 10
292 > assert "hello world" in long_text
293 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 '
294
295 test_verbosity_example.py:19: AssertionError
296 ========================= short test summary info ==========================
297 FAILED test_verbosity_example.py::test_words_fail - AssertionError: assert ['banana', 'apple', 'grapes', 'melon', 'kiwi'] == ['banana', 'apple', 'orange', 'melon', 'kiwi']
298
299 At index 2 diff: 'grapes' != 'orange'
300
301 Full diff:
302 [
303 'banana',
304 'apple',
305 - 'orange',
306 ? ^ ^^
307 + 'grapes',
308 ? ^ ^ +
309 'melon',
310 'kiwi',
311 ]
312 FAILED test_verbosity_example.py::test_numbers_fail - AssertionError: assert {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4} == {'0': 0, '10': 10, '20': 20, '30': 30, '40': 40}
313
314 Common items:
315 {'0': 0}
316 Left contains 4 more items:
317 {'1': 1, '2': 2, '3': 3, '4': 4}
318 Right contains 4 more items:
319 {'10': 10, '20': 20, '30': 30, '40': 40}
320
321 Full diff:
322 {
323 '0': 0,
324 - '10': 10,
325 ? - -
326 + '1': 1,
327 - '20': 20,
328 ? - -
329 + '2': 2,
330 - '30': 30,
331 ? - -
332 + '3': 3,
333 - '40': 40,
334 ? - -
335 + '4': 4,
336 }
337 FAILED test_verbosity_example.py::test_long_text_fail - 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 '
338 ======================= 3 failed, 1 passed in 0.12s ========================
339
340 Notice now that:
341
342 * Each test inside the file gets its own line in the output.
343 * ``test_words_fail`` gives the same output as before in this case.
344 * ``test_numbers_fail`` now shows a full text diff of the two dictionaries.
345 * ``test_long_text_fail`` also doesn't truncate on the right hand side as before, but now pytest won't truncate any
346 text at all, regardless of its size.
347
348 Those were examples of how verbosity affects normal test session output, but verbosity also is used in other
349 situations, for example you are shown even fixtures that start with ``_`` if you use ``pytest --fixtures -v``.
350
351 Using higher verbosity levels (``-vvv``, ``-vvvv``, ...) is supported, but has no effect in pytest itself at the moment,
352 however some plugins might make use of higher verbosity.
353
354 .. _`pytest.fine_grained_verbosity`:
355
356 Fine-grained verbosity
357 ~~~~~~~~~~~~~~~~~~~~~~
358
359 In addition to specifying the application wide verbosity level, it is possible to control specific aspects independently.
360 This is done by setting a verbosity level in the configuration file for the specific aspect of the output.
361
362 :confval:`verbosity_assertions`: Controls how verbose the assertion output should be when pytest is executed. Running
363 ``pytest --no-header`` with a value of ``2`` would have the same output as the previous example, but each test inside
364 the file is shown by a single character in the output.
365
366 :confval:`verbosity_test_cases`: Controls how verbose the test execution output should be when pytest is executed.
367 Running ``pytest --no-header`` with a value of ``2`` would have the same output as the first verbosity example, but each
368 test inside the file gets its own line in the output.
369
370 .. _`pytest.detailed_failed_tests_usage`:
371
372 Producing a detailed summary report
373 --------------------------------------------------
374
375 The ``-r`` flag can be used to display a "short test summary info" at the end of the test session,
376 making it easy in large test suites to get a clear picture of all failures, skips, xfails, etc.
377
378 It defaults to ``fE`` to list failures and errors.
379
380 .. regendoc:wipe
381
382 Example:
383
384 .. code-block:: python
385
386 # content of test_example.py
387 import pytest
388
389
390 @pytest.fixture
391 def error_fixture():
392 assert 0
393
394
395 def test_ok():
396 print("ok")
397
398
399 def test_fail():
400 assert 0
401
402
403 def test_error(error_fixture):
404 pass
405
406
407 def test_skip():
408 pytest.skip("skipping this test")
409
410
411 def test_xfail():
412 pytest.xfail("xfailing this test")
413
414
415 @pytest.mark.xfail(reason="always xfail")
416 def test_xpass():
417 pass
418
419
420 .. code-block:: pytest
421
422 $ pytest -ra
423 =========================== test session starts ============================
424 platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
425 rootdir: /home/sweet/project
426 collected 6 items
427
428 test_example.py .FEsxX [100%]
429
430 ================================== ERRORS ==================================
431 _______________________ ERROR at setup of test_error _______________________
432
433 @pytest.fixture
434 def error_fixture():
435 > assert 0
436 E assert 0
437
438 test_example.py:6: AssertionError
439 ================================= FAILURES =================================
440 ________________________________ test_fail _________________________________
441
442 def test_fail():
443 > assert 0
444 E assert 0
445
446 test_example.py:14: AssertionError
447 ================================ XFAILURES =================================
448 ________________________________ test_xfail ________________________________
449
450 def test_xfail():
451 > pytest.xfail("xfailing this test")
452 E _pytest.outcomes.XFailed: xfailing this test
453
454 test_example.py:26: XFailed
455 ================================= XPASSES ==================================
456 ========================= short test summary info ==========================
457 SKIPPED [1] test_example.py:22: skipping this test
458 XFAIL test_example.py::test_xfail - reason: xfailing this test
459 XPASS test_example.py::test_xpass - always xfail
460 ERROR test_example.py::test_error - assert 0
461 FAILED test_example.py::test_fail - assert 0
462 == 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12s ===
463
464 The ``-r`` options accepts a number of characters after it, with ``a`` used
465 above meaning "all except passes".
466
467 Here is the full list of available characters that can be used:
468
469 - ``f`` - failed
470 - ``E`` - error
471 - ``s`` - skipped
472 - ``x`` - xfailed
473 - ``X`` - xpassed
474 - ``p`` - passed
475 - ``P`` - passed with output
476
477 Special characters for (de)selection of groups:
478
479 - ``a`` - all except ``pP``
480 - ``A`` - all
481 - ``N`` - none, this can be used to display nothing (since ``fE`` is the default)
482
483 More than one character can be used, so for example to only see failed and skipped tests, you can execute:
484
485 .. code-block:: pytest
486
487 $ pytest -rfs
488 =========================== test session starts ============================
489 platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
490 rootdir: /home/sweet/project
491 collected 6 items
492
493 test_example.py .FEsxX [100%]
494
495 ================================== ERRORS ==================================
496 _______________________ ERROR at setup of test_error _______________________
497
498 @pytest.fixture
499 def error_fixture():
500 > assert 0
501 E assert 0
502
503 test_example.py:6: AssertionError
504 ================================= FAILURES =================================
505 ________________________________ test_fail _________________________________
506
507 def test_fail():
508 > assert 0
509 E assert 0
510
511 test_example.py:14: AssertionError
512 ========================= short test summary info ==========================
513 FAILED test_example.py::test_fail - assert 0
514 SKIPPED [1] test_example.py:22: skipping this test
515 == 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12s ===
516
517 Using ``p`` lists the passing tests, whilst ``P`` adds an extra section "PASSES" with those tests that passed but had
518 captured output:
519
520 .. code-block:: pytest
521
522 $ pytest -rpP
523 =========================== test session starts ============================
524 platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
525 rootdir: /home/sweet/project
526 collected 6 items
527
528 test_example.py .FEsxX [100%]
529
530 ================================== ERRORS ==================================
531 _______________________ ERROR at setup of test_error _______________________
532
533 @pytest.fixture
534 def error_fixture():
535 > assert 0
536 E assert 0
537
538 test_example.py:6: AssertionError
539 ================================= FAILURES =================================
540 ________________________________ test_fail _________________________________
541
542 def test_fail():
543 > assert 0
544 E assert 0
545
546 test_example.py:14: AssertionError
547 ================================== PASSES ==================================
548 _________________________________ test_ok __________________________________
549 --------------------------- Captured stdout call ---------------------------
550 ok
551 ========================= short test summary info ==========================
552 PASSED test_example.py::test_ok
553 == 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12s ===
554
555 Creating resultlog format files
556 --------------------------------------------------
557
558 To create plain-text machine-readable result files you can issue:
559
560 .. code-block:: bash
561
562 pytest --resultlog=path
563
564 and look at the content at the ``path`` location. Such files are used e.g.
565 by the `PyPy-test`_ web page to show test results over several revisions.
566
567 .. warning::
568
569 This option is rarely used and is scheduled for removal in pytest 6.0.
570
571 If you use this option, consider using the new `pytest-reportlog <https://github.com/pytest-dev/pytest-reportlog>`__ plugin instead.
572
573 See :ref:`the deprecation docs <resultlog deprecated>` for more information.
574
575
576 .. _`PyPy-test`: http://buildbot.pypy.org/summary
577
578
579 Creating JUnitXML format files
580 ----------------------------------------------------
581
582 To create result files which can be read by Jenkins_ or other Continuous
583 integration servers, use this invocation:
584
585 .. code-block:: bash
586
587 pytest --junit-xml=path
588
589 to create an XML file at ``path``.
590
591
592
593 To set the name of the root test suite xml item, you can configure the ``junit_suite_name`` option in your config file:
594
595 .. code-block:: ini
596
597 [pytest]
598 junit_suite_name = my_suite
599
600 .. versionadded:: 4.0
601
602 JUnit XML specification seems to indicate that ``"time"`` attribute
603 should report total test execution times, including setup and teardown
604 (`1 <http://windyroad.com.au/dl/Open%20Source/JUnit.xsd>`_, `2
605 <https://www.ibm.com/support/knowledgecenter/en/SSQ2R2_14.1.0/com.ibm.rsar.analysis.codereview.cobol.doc/topics/cac_useresults_junit.html>`_).
606 It is the default pytest behavior. To report just call durations
607 instead, configure the ``junit_duration_report`` option like this:
608
609 .. code-block:: ini
610
611 [pytest]
612 junit_duration_report = call
613
614 .. _record_property example:
615
616 record_property
617 ~~~~~~~~~~~~~~~~~
618
619 If you want to log additional information for a test, you can use the
620 ``record_property`` fixture:
621
622 .. code-block:: python
623
624 def test_function(record_property):
625 record_property("example_key", 1)
626 assert True
627
628 This will add an extra property ``example_key="1"`` to the generated
629 ``testcase`` tag:
630
631 .. code-block:: xml
632
633 <testcase classname="test_function" file="test_function.py" line="0" name="test_function" time="0.0009">
634 <properties>
635 <property name="example_key" value="1" />
636 </properties>
637 </testcase>
638
639 Alternatively, you can integrate this functionality with custom markers:
640
641 .. code-block:: python
642
643 # content of conftest.py
644
645
646 def pytest_collection_modifyitems(session, config, items):
647 for item in items:
648 for marker in item.iter_markers(name="test_id"):
649 test_id = marker.args[0]
650 item.user_properties.append(("test_id", test_id))
651
652 And in your tests:
653
654 .. code-block:: python
655
656 # content of test_function.py
657 import pytest
658
659
660 @pytest.mark.test_id(1501)
661 def test_function():
662 assert True
663
664 Will result in:
665
666 .. code-block:: xml
667
668 <testcase classname="test_function" file="test_function.py" line="0" name="test_function" time="0.0009">
669 <properties>
670 <property name="test_id" value="1501" />
671 </properties>
672 </testcase>
673
674 .. warning::
675
676 Please note that using this feature will break schema verifications for the latest JUnitXML schema.
677 This might be a problem when used with some CI servers.
678
679
680 record_xml_attribute
681 ~~~~~~~~~~~~~~~~~~~~~~~
682
683 To add an additional xml attribute to a testcase element, you can use
684 ``record_xml_attribute`` fixture. This can also be used to override existing values:
685
686 .. code-block:: python
687
688 def test_function(record_xml_attribute):
689 record_xml_attribute("assertions", "REQ-1234")
690 record_xml_attribute("classname", "custom_classname")
691 print("hello world")
692 assert True
693
694 Unlike ``record_property``, this will not add a new child element.
695 Instead, this will add an attribute ``assertions="REQ-1234"`` inside the generated
696 ``testcase`` tag and override the default ``classname`` with ``"classname=custom_classname"``:
697
698 .. code-block:: xml
699
700 <testcase classname="custom_classname" file="test_function.py" line="0" name="test_function" time="0.003" assertions="REQ-1234">
701 <system-out>
702 hello world
703 </system-out>
704 </testcase>
705
706 .. warning::
707
708 ``record_xml_attribute`` is an experimental feature, and its interface might be replaced
709 by something more powerful and general in future versions. The
710 functionality per-se will be kept, however.
711
712 Using this over ``record_xml_property`` can help when using ci tools to parse the xml report.
713 However, some parsers are quite strict about the elements and attributes that are allowed.
714 Many tools use an xsd schema (like the example below) to validate incoming xml.
715 Make sure you are using attribute names that are allowed by your parser.
716
717 Below is the Scheme used by Jenkins to validate the XML report:
718
719 .. code-block:: xml
720
721 <xs:element name="testcase">
722 <xs:complexType>
723 <xs:sequence>
724 <xs:element ref="skipped" minOccurs="0" maxOccurs="1"/>
725 <xs:element ref="error" minOccurs="0" maxOccurs="unbounded"/>
726 <xs:element ref="failure" minOccurs="0" maxOccurs="unbounded"/>
727 <xs:element ref="system-out" minOccurs="0" maxOccurs="unbounded"/>
728 <xs:element ref="system-err" minOccurs="0" maxOccurs="unbounded"/>
729 </xs:sequence>
730 <xs:attribute name="name" type="xs:string" use="required"/>
731 <xs:attribute name="assertions" type="xs:string" use="optional"/>
732 <xs:attribute name="time" type="xs:string" use="optional"/>
733 <xs:attribute name="classname" type="xs:string" use="optional"/>
734 <xs:attribute name="status" type="xs:string" use="optional"/>
735 </xs:complexType>
736 </xs:element>
737
738 .. warning::
739
740 Please note that using this feature will break schema verifications for the latest JUnitXML schema.
741 This might be a problem when used with some CI servers.
742
743 .. _record_testsuite_property example:
744
745 record_testsuite_property
746 ^^^^^^^^^^^^^^^^^^^^^^^^^
747
748 .. versionadded:: 4.5
749
750 If you want to add a properties node at the test-suite level, which may contains properties
751 that are relevant to all tests, you can use the ``record_testsuite_property`` session-scoped fixture:
752
753 The ``record_testsuite_property`` session-scoped fixture can be used to add properties relevant
754 to all tests.
755
756 .. code-block:: python
757
758 import pytest
759
760
761 @pytest.fixture(scope="session", autouse=True)
762 def log_global_env_facts(record_testsuite_property):
763 record_testsuite_property("ARCH", "PPC")
764 record_testsuite_property("STORAGE_TYPE", "CEPH")
765
766
767 class TestMe:
768 def test_foo(self):
769 assert True
770
771 The fixture is a callable which receives ``name`` and ``value`` of a ``<property>`` tag
772 added at the test-suite level of the generated xml:
773
774 .. code-block:: xml
775
776 <testsuite errors="0" failures="0" name="pytest" skipped="0" tests="1" time="0.006">
777 <properties>
778 <property name="ARCH" value="PPC"/>
779 <property name="STORAGE_TYPE" value="CEPH"/>
780 </properties>
781 <testcase classname="test_me.TestMe" file="test_me.py" line="16" name="test_foo" time="0.000243663787842"/>
782 </testsuite>
783
784 ``name`` must be a string, ``value`` will be converted to a string and properly xml-escaped.
785
786 The generated XML is compatible with the latest ``xunit`` standard, contrary to `record_property`_
787 and `record_xml_attribute`_.
788
789
790 Sending test report to an online pastebin service
791 --------------------------------------------------
792
793 **Creating a URL for each test failure**:
794
795 .. code-block:: bash
796
797 pytest --pastebin=failed
798
799 This will submit test run information to a remote Paste service and
800 provide a URL for each failure. You may select tests as usual or add
801 for example ``-x`` if you only want to send one particular failure.
802
803 **Creating a URL for a whole test session log**:
804
805 .. code-block:: bash
806
807 pytest --pastebin=all
808
809 Currently only pasting to the https://bpaste.net/ service is implemented.
810
811 .. versionchanged:: 5.2
812
813 If creating the URL fails for any reason, a warning is generated instead of failing the
814 entire test suite.
815
816 .. _jenkins: https://jenkins-ci.org