parametrize-from-file

raw JSON →
0.21.0 verified Sat May 09 auth: no python

A pytest plugin that allows parametrizing test functions with values read from external config files (JSON, TOML, YAML, INI, etc.). Version 0.21.0 supports Python ~=3.8. Releases are infrequent, typically a few times per year.

pip install parametrize-from-file
error pytest: error: unrecognized arguments: --parametrize-from-file-format
cause Using the command-line option for an older version that was removed in v0.20.0.
fix
Remove the --parametrize-from-file-format argument and specify format within the decorator: @parametrize_from_file(format='yaml').
error TypeError: parametrize_from_file() missing 1 required positional argument: 'params'
cause Using the decorator without parentheses when no extra arguments are needed.
fix
Use @parametrize_from_file() with parentheses, even if no arguments are passed.
error pytest.fail.Exception: Could not find a test data file for test_my_func
cause The default file name does not match the test function name (e.g., test file is 'data.yaml' but expects 'test_my_func.yaml').
fix
Specify the file explicitly: @parametrize_from_file(source='data.yaml').
breaking In v0.20.0, the default config format changed from YAML to JSON. Existing YAML test data files will not be found unless you specify the format explicitly.
fix Use @parametrize_from_file(format='yaml') or rename/convert files to JSON.
gotcha The decorator must be applied BEFORE @pytest.mark.parametrize. If applied after, parametrize_from_file will not be able to intercept the parametrization.
fix Ensure @parametrize_from_file is the outermost decorator (i.e., directly above the test function, below any @pytest.mark.*).
deprecated The 'parametrize_from_file.add_source' function was deprecated in v0.18.0 and will be removed in a future release.
fix Use @parametrize_from_file(source='...') instead.

The decorator loads parameters from a file named after the test module (e.g., test_*.yaml). The file is expected in the same directory as the test file.

import parametrize_from_file

@parametrize_from_file
@pytest.mark.parametrize("x, expected", [
    (1, 2),
    (2, 4),
])
def test_double(x, expected):
    assert x * 2 == expected