Skip to content

Commit 4ee9e34

Browse files
authored
Merge pull request #871 from effigies/test/base_test_case
TEST: Add BaseTestCase class to skip TestCases starting with ``_``
2 parents f2ff7fe + 60479c4 commit 4ee9e34

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

nibabel/testing_pytest/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from pkg_resources import resource_filename
1616
from os.path import dirname, abspath, join as pjoin
1717

18+
import unittest
19+
1820
import numpy as np
1921
from numpy.testing import assert_array_equal, assert_warns
2022
from numpy.testing import dec
@@ -220,3 +222,15 @@ def assert_arr_dict_equal(dict1, dict2):
220222
for key, value1 in dict1.items():
221223
value2 = dict2[key]
222224
assert_array_equal(value1, value2)
225+
226+
227+
class BaseTestCase(unittest.TestCase):
228+
""" TestCase that does not attempt to run if prefixed with a ``_``
229+
230+
This restores the nose-like behavior of skipping so-named test cases
231+
in test runners like pytest.
232+
"""
233+
def setUp(self):
234+
if self.__class__.__name__.startswith('_'):
235+
raise unittest.SkipTest("Base test case - subclass to run")
236+
super().setUp()

nibabel/tests/test_wrapstruct.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from ..spatialimages import HeaderDataError
3535
from .. import imageglobals
3636

37-
from unittest import TestCase, SkipTest
37+
from ..testing_pytest import BaseTestCase
3838

3939
from numpy.testing import assert_array_equal
4040
import pytest
@@ -106,7 +106,7 @@ def log_chk(hdr, level):
106106
return hdrc, message, raiser
107107

108108

109-
class _TestWrapStructBase(TestCase):
109+
class _TestWrapStructBase(BaseTestCase):
110110
''' Class implements base tests for binary headers
111111
112112
It serves as a base class for other binary header tests
@@ -119,8 +119,6 @@ def get_bad_bb(self):
119119
return None
120120

121121
def test_general_init(self):
122-
if not self.header_class:
123-
pytest.skip()
124122
hdr = self.header_class()
125123
# binaryblock has length given by header data dtype
126124
binblock = hdr.binaryblock
@@ -140,8 +138,6 @@ def _set_something_into_hdr(self, hdr):
140138

141139
def test__eq__(self):
142140
# Test equal and not equal
143-
if not self.header_class:
144-
pytest.skip()
145141
hdr1 = self.header_class()
146142
hdr2 = self.header_class()
147143
assert hdr1 == hdr2
@@ -158,8 +154,6 @@ def test__eq__(self):
158154

159155
def test_to_from_fileobj(self):
160156
# Successful write using write_to
161-
if not self.header_class:
162-
pytest.skip()
163157
hdr = self.header_class()
164158
str_io = BytesIO()
165159
hdr.write_to(str_io)
@@ -169,8 +163,6 @@ def test_to_from_fileobj(self):
169163
assert hdr2.binaryblock == hdr.binaryblock
170164

171165
def test_mappingness(self):
172-
if not self.header_class:
173-
pytest.skip()
174166
hdr = self.header_class()
175167
with pytest.raises(ValueError):
176168
hdr.__setitem__('nonexistent key', 0.1)
@@ -207,16 +199,12 @@ def test_endianness_ro(self):
207199
endianness on initialization (or occasionally byteswapping the
208200
data) - but this is done via via the as_byteswapped method
209201
'''
210-
if not self.header_class:
211-
pytest.skip()
212202
hdr = self.header_class()
213203
with pytest.raises(AttributeError):
214204
hdr.__setattr__('endianness', '<')
215205

216206
def test_endian_guess(self):
217207
# Check guesses of endian
218-
if not self.header_class:
219-
pytest.skip()
220208
eh = self.header_class()
221209
assert eh.endianness == native_code
222210
hdr_data = eh.structarr.copy()
@@ -231,17 +219,13 @@ def test_binblock_is_file(self):
231219
# strings following. More generally, there may be other perhaps
232220
# optional data after the binary block, in which case you will need to
233221
# override this test
234-
if not self.header_class:
235-
pytest.skip()
236222
hdr = self.header_class()
237223
str_io = BytesIO()
238224
hdr.write_to(str_io)
239225
assert str_io.getvalue() == hdr.binaryblock
240226

241227
def test_structarr(self):
242228
# structarr attribute also read only
243-
if not self.header_class:
244-
pytest.skip()
245229
hdr = self.header_class()
246230
# Just check we can get structarr
247231
hdr.structarr
@@ -260,8 +244,6 @@ def assert_no_log_err(self, hdr):
260244

261245
def test_bytes(self):
262246
# Test get of bytes
263-
if not self.header_class:
264-
pytest.skip()
265247
hdr1 = self.header_class()
266248
bb = hdr1.binaryblock
267249
hdr2 = self.header_class(hdr1.binaryblock)
@@ -292,8 +274,6 @@ def test_bytes(self):
292274

293275
def test_as_byteswapped(self):
294276
# Check byte swapping
295-
if not self.header_class:
296-
pytest.skip()
297277
hdr = self.header_class()
298278
assert hdr.endianness == native_code
299279
# same code just returns a copy
@@ -318,8 +298,6 @@ def check_fix(self, *args, **kwargs):
318298

319299
def test_empty_check(self):
320300
# Empty header should be error free
321-
if not self.header_class:
322-
pytest.skip()
323301
hdr = self.header_class()
324302
hdr.check_fix(error_level=0)
325303

@@ -329,8 +307,6 @@ def _dxer(self, hdr):
329307
return self.header_class.diagnose_binaryblock(binblock)
330308

331309
def test_str(self):
332-
if not self.header_class:
333-
pytest.skip()
334310
hdr = self.header_class()
335311
# Check something returns from str
336312
s1 = str(hdr)
@@ -344,8 +320,6 @@ class _TestLabeledWrapStruct(_TestWrapStructBase):
344320
def test_get_value_label(self):
345321
# Test get value label method
346322
# Make a new class to avoid overwriting recoders of original
347-
if not self.header_class:
348-
pytest.skip()
349323
class MyHdr(self.header_class):
350324
_field_recoders = {}
351325
hdr = MyHdr()

0 commit comments

Comments
 (0)