Skip to main content

Advanced pytest parametrization plugin that generates test case instances from sync or async factories.

Project description

pytest-case-provider

Latest Version Python Supported Versions Pytest versions MyPy Strict Test Coverage Downloads GitHub stars

Parametrization for pytest with fixture support, async support, class-method support, shared storage, and module-level case discovery. Declarative, typed, on-demand injection for sync, async, iterable, and async-iterable providers.


Overview

Extends pytest’s parametrization layer with case storages and containers. Containers attach case sets to test functions and test methods via inject_func and inject_method. Fixtures integrate natively. Async is supported. Matching case_*.py modules are scanned and loaded. Duplicate case names collapse to single instances.


Core Concepts

Providers

Functions that yield case values. Supported forms:

  • Sync
  • Async
  • Iterable
  • Async iterable
  • Fixture-dependent

Storage

CaseStorage instances hold providers. CompositeCaseStorage merges multiple storages.

Container

CaseContainer wraps a storage and exposes:

  • .case()
  • .include()
  • .inject_func()
  • .inject_method()

A single container instance feeds any number of tests.

Injectors

inject_func() and inject_method() create isolated containers for direct use. .include() attaches external storages to them.

Module-Level Loading

If test_x.py exists alongside case_x.py, the plugin imports the case module and registers its providers. Tests using inject_func() or inject_method() receive matching providers. Matching is based on the type annotation of the case parameter.


Installation

pip install pytest-case-provider

Quick Start

# test_example.py
import typing
from dataclasses import dataclass
from pytest_case_provider import CaseContainer

@dataclass(frozen=True)
class MyCase:
    foo: int

container = CaseContainer[MyCase]()

@container.inject_func()
def test_cases(case: MyCase) -> None:
    assert isinstance(case, MyCase)

class TestGroup:
    @container.inject_method()
    def test_group(self, case: MyCase) -> None:
        assert case.foo >= 1

@container.case()
def case_small() -> MyCase:
    return MyCase(foo=1)

@container.case()
async def case_async() -> MyCase:
    return MyCase(foo=999)

@container.case()
def case_range() -> typing.Iterator[MyCase]:
    yield MyCase(foo=10)

Module-Level Case Files

tests/
    test_math.py
    case_math.py

case_math.py:

from dataclasses import dataclass

@dataclass(frozen=True)
class MathCase:
    x: int
    y: int

def case_x1_y2() -> MathCase:
    return MathCase(1, 2)

test_math.py:

from pytest_case_provider import inject_func
from tests.case_math import MathCase

@inject_func()
def test_add(case: MathCase) -> None:
    assert case.x + case.y == 3

The plugin imports case_math and binds its providers to the injector.


Pytest Expansion

test_example.py::test_cases[case_small]
test_example.py::test_cases[case_async]
test_example.py::test_cases[case_range0]
test_example.py::test_cases[case_range1]
test_example.py::TestGroup::test_group[case_small]
test_example.py::TestGroup::test_group[case_async]
test_example.py::TestGroup::test_group[case_range0]
test_example.py::TestGroup::test_group[case_range1]

Notes

  • Async requires pytest-asyncio.
  • AnyIO execution paths are disabled.
  • Case deduplication follows provider-name semantics.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_case_provider-0.4.1.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_case_provider-0.4.1-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_case_provider-0.4.1.tar.gz.

File metadata

  • Download URL: pytest_case_provider-0.4.1.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_case_provider-0.4.1.tar.gz
Algorithm Hash digest
SHA256 831ab8d5ee5ae48a6718c8edc38bd95930eb317a413a202e1235045fcf753551
MD5 49eb8efe2914cfd44fe5453bcec043c7
BLAKE2b-256 3f287c3c925ac774f0156a3ca97175dd69f45392ed301295306c374ff87a9cf7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_case_provider-0.4.1.tar.gz:

Publisher: publish.yaml on zerlok/pytest-case-provider

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_case_provider-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_case_provider-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad9b2f4c8abaadd9a0a836fcb80f8da92a1fa90dc5303026f6131610ee801f26
MD5 8772588197b7a71427b6287d49f517df
BLAKE2b-256 daffbfc064a4685d909e70bc8e4ab4f20a37f48da22ff704938ba028c5aed832

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_case_provider-0.4.1-py3-none-any.whl:

Publisher: publish.yaml on zerlok/pytest-case-provider

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page