Skip to content

Commit

Permalink
Adjust test imports to ensure all tests run once
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
colinpalmer committed Jul 25, 2024
1 parent 1eee325 commit 4c1126b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
13 changes: 8 additions & 5 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_bzip2mrcfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_gzipmrcfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 6 additions & 4 deletions tests/test_load_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_mrcfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -58,7 +57,7 @@
# return tests


class MrcFileTest(MrcObjectTest):
class MrcFileTest(test_mrcobject.MrcObjectTest):

"""Unit tests for MRC file I/O.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_mrcinterpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_mrcmemmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4c1126b

Please sign in to comment.