diff --git a/.travis.yml b/.travis.yml index 9dc9bc7..17db93e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,5 +17,6 @@ install: - python setup.py install script: + - python setup.py check --restructuredtext - cd tests - - py.test --fits + - py.test --arraydiff diff --git a/README.md b/README.md index 7647d74..ff90497 100644 --- a/README.md +++ b/README.md @@ -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 @@ -91,6 +91,10 @@ def test_image(): ... ``` +The default file format can also be specified using the +``--arraydiff-default-format=`` flag when running ``py.test``, and +```` 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``. diff --git a/appveyor.yml b/appveyor.yml index ab7611c..a689789 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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" diff --git a/pytest_arraydiff/plugin.py b/pytest_arraydiff/plugin.py index 300964a..d21dc54 100755 --- a/pytest_arraydiff/plugin.py +++ b/pytest_arraydiff/plugin.py @@ -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): @@ -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): @@ -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)) diff --git a/setup.py b/setup.py index c0c8aa2..6eeb1b9 100755 --- a/setup.py +++ b/setup.py @@ -1,3 +1,4 @@ +import sys from setuptools import setup from pytest_arraydiff import __version__ @@ -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__, diff --git a/tests/baseline/test_succeeds_class.fits b/tests/baseline/test_succeeds_class.fits index 2e6c1b1..6b5ccef 100644 Binary files a/tests/baseline/test_succeeds_class.fits and b/tests/baseline/test_succeeds_class.fits differ diff --git a/tests/baseline/test_succeeds_func.fits b/tests/baseline/test_succeeds_func.fits deleted file mode 100644 index eee021e..0000000 Binary files a/tests/baseline/test_succeeds_func.fits and /dev/null differ diff --git a/tests/baseline/test_succeeds_func_default.txt b/tests/baseline/test_succeeds_func_default.txt new file mode 100644 index 0000000..1e60a17 --- /dev/null +++ b/tests/baseline/test_succeeds_func_default.txt @@ -0,0 +1,3 @@ +0 1 2 3 4 +5 6 7 8 9 +10 11 12 13 14 diff --git a/tests/baseline/test_succeeds_func_array.fits b/tests/baseline/test_succeeds_func_fits.fits similarity index 85% rename from tests/baseline/test_succeeds_func_array.fits rename to tests/baseline/test_succeeds_func_fits.fits index 2950d77..2e452d9 100644 Binary files a/tests/baseline/test_succeeds_func_array.fits and b/tests/baseline/test_succeeds_func_fits.fits differ diff --git a/tests/baseline/test_succeeds_func_text.txt b/tests/baseline/test_succeeds_func_text.txt new file mode 100644 index 0000000..1e60a17 --- /dev/null +++ b/tests/baseline/test_succeeds_func_text.txt @@ -0,0 +1,3 @@ +0 1 2 3 4 +5 6 7 8 9 +10 11 12 13 14 diff --git a/tests/baseline/test_tolerance.fits b/tests/baseline/test_tolerance.fits index b6cd48c..9fab1ae 100644 Binary files a/tests/baseline/test_tolerance.fits and b/tests/baseline/test_tolerance.fits differ diff --git a/tests/test_pytest_arraydiff.py b/tests/test_pytest_arraydiff.py index 692ba33..afae0bb 100644 --- a/tests/test_pytest_arraydiff.py +++ b/tests/test_pytest_arraydiff.py @@ -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)) @@ -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