{"id":5031,"library":"pytest-cases","title":"pytest-cases","description":"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.","status":"active","version":"3.10.1","language":"en","source_language":"en","source_url":"https://github.com/smarie/python-pytest-cases","tags":["testing","pytest","fixtures","test cases","parametrization"],"install":[{"cmd":"pip install pytest-cases","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core testing framework that pytest-cases extends and relies on.","package":"pytest","optional":false}],"imports":[{"note":"Used to decorate test functions, collecting cases from specified modules or classes.","symbol":"parametrize_with_cases","correct":"from pytest_cases import parametrize_with_cases"},{"note":"Decorator for defining individual case functions.","symbol":"case","correct":"from pytest_cases import case"},{"note":"Alternative to @pytest.fixture, especially useful for cases that only require the fixture conditionally, or when applying @pytest.mark.parametrize to a fixture.","symbol":"fixture","correct":"from pytest_cases import fixture"},{"note":"A fixture (available since v3.5) that provides a dictionary of the current case's details for each parametrized argument.","symbol":"current_cases","correct":"from pytest_cases import current_cases"}],"quickstart":{"code":"import pytest\nfrom pytest_cases import parametrize_with_cases, case\n\n# test_foo_cases.py\n@case\ndef case_positive_int():\n    return 10\n\n@case\ndef case_another_positive_int():\n    return 20\n\n# test_foo.py\ndef foo(a):\n    return a + 5\n\n@parametrize_with_cases(\"a\", cases=\".test_foo_cases\")\ndef test_foo_function(a):\n    \"\"\"Tests the foo function with various cases.\"\"\"\n    expected_result = a + 5\n    assert foo(a) == expected_result\n\n# To run these tests:\n# 1. Save the case functions in a file named `test_foo_cases.py`.\n# 2. Save the test function in a file named `test_foo.py`.\n# 3. Run `pytest` in the directory containing these files.\n# Expected output will show tests passing for both case_positive_int and case_another_positive_int.","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade Python to 3.9+ and pytest to 6+. If unable to upgrade, pin `pytest-cases` to `<3.9.1`.","message":"Support 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.","severity":"breaking","affected_versions":"<3.9.1"},{"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+`.","message":"Compatibility 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.","severity":"breaking","affected_versions":"<3.9.1 (for pytest 8.4), <3.10.0 (for pytest 9)"},{"fix":"Replace `@pytest.fixture` with `from pytest_cases import fixture` and use `@fixture` for such conditional fixtures.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"`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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"With `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.","severity":"gotcha","affected_versions":"<3.8.5 (with pytest 8)"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}