diff --git a/pytest_arraydiff/plugin.py b/pytest_arraydiff/plugin.py index 0801570..f982d71 100755 --- a/pytest_arraydiff/plugin.py +++ b/pytest_arraydiff/plugin.py @@ -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(']', '_') diff --git a/tests/baseline/test_pytest_arraydiff_TestDerivedOne_test_array_one.txt b/tests/baseline/test_pytest_arraydiff_TestDerivedOne_test_array_one.txt new file mode 100644 index 0000000..9f9974f --- /dev/null +++ b/tests/baseline/test_pytest_arraydiff_TestDerivedOne_test_array_one.txt @@ -0,0 +1,3 @@ +0 1 2 3 +4 5 6 7 +8 9 10 11 diff --git a/tests/baseline/test_pytest_arraydiff_TestDerivedTwo_test_array_one.txt b/tests/baseline/test_pytest_arraydiff_TestDerivedTwo_test_array_one.txt new file mode 100644 index 0000000..22169f8 --- /dev/null +++ b/tests/baseline/test_pytest_arraydiff_TestDerivedTwo_test_array_one.txt @@ -0,0 +1,2 @@ +0 1 2 3 4 +5 6 7 8 9 diff --git a/tests/test_pytest_arraydiff.py b/tests/test_pytest_arraydiff.py index 31c12cb..a201117 100644 --- a/tests/test_pytest_arraydiff.py +++ b/tests/test_pytest_arraydiff.py @@ -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() + + 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 \ No newline at end of file