Create pytest parametrize decorators from external files.
Project Links
Meta
Author: Chris Sewell
Requires Python: >=3.8
Classifiers
License
- OSI Approved :: MIT License
Programming Language
- Python :: 3
- Python :: 3.8
- Python :: 3.9
- Python :: 3.10
- Python :: 3.11
Framework
- Pytest
pytest-param-files
A small package to generate parametrized pytests from external files.
Simply create a text file with an available format:
dot
format (default):
[name1] description
.
input content
.
expected output content
.
[name2] description
.
input content
.
expected output content
.
yaml
format:
name1:
description: optional description
input: |-
input content
expected: |-
expected output content
name2:
description: optional description
input: |-
input content
expected: |-
expected output content
Then, use the param_file
pytest marker to create a parametrized test:
from pathlib import Path
import pytest
import my_function
PATH = Path(__file__).parent.joinpath("test_file.txt")
@pytest.mark.param_file(PATH, fmt="dot")
def test_function(file_params):
assert my_function(file_params.content) == file_params.expected
and the output will be:
$ pytest -v test_file.py
...
test_file.py::test_function[1-name1] PASSED
test_file.py::test_function[8-name2] FAILED
Alternatively use the assert_expected
method, which will can handle more rich assertion features:
@pytest.mark.param_file(PATH, fmt="dot")
def test_function(file_params):
actual = my_function(file_params.content)
assert file_params.assert_expected(actual, rstrip=True)
$ pytest -v test_file.py
...
test_file.py::test_function[1-name1] PASSED
test_file.py::test_function[8-name2] FAILED
...
E AssertionError: Actual does not match expected
E --- /path/to/test_file.txt:8
E +++ (actual)
E @@ -1 +1 @@
E -content
E +other
Installation
Install from PyPI:
$ pip install pytest-param-files
or install locally (for development):
$ pip install -e .
Regenerating expected output on failures
Running pytest with the --regen-file-failure
option will regenerate the parameter file with actual output, if any test fails.
Other formats
TODO ...
Jul 29, 2023
0.6.0
Jul 28, 2023
0.5.0
Jul 28, 2023
0.4.0
Feb 24, 2023
0.3.5
Jan 14, 2022
0.3.4
Jan 09, 2022
0.3.3
Jan 09, 2022
0.3.2
Jan 09, 2022
0.3.1
Jan 09, 2022
0.3.0
Jan 09, 2022
0.2.2
Jan 09, 2022
0.2.1
Jan 09, 2022
0.2.0
Jan 09, 2022
0.1.0