Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ install:
- python setup.py install

script:
- python setup.py check --restructuredtext
- cd tests
- py.test --fits
- py.test --arraydiff
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Using

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

python
import pytest
Expand Down Expand Up @@ -91,6 +91,10 @@ def test_image():
...
```

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``.

Expand Down
3 changes: 1 addition & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ build: false

test_script:
- "%CMD_IN_ENV% cd tests"
- "%CMD_IN_ENV% py.test --fits"

- "%CMD_IN_ENV% py.test --arraydiff"
12 changes: 9 additions & 3 deletions pytest_arraydiff/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def pytest_addoption(parser):
help="directory to generate reference files in, relative to location where py.test is run", action='store')
group.addoption('--arraydiff-reference-path',
help="directory containing reference files, relative to location where py.test is run", action='store')
group.addoption('--arraydiff-default-format',
help="Default format for the reference arrays (can be 'fits' or 'text' currently)")


def pytest_configure(config):
Expand All @@ -115,17 +117,21 @@ def pytest_configure(config):
if generate_dir is not None:
reference_dir = os.path.abspath(generate_dir)

default_format = config.getoption("--arraydiff-default-format") or 'text'

config.pluginmanager.register(ArrayComparison(config,
reference_dir=reference_dir,
generate_dir=generate_dir))
generate_dir=generate_dir,
default_format=default_format))


class ArrayComparison(object):

def __init__(self, config, reference_dir=None, generate_dir=None):
def __init__(self, config, reference_dir=None, generate_dir=None, default_format='text'):
self.config = config
self.reference_dir = reference_dir
self.generate_dir = generate_dir
self.default_format = default_format

def pytest_runtest_setup(self, item):

Expand All @@ -134,7 +140,7 @@ def pytest_runtest_setup(self, item):
if compare is None:
return

file_format = compare.kwargs.get('file_format', 'text')
file_format = compare.kwargs.get('file_format', self.default_format)

if file_format not in FORMATS:
raise ValueError("Unknown format: {0}".format(file_format))
Expand Down
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from setuptools import setup

from pytest_arraydiff import __version__
Expand All @@ -6,8 +7,11 @@
import pypandoc
long_description = pypandoc.convert('README.md', 'rst')
except (IOError, ImportError):
with open('README.md') as infile:
long_description = infile.read()
if 'register' in sys.argv:
raise
else:
with open('README.md') as infile:
long_description = infile.read()

setup(
version=__version__,
Expand Down
Binary file modified tests/baseline/test_succeeds_class.fits
Binary file not shown.
Binary file removed tests/baseline/test_succeeds_func.fits
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/baseline/test_succeeds_func_default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/baseline/test_succeeds_func_text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
Binary file modified tests/baseline/test_tolerance.fits
Binary file not shown.
37 changes: 32 additions & 5 deletions tests/test_pytest_arraydiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

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


@pytest.mark.array_compare(file_format='text', reference_dir=reference_dir)
def test_succeeds_func_text():
return np.arange(3 * 5 * 4).reshape((3, 5, 4))
return np.arange(3 * 5).reshape((3, 5))


@pytest.mark.array_compare(file_format='fits', reference_dir=reference_dir)
def test_succeeds_func_fits():
return np.arange(3 * 5 * 4).reshape((3, 5, 4))
return np.arange(3 * 5).reshape((3, 5))


class TestClass(object):

@pytest.mark.array_compare(reference_dir=reference_dir)
@pytest.mark.array_compare(file_format='fits', reference_dir=reference_dir)
def test_succeeds_class(self):
return np.arange(2 * 4 * 3).reshape((2, 4, 3))

Expand Down Expand Up @@ -91,7 +91,34 @@ def test_generate(file_format):
assert os.path.exists(os.path.join(gen_dir, 'test_gen.' + ('fits' if file_format == 'fits' else 'txt')))


@pytest.mark.array_compare(reference_dir=reference_dir, rtol=0.5)
TEST_DEFAULT = """
import pytest
import numpy as np
from astropy.io import fits
@pytest.mark.array_compare
def test_default():
return np.arange(6 * 5).reshape((6, 5))
"""

@pytest.mark.parametrize('file_format', ('fits', 'text'))
def test_default_format(file_format):

tmpdir = tempfile.mkdtemp()

test_file = os.path.join(tmpdir, 'test.py')
with open(test_file, 'w') as f:
f.write(TEST_DEFAULT)

gen_dir = os.path.join(tmpdir, 'spam', 'egg')

# If we do generate, the test should succeed and a new file will appear
code = subprocess.call('py.test -s --arraydiff-default-format={0}'
' --arraydiff-generate-path={1} {2}'.format(file_format, gen_dir, test_file), shell=True)
assert code == 0
assert os.path.exists(os.path.join(gen_dir, 'test_default.' + ('fits' if file_format == 'fits' else 'txt')))


@pytest.mark.array_compare(reference_dir=reference_dir, rtol=0.5, file_format='fits')
def test_tolerance():
return np.ones((3,4)) * 1.6

Expand Down