pytest-subtests

0.15.0 · active · verified Thu Apr 09

pytest-subtests is a pytest plugin that provides support for the `unittest.TestCase.subTest()` context manager, allowing developers to write test functions that run multiple distinct checks while reporting individual failures. It introduces a `subtests` fixture for this purpose. The current version is 0.15.0, and it follows the pytest ecosystem's release cadence, often updating to ensure compatibility with new pytest versions.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the `subtests` fixture to perform multiple assertions within a single test function. Each `with subtests.test(...)` block is treated as an independent subtest, and failures are reported individually, allowing the parent test to continue running all subtests before ultimately failing.

import pytest

def test_multiple_checks(subtests):
    values = [1, 2, 3]
    for i, value in enumerate(values):
        with subtests.test(f"Check value {value}", index=i):
            # This subtest will fail for value=2
            assert value % 2 != 0

# To run this test, save it as a .py file (e.g., test_example.py) 
# and execute pytest from your terminal:
# pytest test_example.py

view raw JSON →