parametrize
parametrize
¶
Module for parametrizing tests from fixture files.
Attributes¶
F = TypeVar('F', bound=(Callable[..., Any]))
module-attribute
¶
Type variable for callable objects.
Represents any callable that accepts variable arguments and returns any type. Used for decorator type annotations to preserve the original function signature.
FixtureData: TypeAlias = tuple[str, list[tuple], list[str] | None]
module-attribute
¶
Type alias for parametrized test data extracted from fixture files.
This type represents the structure of data returned by fixture parsing functions. It contains:
str
: The fixture file name or identifierlist[tuple]
: List of parameter tuples for each test caselist[str] | None
: Optional list of test IDs, or None for auto-generated IDs
Functions¶
parametrize_from_fixture(fixture_name: str, *, file_format: str = 'auto', encoding: str = 'utf-8', fixtures_dir: str | Path | None = None, id_field: str | None = 'id', **kwargs: Any) -> Callable[[F], F]
¶
Parametrize a test function using data from a fixture file.
This decorator reads data from a fixture file and automatically applies pytest.mark.parametrize to the test function. It supports CSV, JSON, JSONL, and YAML file formats.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fixture_name
|
str
|
Path to the fixture file relative to the fixtures directory. |
required |
file_format
|
str
|
File format ("csv", "json", "jsonl", "yaml", or "auto" to detect from extension). |
'auto'
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
fixtures_dir
|
str | Path | None
|
Override the fixtures directory path. If None, defaults to "tests/fixtures/". |
None
|
id_field
|
str | None
|
The field name to use for test IDs. If None, no test IDs will be used. Defaults to "id". |
'id'
|
**kwargs
|
Any
|
Additional arguments passed to pytest.mark.parametrize. |
{}
|
Returns:
Type | Description |
---|---|
Callable[[F], F]
|
A decorator that parametrizes the test function. |
Raises:
Type | Description |
---|---|
ValueError
|
If the file format is unsupported or data format is invalid. |
FileNotFoundError
|
If the fixture file doesn't exist. |
ImportError
|
If a required library is not installed. |
Note
Test IDs can be specified using a special column (CSV) or key (JSON/JSONL/YAML). When present, these IDs are automatically used for test identification. User-provided 'ids' parameter takes precedence over file-based IDs.
Example
CSV file with custom IDs:
JSON file with custom IDs:
[
{"id": "test_case_1", "value": "a", "expected": "b"},
{"id": "test_case_2", "value": "x", "expected": "y"}
]
Usage:
Source code in src/pytest_fixtures_fixtures/parametrize.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|