pytest-regressions
pytest-regressions is a Pytest plugin that provides a set of fixtures to simplify writing regression tests. It helps in maintaining tests that generate substantial or specific data files, such as dictionaries, dataframes, images, or numeric tables. The plugin stores expected data as baselines in a data directory (leveraging pytest-datadir) and compares future test runs against these baselines, providing detailed diffs upon failure. The current version is 2.10.0, and it maintains a regular release cadence with frequent minor updates.
Warnings
- gotcha Using the `--force-regen` or `--regen-all` pytest flags will regenerate and overwrite existing baseline files without failing the tests. While useful for updating baselines, misuse can lead to unintentional data loss if not carefully managed and committed to version control.
- gotcha Specific regression fixtures like `dataframe_regression`, `image_regression`, `num_regression`, and `ndarrays_regression` rely on optional dependencies (pandas, Pillow, numpy) which are NOT installed automatically with `pip install pytest-regressions`. Attempting to use these fixtures without the respective dependencies will result in `ImportError` or similar issues.
- gotcha When using parametrized tests with regression fixtures, a separate regression file will be generated for each parameterization. This can lead to a large number of baseline files and potentially very long filenames if parameter names are extensive, which might exceed operating system path length limits.
Install
-
pip install pytest-regressions
Imports
- data_regression
def test_data(data_regression): # No explicit import needed, pytest discovers fixtures automatically
Quickstart
import pytest
def summary_grids():
# In a real scenario, this would compute/read values
return {
"Main Grid": {
"id": 0,
"cell_count": 1000,
"active_cells": 300,
"properties": [
{"name": "Temperature", "min": 75, "max": 85},
{"name": "Porosity", "min": 0.3, "max": 0.4},
],
},
}
def test_grids_data(data_regression):
data = summary_grids()
data_regression.check(data)