Registry / testing / pytest-cases

pytest-cases

JSON →
library3.10.1pypypi✓ verified 24d ago

pytest-cases is a Python library that extends pytest to enable the separation of test code from test cases, allowing for more modular and reusable tests. It leverages pytest's parametrization capabilities to define test data and expected results in 'case functions,' which are then injected into test functions. The library is actively maintained, with frequent minor and patch releases to ensure compatibility with new pytest and Python versions, as well as to introduce new features.

pip install pytest-cases
INSTALL
IMPORT
SIG · PYTEST-CASES
P
pytest-cases
testingpythonv3.10.1
Install
2.8s avg
Import
390ms
Disk
31MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.10.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.082s · 32.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.8s · import 0.074s · 33MB
31MB installed
● package 31MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parametrize_with_cases
from pytest_cases import parametrize_with_cases
from pytest_cases import parametrize_with_cases

This quickstart demonstrates how to define test cases in a separate file using the `@case` decorator and then use `parametrize_with_cases` to inject these cases into a test function. The `cases` argument points to the module containing the case functions. When `pytest` is run, it will automatically discover and execute `test_foo_function` twice, once for each defined case, allowing for clear separation of test logic and test data.

import pytest from pytest_cases import parametrize_with_cases, case # test_foo_cases.py @case def case_positive_int(): return 10 @case def case_another_positive_int(): return 20 # test_foo.py def foo(a): return a + 5 @parametrize_with_cases("a", cases=".test_foo_cases") def test_foo_function(a): """Tests the foo function with various cases.""" expected_result = a + 5 assert foo(a) == expected_result # To run these tests: # 1. Save the case functions in a file named `test_foo_cases.py`. # 2. Save the test function in a file named `test_foo.py`. # 3. Run `pytest` in the directory containing these files. # Expected output will show tests passing for both case_positive_int and case_another_positive_int.
Debug
Known issues
breakingSupport for Python versions earlier than 3.9 and pytest versions earlier than 6 was dropped in `pytest-cases` v3.9.1. Ensure your environment meets these minimum requirements when upgrading.
fix
Upgrade Python to 3.9+ and pytest to 6+. If unable to upgrade, pin `pytest-cases` to `<3.9.1`.
affects: <3.9.1
breakingCompatibility fixes were required for `pytest` versions 8.4 and 9.0. Users upgrading `pytest` without also updating `pytest-cases` to at least `3.9.1` (for `pytest 8.4`) or `3.10.0` (for `pytest 9`) may encounter `AttributeError` or `ValueError` related to fixture handling and parametrized cases.
fix
Ensure `pytest-cases` is updated to version `3.9.1` or higher for `pytest 8.4+`, and `3.10.0` or higher for `pytest 9+`.
affects: <3.9.1 (for pytest 8.4), <3.10.0 (for pytest 9)
gotchaWhen a fixture is only required by *some* of your cases, use `@fixture` from `pytest_cases` instead of `@pytest.fixture`. Using `@pytest.fixture` will cause the fixture to be set up and torn down for *all* cases, even those not requiring it, leading to inefficiencies.
fix
Replace `@pytest.fixture` with `from pytest_cases import fixture` and use `@fixture` for such conditional fixtures.
affects: All versions
gotcha`pytest-cases` can affect the order of pytest test execution. While this sometimes resolves `pytest#5054`, it can also lead to unexpected test ordering. If test order is critical and implicitly relied upon, this change might cause subtle issues.
fix
Be aware that test order may change. If undesirable ordering occurs, report it to the `pytest-cases` developers. Design tests to be independent of execution order where possible.
affects: All versions
gotchaWith `pytest 8`, the warning `PytestRemovedIn9Warning: Marks applied to fixtures have no effect` might occur. `pytest-cases` version `3.8.5` suppressed this warning. On older `pytest-cases` versions with `pytest 8`, you might observe this warning or find that marks on fixtures are not applied as expected.
fix
Upgrade `pytest-cases` to version `3.8.5` or newer if running with `pytest 8` to suppress this warning and ensure consistent behavior of marks.
affects: <3.8.5 (with pytest 8)
Upgrade
Version history
3.10.1latest on PyPI · released Mar 2, 2026
Audit
Dependencies
pytestrequiredCore testing framework that pytest-cases extends and relies on.
Agent activity
51 hits · last 30 days
node
42
OpenAI (training)
1
Resources
pytest-cases — pip install pytest-cases · libregistry