Skip to content

Commit cbb0a6c

Browse files
committed
Use test_name in place of item name for saved array
Previously tests could not be inherited from classes in a way that let each class save its own array. This change makes the file written use the full test name, including the class name, instead of just the test name within the class.
1 parent 069039b commit cbb0a6c

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

pytest_arraydiff/plugin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,16 @@ 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
316+
elif derive_classes:
317+
filename = test_name
318+
filename = filename.replace('.', '_')
319+
filename = filename + '.' + extension
320+
filename = filename.replace('[', '_').replace(']', '_')
321+
filename = filename.replace('_.' + extension, '.' + extension)
315322
else:
316323
filename = item.name + '.' + extension
317324
filename = filename.replace('[', '_').replace(']', '_')
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)