Skip to main content
CI Status

About

This is a py.test plugin to facilitate the generation and comparison of data arrays produced during tests, in particular in cases where the arrays are too large to conveniently hard-code them in the tests (e.g. np.testing.assert_allclose(x, [1, 2, 3])).

The basic idea is that you can write a test that generates a Numpy array (or other related objects depending on the format). You can then either run the tests in a mode to generate reference files from the arrays, or you can run the tests in comparison mode, which will compare the results of the tests to the reference ones within some tolerance.

At the moment, the supported file formats for the reference files are:

  • A plain text-based format (based on Numpy loadtxt output)

  • The FITS format (requires astropy). With this format, tests can return either a Numpy array for a FITS HDU object.

For more information on how to write tests to do this, see the Using section below.

Installing

This plugin is compatible with Python 2.7, and 3.5 and later, and requires pytest and numpy to be installed.

To install, you can do:

pip install pytest-arraydiff

You can check that the plugin is registered with pytest by doing:

py.test --version

which will show a list of plugins:

This is pytest version 2.7.1, imported from ...
setuptools registered plugins:
  pytest-arraydiff-0.1 at ...

Using

To use, you simply need to mark the function where you want to compare arrays using @pytest.mark.array_compare, and make sure that the function returns a plain Numpy array:

python
import pytest
import numpy as np

@pytest.mark.array_compare
def test_succeeds():
    return np.arange(3 * 5 * 4).reshape((3, 5, 4))

To generate the reference data files, run the tests with the --arraydiff-generate-path option with the name of the directory where the generated files should be placed:

py.test --arraydiff-generate-path=reference

If the directory does not exist, it will be created. The directory will be interpreted as being relative to where you are running py.test. Make sure you manually check the reference arrays to ensure they are correct.

Once you are happy with the generated data files, you should move them to a sub-directory called reference relative to the test files (this name is configurable, see below). You can also generate the baseline arrays directly in the right directory.

You can then run the tests simply with:

py.test --arraydiff

and the tests will pass if the arrays are the same. If you omit the --arraydiff option, the tests will run but will only check that the code runs without checking the output arrays.

Options

The @pytest.mark.array_compare marker take an argument to specify the format to use for the reference files:

@pytest.mark.array_compare(file_format='text')
def test_array():
    ...

The default file format can also be specified using the --arraydiff-default-format=<format> flag when running py.test, and <format> should be either fits or text.

The supported formats at this time are text and fits, and contributions for other formats are welcome. The default format is text.

Another argument is the relative tolerance for floating point values (which defaults to 1e-7):

@pytest.mark.array_compare(rtol=20)
def test_array():
    ...

You can also pass keyword arguments to the writers using the write_kwargs. For the text format, these arguments are passed to savetxt while for the fits format they are passed to Astropy’s fits.writeto function.

@pytest.mark.array_compare(file_format='fits', write_kwargs={'output_verify': 'silentfix'})
def test_array():
    ...

Other options include the name of the reference directory (which defaults to reference ) and the filename for the reference file (which defaults to the name of the test with a format-dependent extension).

@pytest.mark.array_compare(reference_dir='baseline_arrays',
                               filename='other_name.fits')
def test_array():
    ...

The reference directory in the decorator above will be interpreted as being relative to the test file. Note that the baseline directory can also be a URL (which should start with http:// or https:// and end in a slash).

Finally, you can also set a custom baseline directory globally when running tests by running py.test with:

py.test --arraydiff --arraydiff-reference-path=baseline_arrays

This directory will be interpreted as being relative to where the tests are run. In addition, if both this option and the reference_dir option in the array_compare decorator are used, the one in the decorator takes precedence.

Test failure example

If the arrays produced by the tests are correct, then the test will pass, but if they are not, the test will fail with a message similar to the following:

E               AssertionError:
E
E               a: /var/folders/zy/t1l3sx310d3d6p0kyxqzlrnr0000gr/T/tmpbvjkzt_q/test_to_mask_rect-mode_subpixels-subpixels_18.txt
E               b: /var/folders/zy/t1l3sx310d3d6p0kyxqzlrnr0000gr/T/tmpbvjkzt_q/reference-test_to_mask_rect-mode_subpixels-subpixels_18.txt
E
E               Not equal to tolerance rtol=1e-07, atol=0
E
E               (mismatch 47.22222222222222%)
E                x: array([[ 0.      ,  0.      ,  0.      ,  0.      ,  0.404012,  0.55    ,
E                        0.023765,  0.      ,  0.      ],
E                      [ 0.      ,  0.      ,  0.      ,  0.112037,  1.028704,  1.1     ,...
E                y: array([[ 0.      ,  0.      ,  0.      ,  0.      ,  0.367284,  0.5     ,
E                        0.021605,  0.      ,  0.      ],
E                      [ 0.      ,  0.      ,  0.      ,  0.101852,  0.935185,  1.      ,...

The file paths included in the exception are then available for inspection.

Running the tests for pytest-arraydiff

If you are contributing some changes and want to run the tests, first install the latest version of the plugin then do:

cd tests
py.test --arraydiff

The reason for having to install the plugin first is to ensure that the plugin is correctly loaded as part of the test suite.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-arraydiff-0.4.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_arraydiff-0.4.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest-arraydiff-0.4.0.tar.gz.

File metadata

  • Download URL: pytest-arraydiff-0.4.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pytest-arraydiff-0.4.0.tar.gz
Algorithm Hash digest
SHA256 55a675e84f4f7f4511e5b24d9b45d9772ef09a308066b6b17fb22ed1e22ebdf5
MD5 f1329e8b636f2381ebf2b56ee7f3aba0
BLAKE2b-256 862da24aee32d948f05b676e4fcd4d1afb5aa834621cfe383dc9cea31b921c39

See more details on using hashes here.

File details

Details for the file pytest_arraydiff-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_arraydiff-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pytest_arraydiff-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25635705a0a38b3ef336786afa39fa7a3525dac85c9699ba666055b0af6973e8
MD5 c0677ddfa51f55438738a07bbefa6c9b
BLAKE2b-256 23dfc0808e83754dfbc7f6a6b0b614a686dd75980cf03efe465e5cd51570b88c

See more details on using hashes here.

Release history Release notifications | RSS feed

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

0.5.0

2 files

This release

0.4.0 This release

2 files

0.3

2 files

0.2

2 files

0.1

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page