Pytest ArrayDiff
pytest-arraydiff is a pytest plugin designed to facilitate the generation and comparison of large data arrays produced during tests, avoiding the need to hard-code them. It allows tests to generate reference files or compare results against existing references within specified tolerances. The current version is 0.6.1, with releases occurring on an irregular but active cadence, typically every few months.
Warnings
- breaking The `astropy` library, previously a direct dependency for FITS format support, was removed as a hard requirement in v0.5.0. If you use the FITS format, you must now explicitly install `astropy` separately.
- gotcha When generating reference files using `--arraydiff-generate-path`, the created files are placed in the specified directory. These files should be manually reviewed for correctness and then moved to the designated 'reference' subdirectory relative to your test files.
- gotcha A test function decorated with `@pytest.mark.array_compare` must return the NumPy array (or other supported object like a pandas DataFrame) that is intended for comparison. If nothing is returned, the array comparison functionality will not be invoked.
- gotcha The default file format for comparison is 'text'. If you intend to use another format like 'fits' or 'pandas_hdf5', you must specify it either via the `file_format` argument in the decorator (`@pytest.mark.array_compare(file_format='fits')`) or using the `--arraydiff-default-format` command-line option (`pytest --arraydiff-default-format=fits`).
Install
-
pip install pytest-arraydiff
Imports
- pytest
import pytest
Quickstart
import pytest
import numpy as np
@pytest.mark.array_compare
def test_succeeds():
# This test will generate or compare an array
# run with `pytest --arraydiff-generate-path=reference_data`
# or `pytest --arraydiff-default-format=text`
return np.arange(3 * 5 * 4).reshape((3, 5, 4))