From 4c1126babd1ad8632cd14f3a1b34c7985d5101bf Mon Sep 17 00:00:00 2001 From: Colin Palmer Date: Thu, 25 Jul 2024 10:00:29 +0100 Subject: [PATCH] Adjust test imports to ensure all tests run once Some classes had been missed from tests/__init__.py, meaning they were skipped when running 'unittest tests'. Others were being run multiple times because the classes themselves were imported into other test modules and so they were being found by 'unittest discover' or 'pytest'. Importing them as modules instead avoids this (thanks to https://stackoverflow.com/a/68976671) --- tests/__init__.py | 13 ++++++++----- tests/test_bzip2mrcfile.py | 4 ++-- tests/test_gzipmrcfile.py | 4 ++-- tests/test_load_functions.py | 10 ++++++---- tests/test_mrcfile.py | 5 ++--- tests/test_mrcinterpreter.py | 4 ++-- tests/test_mrcmemmap.py | 4 ++-- 7 files changed, 24 insertions(+), 20 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index d107e38..d5e8b88 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -11,25 +11,28 @@ from .test_bzip2mrcfile import Bzip2MrcFileTest from .test_command_line import CommandLineTest +from .test_dtypes import DtypesTest from .test_future_mrcfile import FutureMrcFileTest from .test_gzipmrcfile import GzipMrcFileTest -from .test_load_functions import LoadFunctionTest -from .test_mrcobject import MrcObjectTest -from .test_mrcinterpreter import MrcInterpreterTest +from .test_load_functions import LoadFunctionTest, LoadFunctionTestWithPathlib from .test_mrcfile import MrcFileTest +from .test_mrcinterpreter import MrcInterpreterTest from .test_mrcmemmap import MrcMemmapTest +from .test_mrcobject import MrcObjectTest from .test_utils import UtilsTest from .test_validation import ValidationTest test_classes = [ Bzip2MrcFileTest, CommandLineTest, + DtypesTest, FutureMrcFileTest, GzipMrcFileTest, LoadFunctionTest, - MrcObjectTest, - MrcInterpreterTest, + LoadFunctionTestWithPathlib, MrcFileTest, + MrcInterpreterTest, + MrcObjectTest, MrcMemmapTest, UtilsTest, ValidationTest diff --git a/tests/test_bzip2mrcfile.py b/tests/test_bzip2mrcfile.py index c8dc5b9..5462fe1 100644 --- a/tests/test_bzip2mrcfile.py +++ b/tests/test_bzip2mrcfile.py @@ -12,11 +12,11 @@ import os import unittest -from .test_mrcfile import MrcFileTest +from . import test_mrcfile from mrcfile.bzip2mrcfile import Bzip2MrcFile -class Bzip2MrcFileTest(MrcFileTest): +class Bzip2MrcFileTest(test_mrcfile.MrcFileTest): """Unit tests for bzip2 MRC file I/O. diff --git a/tests/test_gzipmrcfile.py b/tests/test_gzipmrcfile.py index 506bc9a..770a8e5 100644 --- a/tests/test_gzipmrcfile.py +++ b/tests/test_gzipmrcfile.py @@ -12,11 +12,11 @@ import os import unittest -from .test_mrcfile import MrcFileTest +from . import test_mrcfile from mrcfile.gzipmrcfile import GzipMrcFile -class GzipMrcFileTest(MrcFileTest): +class GzipMrcFileTest(test_mrcfile.MrcFileTest): """Unit tests for gzipped MRC file I/O. diff --git a/tests/test_load_functions.py b/tests/test_load_functions.py index 765a772..7ed8de1 100644 --- a/tests/test_load_functions.py +++ b/tests/test_load_functions.py @@ -103,10 +103,12 @@ def test_new_empty_file_with_open_function(self): .format(self.temp_mrc_name)) def test_error_overwriting_file_with_open_function(self): - assert not os.path.exists(self.temp_mrc_name) - open(self.temp_mrc_name, 'w+').close() - assert os.path.exists(self.temp_mrc_name) - with self.assertRaisesRegex(ValueError, "call 'mrcfile\.new\(\)'"): + # Convert name to str to avoid TypeErrors in Python 2.7 with pathlib + temp_mrc_name_str = str(self.temp_mrc_name) + assert not os.path.exists(temp_mrc_name_str) + open(temp_mrc_name_str, 'w+').close() + assert os.path.exists(temp_mrc_name_str) + with self.assertRaisesRegex(ValueError, r"call 'mrcfile\.new\(\)'"): mrcfile.open(self.temp_mrc_name, mode='w+') def test_header_only_opening(self): diff --git a/tests/test_mrcfile.py b/tests/test_mrcfile.py index 7a2a845..0e326bd 100644 --- a/tests/test_mrcfile.py +++ b/tests/test_mrcfile.py @@ -22,8 +22,7 @@ except ImportError: from numpy import ComplexWarning -from . import helpers -from .test_mrcobject import MrcObjectTest +from . import helpers, test_mrcobject from mrcfile.mrcfile import MrcFile from mrcfile.mrcobject import (IMAGE_STACK_SPACEGROUP, VOLUME_SPACEGROUP, VOLUME_STACK_SPACEGROUP) @@ -58,7 +57,7 @@ # return tests -class MrcFileTest(MrcObjectTest): +class MrcFileTest(test_mrcobject.MrcObjectTest): """Unit tests for MRC file I/O. diff --git a/tests/test_mrcinterpreter.py b/tests/test_mrcinterpreter.py index 1fdff3e..1f73f6e 100644 --- a/tests/test_mrcinterpreter.py +++ b/tests/test_mrcinterpreter.py @@ -15,12 +15,12 @@ import numpy as np -from .test_mrcobject import MrcObjectTest +from . import test_mrcobject from mrcfile.constants import MAP_ID_OFFSET_BYTES from mrcfile.mrcinterpreter import MrcInterpreter -class MrcInterpreterTest(MrcObjectTest): +class MrcInterpreterTest(test_mrcobject.MrcObjectTest): """Unit tests for MrcInterpreter class. diff --git a/tests/test_mrcmemmap.py b/tests/test_mrcmemmap.py index 237503e..9a1692d 100644 --- a/tests/test_mrcmemmap.py +++ b/tests/test_mrcmemmap.py @@ -14,11 +14,11 @@ import numpy as np -from .test_mrcfile import MrcFileTest +from . import test_mrcfile from mrcfile.mrcmemmap import MrcMemmap -class MrcMemmapTest(MrcFileTest): +class MrcMemmapTest(test_mrcfile.MrcFileTest): """Unit tests for MRC file I/O with memory-mapped files.