|
| 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:: pytest |
| 30 | +
|
| 31 | + $ pytest -q test_subtest.py |
| 32 | +
|
| 33 | +
|
| 34 | +In the output above: |
| 35 | + |
| 36 | +* Subtest failures are reported as ``SUBFAILED``. |
| 37 | +* Subtests are reported first and the "top-level" test is reported at the end on its own. |
| 38 | + |
| 39 | +Note that it is possible to use ``subtests`` multiple times in the same test, or even mix and match with normal assertions |
| 40 | +outside the ``subtests.test`` block: |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | +
|
| 44 | + def test(subtests): |
| 45 | + for i in range(5): |
| 46 | + with subtests.test("stage 1", i=i): |
| 47 | + assert i % 2 == 0 |
| 48 | +
|
| 49 | + assert func() == 10 |
| 50 | +
|
| 51 | + for i in range(10, 20): |
| 52 | + with subtests.test("stage 2", i=i): |
| 53 | + assert i % 2 == 0 |
| 54 | +
|
| 55 | +.. note:: |
| 56 | + |
| 57 | + See :ref:`parametrize` for an alternative to subtests. |
| 58 | + |
| 59 | + |
| 60 | +Verbosity |
| 61 | +--------- |
| 62 | + |
| 63 | +By default, only **subtest failures** are shown. Higher verbosity levels (``-v``) will also show progress output for **passed** subtests. |
| 64 | + |
| 65 | +It is possible to control the verbosity of subtests by setting :confval:`verbosity_subtests`. |
| 66 | + |
| 67 | + |
| 68 | +Typing |
| 69 | +------ |
| 70 | + |
| 71 | +:class:`pytest.Subtests` is exported so it can be used in type annotations: |
| 72 | + |
| 73 | +.. code-block:: python |
| 74 | +
|
| 75 | + def test(subtests: pytest.Subtests) -> None: ... |
| 76 | +
|
| 77 | +.. _parametrize_vs_subtests: |
| 78 | + |
| 79 | +Parametrization vs Subtests |
| 80 | +--------------------------- |
| 81 | + |
| 82 | +While :ref:`traditional pytest parametrization <parametrize>` and ``subtests`` are similar, they have important differences and use cases. |
| 83 | + |
| 84 | + |
| 85 | +Parametrization |
| 86 | +~~~~~~~~~~~~~~~ |
| 87 | + |
| 88 | +* Happens at collection time. |
| 89 | +* Generates individual tests. |
| 90 | +* Parametrized tests can be referenced from the command line. |
| 91 | +* Plays well with plugins that handle test execution, such as ``--last-failed``. |
| 92 | +* Ideal for decision table testing. |
| 93 | + |
| 94 | +Subtests |
| 95 | +~~~~~~~~ |
| 96 | + |
| 97 | +* Happen during test execution. |
| 98 | +* Are not known at collection time. |
| 99 | +* Can be generated dynamically. |
| 100 | +* Cannot be referenced individually from the command line. |
| 101 | +* Plugins that handle test execution cannot target individual subtests. |
| 102 | +* An assertion failure inside a subtest does not interrupt the test, letting users see all failures in the same report. |
| 103 | + |
| 104 | + |
| 105 | +.. note:: |
| 106 | + |
| 107 | + 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. |
| 108 | + |
| 109 | + The core implementation should be compatible to the plugin implementation, except it does not contain custom command-line options to control subtest output. |
0 commit comments