pytest_fixtures_fixtures
pytest_fixtures_fixtures
¶
Handy fixtures to access your fixtures.
Classes¶
FixturePath
¶
Bases: Protocol
A callable that constructs paths inside the fixtures directory.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], must_exist: bool = True) -> Path
¶
Construct a path to a fixture file.
This method creates a path to a fixture file within the fixtures directory.
It's used by the path_for_fixture
fixture to provide a consistent interface
for accessing test fixture files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
One or more path components (e.g., "data", "sample.json"). Can be strings or path-like objects. |
()
|
must_exist
|
bool
|
If True (default), raise FileNotFoundError if the file does not exist. |
True
|
Returns:
Type | Description |
---|---|
Path
|
The constructed fixture path. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If must_exist=True and the fixture file doesn't exist. |
Example
Using the FixturePath protocol in a test:
def test_fixture_path(path_for_fixture: FixturePath):
# Construct a path to a fixture file
data_path = path_for_fixture("data", "sample.json")
assert data_path.suffix == ".json"
# Check if a file exists without raising an error
optional_path = path_for_fixture("optional", "file.txt", must_exist=False)
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadCsvDictFixture
¶
Bases: Protocol
A callable that reads and parses CSV fixture files as dictionaries.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8') -> Generator[dict[str, str], None, None]
¶
Read and parse a CSV fixture file as dictionaries.
This method reads CSV fixture files using Python's built-in csv.DictReader. The result is a generator of dictionaries, one for each row in the file. Each row is returned as a dictionary with column headers as keys and row values as string values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the CSV fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
Returns:
Type | Description |
---|---|
None
|
A generator of dictionaries, one for each row in the CSV file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the fixture file doesn't exist. |
Error
|
If the file contains malformed CSV data. |
Example
Using the ReadCsvDictFixture protocol in a test:
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadCsvFixture
¶
Bases: Protocol
A callable that reads and parses CSV fixture files.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8') -> Generator[list[str], None, None]
¶
Read and parse a CSV fixture file.
This method reads CSV fixture files using Python's built-in csv.reader. The result is a generator of lists, one for each row in the file. Each row is returned as a list of strings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the CSV fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
Returns:
Type | Description |
---|---|
None
|
A generator of lists, one for each row in the CSV file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the fixture file doesn't exist. |
Error
|
If the file contains malformed CSV data. |
Example
Using the ReadCsvFixture protocol in a test:
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadFixture
¶
Bases: Protocol
A callable that reads and optionally deserializes fixture files.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8', mode: str = 'r', deserialize: Callable[[str | bytes], Any] = lambda x: x) -> Any
¶
Read and optionally deserialize a fixture file.
This method reads fixture files with customizable encoding, file mode, and deserialization. It's used by various read fixture functions to provide a consistent interface for accessing fixture file contents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
mode
|
str
|
File open mode (default: "r" for text mode). |
'r'
|
deserialize
|
Callable[[str | bytes], Any]
|
Function to process the file contents. Takes str or bytes and returns Any (default: identity). |
lambda x: x
|
Returns:
Type | Description |
---|---|
Any
|
The result of applying the deserialize function to the file contents. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the fixture file doesn't exist. |
UnicodeDecodeError
|
If the file cannot be decoded with the specified encoding. |
OSError
|
If there's an error reading the file. |
Example
Using the ReadFixture protocol in a test:
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadJsonFixture
¶
Bases: Protocol
A callable that reads and parses JSON fixture files.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8') -> dict
¶
Read and parse a JSON fixture file.
This method reads JSON fixture files and automatically parses them into Python dictionaries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the JSON fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
Returns:
Type | Description |
---|---|
dict
|
The parsed JSON data as a Python dictionary. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the fixture file doesn't exist. |
JSONDecodeError
|
If the file contains invalid JSON. |
Example
Using the ReadJsonFixture protocol in a test:
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadJsonlFixture
¶
Bases: Protocol
A callable that reads and parses JSONL (JSON Lines) fixture files.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8') -> Generator[dict, None, None]
¶
Read and parse a JSONL (JSON Lines) fixture file.
This method reads JSONL fixture files, where each line contains a separate JSON object. The result is a generator of dictionaries, one for each line in the file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the JSONL fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
Returns:
Type | Description |
---|---|
None
|
A generator of dictionaries, one for each JSON object in the file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the fixture file doesn't exist. |
JSONDecodeError
|
If any line contains invalid JSON. |
Example
Using the ReadJsonlFixture protocol in a test:
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadYamlFixture
¶
Bases: Protocol
A callable that reads and parses YAML fixture files.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8', unsafe_load: bool = False) -> Any
¶
Read and parse a YAML fixture file.
This method reads YAML fixture files and automatically parses them into Python objects (typically dictionaries). Requires the PyYAML library to be installed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the YAML fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
unsafe_load
|
bool
|
If True, uses yaml.FullLoader instead of yaml.SafeLoader (default: False). WARNING: Only use unsafe_load=True with trusted YAML content. |
False
|
Returns:
Type | Description |
---|---|
Any
|
The parsed YAML data (typically a dictionary or list). |
Raises:
Type | Description |
---|---|
ImportError
|
If PyYAML is not installed. |
FileNotFoundError
|
If the fixture file doesn't exist. |
YAMLError
|
If the file contains invalid YAML syntax. |
Example
Using the ReadYamlFixture protocol in a test:
Source code in src/pytest_fixtures_fixtures/protocols.py
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 |
|