Skip to content

Commit 68f0797

Browse files
authored
Merge pull request #2 from astrofrog/default-format
Make it possible to specify default file format for reference arrays
2 parents f411235 + 5d66e97 commit 68f0797

12 files changed

+61
-14
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ install:
1717
- python setup.py install
1818

1919
script:
20+
- python setup.py check --restructuredtext
2021
- cd tests
21-
- py.test --fits
22+
- py.test --arraydiff

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Using
4646

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

5151
python
5252
import pytest
@@ -91,6 +91,10 @@ def test_image():
9191
...
9292
```
9393

94+
The default file format can also be specified using the
95+
``--arraydiff-default-format=<format>`` flag when running ``py.test``, and
96+
``<format>`` should be either ``fits`` or ``text``.
97+
9498
The supported formats at this time are ``text`` and ``fits``, and contributions
9599
for other formats are welcome. The default format is ``text``.
96100

appveyor.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,4 @@ build: false
2929

3030
test_script:
3131
- "%CMD_IN_ENV% cd tests"
32-
- "%CMD_IN_ENV% py.test --fits"
33-
32+
- "%CMD_IN_ENV% py.test --arraydiff"

pytest_arraydiff/plugin.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ def pytest_addoption(parser):
9898
help="directory to generate reference files in, relative to location where py.test is run", action='store')
9999
group.addoption('--arraydiff-reference-path',
100100
help="directory containing reference files, relative to location where py.test is run", action='store')
101+
group.addoption('--arraydiff-default-format',
102+
help="Default format for the reference arrays (can be 'fits' or 'text' currently)")
101103

102104

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

120+
default_format = config.getoption("--arraydiff-default-format") or 'text'
121+
118122
config.pluginmanager.register(ArrayComparison(config,
119123
reference_dir=reference_dir,
120-
generate_dir=generate_dir))
124+
generate_dir=generate_dir,
125+
default_format=default_format))
121126

122127

123128
class ArrayComparison(object):
124129

125-
def __init__(self, config, reference_dir=None, generate_dir=None):
130+
def __init__(self, config, reference_dir=None, generate_dir=None, default_format='text'):
126131
self.config = config
127132
self.reference_dir = reference_dir
128133
self.generate_dir = generate_dir
134+
self.default_format = default_format
129135

130136
def pytest_runtest_setup(self, item):
131137

@@ -134,7 +140,7 @@ def pytest_runtest_setup(self, item):
134140
if compare is None:
135141
return
136142

137-
file_format = compare.kwargs.get('file_format', 'text')
143+
file_format = compare.kwargs.get('file_format', self.default_format)
138144

139145
if file_format not in FORMATS:
140146
raise ValueError("Unknown format: {0}".format(file_format))

setup.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from setuptools import setup
23

34
from pytest_arraydiff import __version__
@@ -6,8 +7,11 @@
67
import pypandoc
78
long_description = pypandoc.convert('README.md', 'rst')
89
except (IOError, ImportError):
9-
with open('README.md') as infile:
10-
long_description = infile.read()
10+
if 'register' in sys.argv:
11+
raise
12+
else:
13+
with open('README.md') as infile:
14+
long_description = infile.read()
1115

1216
setup(
1317
version=__version__,
0 Bytes
Binary file not shown.
-5.63 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0 1 2 3 4
2+
5 6 7 8 9
3+
10 11 12 13 14
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0 1 2 3 4
2+
5 6 7 8 9
3+
10 11 12 13 14

tests/baseline/test_tolerance.fits

0 Bytes
Binary file not shown.

tests/test_pytest_arraydiff.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010

1111
@pytest.mark.array_compare(reference_dir=reference_dir)
1212
def test_succeeds_func_default():
13-
return np.arange(3 * 5 * 4).reshape((3, 5, 4))
13+
return np.arange(3 * 5).reshape((3, 5))
1414

1515

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

2020

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

2525

2626
class TestClass(object):
2727

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

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

9393

94-
@pytest.mark.array_compare(reference_dir=reference_dir, rtol=0.5)
94+
TEST_DEFAULT = """
95+
import pytest
96+
import numpy as np
97+
from astropy.io import fits
98+
@pytest.mark.array_compare
99+
def test_default():
100+
return np.arange(6 * 5).reshape((6, 5))
101+
"""
102+
103+
@pytest.mark.parametrize('file_format', ('fits', 'text'))
104+
def test_default_format(file_format):
105+
106+
tmpdir = tempfile.mkdtemp()
107+
108+
test_file = os.path.join(tmpdir, 'test.py')
109+
with open(test_file, 'w') as f:
110+
f.write(TEST_DEFAULT)
111+
112+
gen_dir = os.path.join(tmpdir, 'spam', 'egg')
113+
114+
# If we do generate, the test should succeed and a new file will appear
115+
code = subprocess.call('py.test -s --arraydiff-default-format={0}'
116+
' --arraydiff-generate-path={1} {2}'.format(file_format, gen_dir, test_file), shell=True)
117+
assert code == 0
118+
assert os.path.exists(os.path.join(gen_dir, 'test_default.' + ('fits' if file_format == 'fits' else 'txt')))
119+
120+
121+
@pytest.mark.array_compare(reference_dir=reference_dir, rtol=0.5, file_format='fits')
95122
def test_tolerance():
96123
return np.ones((3,4)) * 1.6
97124

0 commit comments

Comments
 (0)