Package: pytest-arraydiff
AboutThis is apy.testplugin 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, e.g. pandas DataFrame).
You can then either run the
tests in a mode togeneratereference files from the arrays, or you can run
the tests incomparisonmode, 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 Numpyloadtxtoutput)The FITS format (requiresastropy). With this
format, tests can return either a Numpy array for a FITS HDU object.A pandas HDF5 format using the pandas HDFStoreFor more information on how to write tests to do this, see theUsingsection below.InstallingThis plugin is compatible with Python 2.7, and 3.5 and later, and
requirespytestandnumpyto be installed.To install, you can do:pip install pytest-arraydiffYou can check that the plugin is registered with pytest by doing:py.test --versionwhich will show a list of plugins:This is pytest version 2.7.1, imported from ...
setuptools registered plugins:
  pytest-arraydiff-0.1 at ...UsingTo 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-pathoption with the name of the directory
where the generated files should be placed:py.test --arraydiff-generate-path=referenceIf the directory does not exist, it will be created. The directory will
be interpreted as being relative to where you are runningpy.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 calledreferencerelative 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 --arraydiffand the tests will pass if the arrays are the same. If you omit the--arraydiffoption, the tests will run but will only check that the
code runs without checking the output arrays.OptionsThe@pytest.mark.array_comparemarker take an argument to specify
the format to use for the reference files:@pytest.mark.array_compare(file_format='text')deftest_array():...The default file format can also be specified using the--arraydiff-default-format=<format>flag when runningpy.test,
and<format>should be eitherfitsortext.The supported formats at this time aretextandfits, and
contributions for other formats are welcome. The default format istext.Additional arguments are the relative and absolute tolerances for floating
point values (which default to 1e-7 and 0, respectively):@pytest.mark.array_compare(rtol=20,atol=0.1)deftest_array():...You can also pass keyword arguments to the writers using thewrite_kwargs. For thetextformat, these arguments are passed tosavetxtwhile for thefitsformat they are passed to Astropy’sfits.writetofunction.@pytest.mark.array_compare(file_format='fits',write_kwargs={'output_verify':'silentfix'})deftest_array():...Other options include the name of the reference directory (which
defaults toreference) 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')deftest_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 withhttp://orhttps://and
end in a slash).Finally, you can also set a custom baseline directory globally when
running tests by runningpy.testwith:py.test --arraydiff --arraydiff-reference-path=baseline_arraysThis directory will be interpreted as being relative to where the tests
are run. In addition, if both this option and thereference_diroption in thearray_comparedecorator are used, the one in the
decorator takes precedence.Test failure exampleIf 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-arraydiffIf 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 --arraydiffThe reason for having to install the plugin first is to ensure that the
plugin is correctly loaded as part of the test suite.