### Install pytest-regressions via pip Source: https://pytest-regressions.readthedocs.io/en/latest/installation.html Standard installation command for the base package. ```bash $ pip install pytest-regressions ``` -------------------------------- ### Install pytest-regressions via conda Source: https://pytest-regressions.readthedocs.io/en/latest/installation.html Installation command for users of the conda package manager. ```bash $ conda install -c conda-forge pytest-regressions ``` -------------------------------- ### Install pytest-regressions with extra dependencies Source: https://pytest-regressions.readthedocs.io/en/latest/installation.html Install the package with support for dataframe, num, or image fixtures. ```bash $ pip install pytest-regressions[dataframe,num,image] ``` -------------------------------- ### Example failure message Source: https://pytest-regressions.readthedocs.io/en/latest/overview.html The output displayed when a regression file is missing and needs to be created. ```text > pytest.fail(msg) E Failed: File not found in data directory, created: E - C:\Users\bruno\pytest-regressions\tests\test_grids\test_grids2.yml ``` -------------------------------- ### Manual assertion test Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/overview.rst.txt An example of manual assertions that are prone to maintenance issues as data grows. ```python def test_grids(): data = summary_grids() assert data["Main Grid"]["id"] == 0 assert data["Main Grid"]["cell_count"] == 1000 assert data["Main Grid"]["active_cells"] == 300 assert data["Main Grid"]["properties"] == [ {"name": "Temperature", "min": 75, "max": 85}, {"name": "Porosity", "min": 0.3, "max": 0.4}, ] ... ``` -------------------------------- ### Define a sample data function Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/overview.rst.txt A function returning a dictionary representing simulation grid data for testing purposes. ```python def summary_grids(): 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}, ], }, "Refin1": { "id": 1, "cell_count": 48, "active_cells": 44, "properties": [ {"name": "Temperature", "min": 78, "max": 81}, {"name": "Porosity", "min": 0.36, "max": 0.39}, ], }, } ``` -------------------------------- ### Generate dynamic filenames with request fixture Source: https://pytest-regressions.readthedocs.io/en/latest/overview.html Combine the request fixture with basename to create filenames based on the test name with custom prefixes or suffixes. ```python def test_grids3(data_regression, data1, data2, request): data_regression.check(data1, basename=f"{request.node.name}_data1") data_regression.check(data2, basename=f"{request.node.name}_data2") ``` -------------------------------- ### Use data_regression fixture Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/overview.rst.txt Simplifies testing by using the data_regression fixture to automatically check dictionary data against a baseline file. ```python def test_grids2(data_regression): data = summary_grids() data_regression.check(data) ``` -------------------------------- ### Register custom ini options Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/overview.rst.txt Registers custom configuration options in conftest.py to support directory overrides. ```python def pytest_addoption(parser): parser.addini("lazy_datadir", "my own datadir for pytest-regressions") parser.addini("original_datadir", "my own original_datadir for pytest-regressions") ``` -------------------------------- ### Regenerate all regression files Source: https://pytest-regressions.readthedocs.io/en/latest/overview.html Use the --regen-all flag to update all regression files without failing the tests. ```bash $ pytest --regen-all ``` -------------------------------- ### Override data directory paths Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/overview.rst.txt Configures custom data directories by overriding the lazy_datadir and original_datadir fixtures. ```python import pytest from pathlib import Path from myapp.config import PATH @pytest.fixture(scope="session") def lazy_datadir() -> Path: return PATH.repo / "test-data-regression" @pytest.fixture(scope="session") def original_datadir() -> Path: return PATH.repo / "test-data-regression" ``` -------------------------------- ### Generated YAML baseline file Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/overview.rst.txt The format of the baseline file generated by the data_regression fixture. ```yaml Main Grid: active_cells: 300 cell_count: 1000 id: 0 properties: - max: 85 min: 75 name: Temperature - max: 0.4 min: 0.3 name: Porosity Refin1: active_cells: 44 cell_count: 48 id: 1 properties: - max: 81 min: 78 name: Temperature - max: 0.39 min: 0.36 name: Porosity ``` -------------------------------- ### DataRegressionFixture.check Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/api.rst.txt Checks data against a reference file. ```APIDOC ## DataRegressionFixture.check ### Description Verifies that the provided data matches the stored reference data. ``` -------------------------------- ### Force regeneration of regression files Source: https://pytest-regressions.readthedocs.io/en/latest/overview.html Use the --force-regen flag to update expected regression files when implementation changes are intentional. ```bash $ pytest --force-regen ``` -------------------------------- ### Configure data directories via ini file Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/overview.rst.txt Defines custom data directory paths in the pytest configuration file. ```ini [pytest] lazy_datadir = mydatadir original_datadir = my_originaldatadir ``` -------------------------------- ### ImageRegressionFixture.check Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/api.rst.txt Checks image data against a reference file. ```APIDOC ## ImageRegressionFixture.check ### Description Verifies that the provided image data matches the stored reference image. ``` -------------------------------- ### FileRegressionFixture.check Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/api.rst.txt Checks file content against a reference file. ```APIDOC ## FileRegressionFixture.check ### Description Verifies that the content of a file matches the stored reference file. ``` -------------------------------- ### Check numeric data with num_regression Source: https://pytest-regressions.readthedocs.io/en/latest/api.html Verifies a dictionary of numeric arrays against a recorded reference file. ```python num_regression.check({ 'U_gas': U[0][positions], 'U_liquid': U[1][positions], 'gas_vol_frac [-]': vol_frac[0][positions], 'liquid_vol_frac [-]': vol_frac[1][positions], 'P': Pa_to_bar(P)[positions], }) ``` -------------------------------- ### Override fixtures using ini options Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/overview.rst.txt Implements fixture overrides that read paths from the pytest configuration. ```python import pytest import pathlib @pytest.fixture(scope="session") def original_datadir(request) -> pathlib.Path: config = request.config return config.rootpath / config.getini('original_datadir') @pytest.fixture(scope="session") def lazy_datadir(request) -> pathlib.Path: config = request.config return config.rootpath / config.getini('lazy_datadir') ``` -------------------------------- ### Parametrize tests with data_regression Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/overview.rst.txt Uses pytest parametrization to generate unique regression files for different data inputs. ```python @pytest.mark.parametrize('data', [summary_grids(), summary_grids_2()]) def test_grids3(data_regression, data): data_regression.check(data) ``` ```python @pytest.mark.parametrize('data', [summary_grids(), summary_grids_2()], ids=['short', 'long']) def test_grids3(data_regression, data): data_regression.check(data) ``` -------------------------------- ### Check NumPy ndarrays with regression fixture Source: https://pytest-regressions.readthedocs.io/en/latest/api.html Verifies a dictionary of NumPy arrays against recorded data using specified tolerances. ```python def test_some_data(ndarrays_regression): points, values = some_function() ndarrays_regression.check( { 'points': points, # array with shape (100, 3) 'values': values, # array with shape (100,) }, default_tolerance=dict(atol=1e-8, rtol=1e-8) ) ``` -------------------------------- ### NumericRegressionFixture.check Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/api.rst.txt Checks numeric data against a reference file. ```APIDOC ## NumericRegressionFixture.check ### Description Verifies that the provided numeric data matches the stored reference data. ``` -------------------------------- ### NDArraysRegressionFixture.check Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/api.rst.txt Checks numpy arrays against a reference file. ```APIDOC ## NDArraysRegressionFixture.check ### Description Verifies that the provided numpy arrays match the stored reference data. ``` -------------------------------- ### Define column-specific tolerances Source: https://pytest-regressions.readthedocs.io/en/latest/api.html Provide a dictionary mapping DataFrame keys to specific tolerance settings for validation. ```python tolerances={'U': dict(atol=1e-2)} ``` -------------------------------- ### Customize regression filenames Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/overview.rst.txt Controls the output filename of regression data using the basename parameter. ```python def test_grids3(data_regression, data): data_regression.check(data, basename="my_custom_name") ``` ```python def test_grids3(data_regression, data1, data2, request): data_regression.check(data1, basename=f"{request.node.name}_data1") data_regression.check(data2, basename=f"{request.node.name}_data2") ``` -------------------------------- ### Define default tolerances Source: https://pytest-regressions.readthedocs.io/en/latest/api.html Set global tolerance values for the check call, overriding numpy's default isclose behavior. ```python default_tolerance=dict(atol=1e-7, rtol=1e-18). ``` -------------------------------- ### DataFrameRegressionFixture.check Source: https://pytest-regressions.readthedocs.io/en/latest/_sources/api.rst.txt Checks pandas DataFrame against a reference file. ```APIDOC ## DataFrameRegressionFixture.check ### Description Verifies that the provided pandas DataFrame matches the stored reference data. ``` -------------------------------- ### Define numeric regression tolerances Source: https://pytest-regressions.readthedocs.io/en/latest/api.html Specifies custom absolute or relative tolerances for numeric comparisons. ```python tolerances={'U': Tolerance(atol=1e-2)} ``` -------------------------------- ### Check pandas DataFrame against regression data Source: https://pytest-regressions.readthedocs.io/en/latest/api.html Use this method to validate a numeric pandas DataFrame against a stored reference or generate a new one. ```python data_frame = pandas.DataFrame.from_dict({ 'U_gas': U[0][positions], 'U_liquid': U[1][positions], 'gas_vol_frac [-]': vol_frac[0][positions], 'liquid_vol_frac [-]': vol_frac[1][positions], 'P': Pa_to_bar(P)[positions], }) dataframe_regression.check(data_frame) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.