Debian Codegraph @

doc/en/how-to/subtests.rst

raw
1 .. _subtests:
2
3 How to use subtests
4 ===================
5
6 .. versionadded:: 9.0
7
8 .. note::
9
10 This feature is experimental. Its behavior, particularly how failures are reported, may evolve in future releases. However, the core functionality and usage are considered stable.
11
12 pytest allows for grouping assertions within a normal test, known as *subtests*.
13
14 Subtests are an alternative to parametrization, particularly useful when the exact parametrization values are not known at collection time.
15
16
17 .. code-block:: python
18
19 # content of test_subtest.py
20
21
22 def test(subtests):
23 for i in range(5):
24 with subtests.test(msg="custom message", i=i):
25 assert i % 2 == 0
26
27 Each assertion failure or error is caught by the context manager and reported individually:
28
29 .. code-block:: text
30
31 $ pytest -q test_subtest.py
32 uuuuuF [100%]
33 ================================= FAILURES =================================
34 _______________________ test [custom message] (i=1) ________________________
35
36 subtests = <_pytest.subtests.Subtests object at 0xdeadbeef0001>
37
38 def test(subtests):
39 for i in range(5):
40 with subtests.test(msg="custom message", i=i):
41 > assert i % 2 == 0
42 E assert (1 % 2) == 0
43
44 test_subtest.py:6: AssertionError
45 _______________________ test [custom message] (i=3) ________________________
46
47 subtests = <_pytest.subtests.Subtests object at 0xdeadbeef0001>
48
49 def test(subtests):
50 for i in range(5):
51 with subtests.test(msg="custom message", i=i):
52 > assert i % 2 == 0
53 E assert (3 % 2) == 0
54
55 test_subtest.py:6: AssertionError
56 ___________________________________ test ___________________________________
57 contains 2 failed subtests
58 ========================= short test summary info ==========================
59 SUBFAILED[custom message] (i=1) test_subtest.py::test - assert (1 % 2) == 0
60 SUBFAILED[custom message] (i=3) test_subtest.py::test - assert (3 % 2) == 0
61 FAILED test_subtest.py::test - contains 2 failed subtests
62 3 failed, 3 subtests passed in 0.12s
63
64 In the output above:
65
66 * The compact progress output uses ``u`` for both passed and failed subtests;
67 see the short test summary for each failed subtest.
68 * Subtest failures are reported as ``SUBFAILED``.
69 * Subtests are reported first and the "top-level" test is reported at the end on its own.
70
71 Note that it is possible to use ``subtests`` multiple times in the same test, or even mix and match with normal assertions
72 outside the ``subtests.test`` block:
73
74 .. code-block:: python
75
76 def test(subtests):
77 for i in range(5):
78 with subtests.test("stage 1", i=i):
79 assert i % 2 == 0
80
81 assert func() == 10
82
83 for i in range(10, 20):
84 with subtests.test("stage 2", i=i):
85 assert i % 2 == 0
86
87 .. note::
88
89 See :ref:`parametrize` for an alternative to subtests.
90
91
92 Verbosity
93 ---------
94
95 By default, only **subtest failures** are shown. Higher verbosity levels (:option:`-v`) will also show progress output for **passed** subtests.
96
97 It is possible to control the verbosity of subtests by setting :confval:`verbosity_subtests`.
98
99
100 Typing
101 ------
102
103 :class:`pytest.Subtests` is exported so it can be used in type annotations:
104
105 .. code-block:: python
106
107 def test(subtests: pytest.Subtests) -> None: ...
108
109 .. _parametrize_vs_subtests:
110
111 Parametrization vs Subtests
112 ---------------------------
113
114 While :ref:`traditional pytest parametrization <parametrize>` and ``subtests`` are similar, they have important differences and use cases.
115
116
117 Parametrization
118 ~~~~~~~~~~~~~~~
119
120 * Happens at collection time.
121 * Generates individual tests.
122 * Parametrized tests can be referenced from the command line.
123 * Plays well with plugins that handle test execution, such as :option:`--last-failed`.
124 * Ideal for decision table testing.
125
126 Subtests
127 ~~~~~~~~
128
129 * Happen during test execution.
130 * Are not known at collection time.
131 * Can be generated dynamically.
132 * Cannot be referenced individually from the command line.
133 * Plugins that handle test execution cannot target individual subtests.
134 * An assertion failure inside a subtest does not interrupt the test, letting users see all failures in the same report.
135
136
137 .. note::
138
139 This feature was originally implemented as a separate plugin in `pytest-subtests <https://github.com/pytest-dev/pytest-subtests>`__, but since ``9.0`` has been merged into the core.
140
141 The core implementation should be compatible with the plugin implementation, except it does not contain custom command-line options to control subtest output.