Skip to content

Commit b5de6d1

Browse files
committed
Clarify language in test comments
1 parent 3f3b5ba commit b5de6d1

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

pytest_arraydiff/plugin.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,20 @@ def pytest_runtest_call(self, item):
309309

310310
# Find test name to use as plot name
311311
filename = compare.kwargs.get('filename', None)
312+
derive_classes = compare.kwargs.get('derive_classes', False)
312313
if filename is None:
313314
if single_reference:
314315
filename = item.originalname + '.' + extension
315-
else:
316+
elif derive_classes:
316317
filename = test_name
317318
filename = filename.replace('.', '_')
318319
filename = filename + '.' + extension
319320
filename = filename.replace('[', '_').replace(']', '_')
320321
filename = filename.replace('_.' + extension, '.' + extension)
322+
else:
323+
filename = item.name + '.' + extension
324+
filename = filename.replace('[', '_').replace(']', '_')
325+
filename = filename.replace('_.' + extension, '.' + extension)
321326

322327
# What we do now depends on whether we are generating the reference
323328
# files or simply running the test.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0 1 2 3
2+
4 5 6 7
3+
8 9 10 11
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0 1 2 3 4
2+
5 6 7 8 9

tests/test_pytest_arraydiff.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,54 @@ def test_single_reference(self, spam):
176176

177177
def test_nofile():
178178
pass
179+
180+
class BaseTestClass:
181+
arrays = None
182+
@pytest.mark.array_compare(reference_dir=reference_dir, file_format='text', derive_classes=True)
183+
def test_array_one(self):
184+
return self.array
185+
class TestDerivedOne(BaseTestClass):
186+
array = np.arange(3 * 4).reshape((3, 4))
187+
188+
class TestDerivedTwo(BaseTestClass):
189+
array = np.arange(2 * 5).reshape((2, 5))
190+
191+
192+
193+
DERIVED_FAILING = """
194+
import pytest
195+
import numpy as np
196+
class BaseTestClass:
197+
arrays = None
198+
@pytest.mark.array_compare(reference_dir="{reference_dir}", file_format='text')
199+
def test_array_one(self):
200+
return self.array
201+
class TestDerivedOne(BaseTestClass):
202+
array = np.arange(3 * 4).reshape((3, 4))
203+
204+
class TestDerivedTwo(BaseTestClass):
205+
array = np.arange(2 * 5).reshape((2, 5))
206+
"""
207+
208+
209+
def test_derived_fails():
210+
211+
tmpdir = tempfile.mkdtemp()
212+
213+
test_file = os.path.join(tmpdir, 'test.py')
214+
gen_dir = os.path.join(tmpdir, 'spam', 'egg')
215+
with open(test_file, 'w') as f:
216+
f.write(DERIVED_FAILING.format(reference_dir=gen_dir))
217+
218+
# If we use --arraydiff, it should detect that the file is missing
219+
code = subprocess.call(f'pytest --arraydiff {test_file}', shell=True)
220+
assert code != 0
221+
222+
# when we generate the test files without the derive option the generation should succeed
223+
code = subprocess.call(['pytest', f'--arraydiff-generate-path={gen_dir}', test_file],
224+
timeout=10)
225+
assert code == 0
226+
227+
# but when the test is run again, it should fail, because the different tests are looking at the same file
228+
code = subprocess.call(f'pytest --arraydiff {test_file}', shell=True)
229+
assert code != 0

0 commit comments

Comments
 (0)