Skip to content

Add option to use different reference arrays for derived test classes #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions pytest_arraydiff/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,16 @@ def pytest_runtest_call(self, item):

# Find test name to use as plot name
filename = compare.kwargs.get('filename', None)
derive_classes = compare.kwargs.get('derive_classes', False)
if filename is None:
if single_reference:
filename = item.originalname + '.' + extension
elif derive_classes:
filename = test_name
filename = filename.replace('.', '_')
filename = filename + '.' + extension
filename = filename.replace('[', '_').replace(']', '_')
filename = filename.replace('_.' + extension, '.' + extension)
else:
filename = item.name + '.' + extension
filename = filename.replace('[', '_').replace(']', '_')
Expand Down
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0 1 2 3 4
5 6 7 8 9
51 changes: 51 additions & 0 deletions tests/test_pytest_arraydiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,54 @@ def test_single_reference(self, spam):

def test_nofile():
pass

class BaseTestClass:
arrays = None
@pytest.mark.array_compare(reference_dir=reference_dir, file_format='text', derive_classes=True)
def test_array_one(self):
return self.array
class TestDerivedOne(BaseTestClass):
array = np.arange(3 * 4).reshape((3, 4))

class TestDerivedTwo(BaseTestClass):
array = np.arange(2 * 5).reshape((2, 5))



DERIVED_FAILING = """
import pytest
import numpy as np
class BaseTestClass:
arrays = None
@pytest.mark.array_compare(reference_dir="{reference_dir}", file_format='text')
def test_array_one(self):
return self.array
class TestDerivedOne(BaseTestClass):
array = np.arange(3 * 4).reshape((3, 4))

class TestDerivedTwo(BaseTestClass):
array = np.arange(2 * 5).reshape((2, 5))
"""


def test_derived_fails():

tmpdir = tempfile.mkdtemp()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably just use tmp_path fixture? https://docs.pytest.org/en/stable/how-to/tmp_path.html


test_file = os.path.join(tmpdir, 'test.py')
gen_dir = os.path.join(tmpdir, 'spam', 'egg')
with open(test_file, 'w') as f:
f.write(DERIVED_FAILING.format(reference_dir=gen_dir))

# If we use --arraydiff, it should detect that the file is missing
code = subprocess.call(f'pytest --arraydiff {test_file}', shell=True)
assert code != 0

# when we generate the test files without the derive option the generation should succeed
code = subprocess.call(['pytest', f'--arraydiff-generate-path={gen_dir}', test_file],
timeout=10)
assert code == 0

# but when the test is run again, it should fail, because the different tests are looking at the same file
code = subprocess.call(f'pytest --arraydiff {test_file}', shell=True)
assert code != 0
Loading