Skip to content

Commit 4c1126b

Browse files
committed
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)
1 parent 1eee325 commit 4c1126b

File tree

7 files changed

+24
-20
lines changed

7 files changed

+24
-20
lines changed

tests/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,28 @@
1111

1212
from .test_bzip2mrcfile import Bzip2MrcFileTest
1313
from .test_command_line import CommandLineTest
14+
from .test_dtypes import DtypesTest
1415
from .test_future_mrcfile import FutureMrcFileTest
1516
from .test_gzipmrcfile import GzipMrcFileTest
16-
from .test_load_functions import LoadFunctionTest
17-
from .test_mrcobject import MrcObjectTest
18-
from .test_mrcinterpreter import MrcInterpreterTest
17+
from .test_load_functions import LoadFunctionTest, LoadFunctionTestWithPathlib
1918
from .test_mrcfile import MrcFileTest
19+
from .test_mrcinterpreter import MrcInterpreterTest
2020
from .test_mrcmemmap import MrcMemmapTest
21+
from .test_mrcobject import MrcObjectTest
2122
from .test_utils import UtilsTest
2223
from .test_validation import ValidationTest
2324

2425
test_classes = [
2526
Bzip2MrcFileTest,
2627
CommandLineTest,
28+
DtypesTest,
2729
FutureMrcFileTest,
2830
GzipMrcFileTest,
2931
LoadFunctionTest,
30-
MrcObjectTest,
31-
MrcInterpreterTest,
32+
LoadFunctionTestWithPathlib,
3233
MrcFileTest,
34+
MrcInterpreterTest,
35+
MrcObjectTest,
3336
MrcMemmapTest,
3437
UtilsTest,
3538
ValidationTest

tests/test_bzip2mrcfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import os
1313
import unittest
1414

15-
from .test_mrcfile import MrcFileTest
15+
from . import test_mrcfile
1616
from mrcfile.bzip2mrcfile import Bzip2MrcFile
1717

1818

19-
class Bzip2MrcFileTest(MrcFileTest):
19+
class Bzip2MrcFileTest(test_mrcfile.MrcFileTest):
2020

2121
"""Unit tests for bzip2 MRC file I/O.
2222

tests/test_gzipmrcfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import os
1313
import unittest
1414

15-
from .test_mrcfile import MrcFileTest
15+
from . import test_mrcfile
1616
from mrcfile.gzipmrcfile import GzipMrcFile
1717

1818

19-
class GzipMrcFileTest(MrcFileTest):
19+
class GzipMrcFileTest(test_mrcfile.MrcFileTest):
2020

2121
"""Unit tests for gzipped MRC file I/O.
2222

tests/test_load_functions.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ def test_new_empty_file_with_open_function(self):
103103
.format(self.temp_mrc_name))
104104

105105
def test_error_overwriting_file_with_open_function(self):
106-
assert not os.path.exists(self.temp_mrc_name)
107-
open(self.temp_mrc_name, 'w+').close()
108-
assert os.path.exists(self.temp_mrc_name)
109-
with self.assertRaisesRegex(ValueError, "call 'mrcfile\.new\(\)'"):
106+
# Convert name to str to avoid TypeErrors in Python 2.7 with pathlib
107+
temp_mrc_name_str = str(self.temp_mrc_name)
108+
assert not os.path.exists(temp_mrc_name_str)
109+
open(temp_mrc_name_str, 'w+').close()
110+
assert os.path.exists(temp_mrc_name_str)
111+
with self.assertRaisesRegex(ValueError, r"call 'mrcfile\.new\(\)'"):
110112
mrcfile.open(self.temp_mrc_name, mode='w+')
111113

112114
def test_header_only_opening(self):

tests/test_mrcfile.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
except ImportError:
2323
from numpy import ComplexWarning
2424

25-
from . import helpers
26-
from .test_mrcobject import MrcObjectTest
25+
from . import helpers, test_mrcobject
2726
from mrcfile.mrcfile import MrcFile
2827
from mrcfile.mrcobject import (IMAGE_STACK_SPACEGROUP, VOLUME_SPACEGROUP,
2928
VOLUME_STACK_SPACEGROUP)
@@ -58,7 +57,7 @@
5857
# return tests
5958

6059

61-
class MrcFileTest(MrcObjectTest):
60+
class MrcFileTest(test_mrcobject.MrcObjectTest):
6261

6362
"""Unit tests for MRC file I/O.
6463

tests/test_mrcinterpreter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
import numpy as np
1717

18-
from .test_mrcobject import MrcObjectTest
18+
from . import test_mrcobject
1919
from mrcfile.constants import MAP_ID_OFFSET_BYTES
2020
from mrcfile.mrcinterpreter import MrcInterpreter
2121

2222

23-
class MrcInterpreterTest(MrcObjectTest):
23+
class MrcInterpreterTest(test_mrcobject.MrcObjectTest):
2424

2525
"""Unit tests for MrcInterpreter class.
2626

tests/test_mrcmemmap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
import numpy as np
1616

17-
from .test_mrcfile import MrcFileTest
17+
from . import test_mrcfile
1818
from mrcfile.mrcmemmap import MrcMemmap
1919

2020

21-
class MrcMemmapTest(MrcFileTest):
21+
class MrcMemmapTest(test_mrcfile.MrcFileTest):
2222

2323
"""Unit tests for MRC file I/O with memory-mapped files.
2424

0 commit comments

Comments
 (0)