A pytest library for parametrizing tests using type hints.
Project description
Package to use type annotations for parametrization in pytest
This package allows the declaration of parametrization using type annotations and definition of test cases using a decorator.
The primary reason of using this package over the standard pytest.mark.parametrize is readability and maintainability
of test cases especially if many test cases are defined for a single test function.
In addition, error messages are more informative compared to those provided by pytest.mark.parametrize.
Install pytest-parametrization-annotation
pip install pytest-parametrization-annotation
Usage
Defining a test function parameter as parametrized
To define a parameter as parametrized, use the Parametrized class or an instance of the Parametrized class as an
annotation.
from typing import Annotated
from pytest_parametrization_annotation import Parametrized
# Both definitions are treated the same
def test(a: Annotated[int, Parametrized], b: Annotated[int, Parametrized()]) -> None:
...
By default, parameters annotated in this way are treated as direct parameters.
To define a parameter as indirect, use the indirect argument when instantiating the Parametrized class.
from typing import Annotated
from pytest_parametrization_annotation import Parametrized
def test(a: Annotated[int, Parametrized(indirect=True)]) -> None:
...
In addition, the Parametrized class provides to methods to define default values, default and default_factory.
from typing import Annotated
from pytest_parametrization_annotation import Parametrized
def test(
a: Annotated[int, Parametrized(default=1)],
b: Annotated[str, Parametrized(default_factory=lambda: "Hello World!")]
) -> None:
...
Defining test cases
To define test cases, use the case marker.
Each parametrized argument must be reflected as keyword in the case marker.
from typing import Annotated
import pytest
from pytest_parametrization_annotation import Parametrized
@pytest.mark.case(a=1)
def test(a: Annotated[int, Parametrized]) -> None:
...
If a parametrized argument is missing from the case marker, the test suit will fail and a detailed error message will be provided.
examples/test_basic.py::test_addition | Case number 1: Failed to populate because the parameter 'b' is not provided and default is not configured.
Every case marker defines a single test case. To define multiple test cases, use multiple case markers.
from typing import Annotated
import pytest
from pytest_parametrization_annotation import Parametrized
@pytest.mark.case(a=1)
@pytest.mark.case(a=2)
@pytest.mark.case(a=3)
def test(a: Annotated[int, Parametrized]) -> None:
...
Optionally a case can be named by providing the first positional argument to the case marker. If this is not provided the default pytest naming scheme is used.
from typing import Annotated
import pytest
from pytest_parametrization_annotation import Parametrized
@pytest.mark.case("Example", a=1)
def test(a: Annotated[int, Parametrized]) -> None:
...
Develop
Install dependencies
poetry install
Format code
poetry run ruff format
poetry run black .
Run static type checker
poetry run mypy .
Run tests
poetry run tox run-parallel
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_parametrization_annotation-1.0.0.tar.gz.
File metadata
- Download URL: pytest_parametrization_annotation-1.0.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6069b603de899009b38c6ddb765936a6474d29ed7e920ba45bee8fec2bc764b
|
|
| MD5 |
93defe6879a5720b316d9fbecd5f5b0d
|
|
| BLAKE2b-256 |
c757abd3a2a53376cc40d38a2fe91e3fc1842946dfe9d880ccc8053c4062e6f5
|
Provenance
The following attestation bundles were made for pytest_parametrization_annotation-1.0.0.tar.gz:
Publisher:
publish-release.yml on skeletorXVI/pytest-parametrization-annotation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_parametrization_annotation-1.0.0.tar.gz -
Subject digest:
f6069b603de899009b38c6ddb765936a6474d29ed7e920ba45bee8fec2bc764b - Sigstore transparency entry: 154455771
- Sigstore integration time:
-
Permalink:
skeletorXVI/pytest-parametrization-annotation@48ab74f17b15d59a1f433e86f5894d15e3c8357c -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/skeletorXVI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-release.yml@48ab74f17b15d59a1f433e86f5894d15e3c8357c -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_parametrization_annotation-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pytest_parametrization_annotation-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b0fa1a9d6604a6263497a3174a3688848710272c34735af88eca1dc8e622151f
|
|
| MD5 |
8067045ff19b52c8cc3050be8a6f8152
|
|
| BLAKE2b-256 |
272b33f282215cd23a290df55be7414b617e8afbff0a4e1406c30882e4f09fac
|
Provenance
The following attestation bundles were made for pytest_parametrization_annotation-1.0.0-py3-none-any.whl:
Publisher:
publish-release.yml on skeletorXVI/pytest-parametrization-annotation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_parametrization_annotation-1.0.0-py3-none-any.whl -
Subject digest:
b0fa1a9d6604a6263497a3174a3688848710272c34735af88eca1dc8e622151f - Sigstore transparency entry: 154455772
- Sigstore integration time:
-
Permalink:
skeletorXVI/pytest-parametrization-annotation@48ab74f17b15d59a1f433e86f5894d15e3c8357c -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/skeletorXVI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-release.yml@48ab74f17b15d59a1f433e86f5894d15e3c8357c -
Trigger Event:
release
-
Statement type: