From 60479c4879cd27095303ac320ad95c0f6b54b2af Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 4 Feb 2020 09:33:59 -0500 Subject: [PATCH] TEST: Add BaseTestCase class to skip TestCases starting with ``_`` --- nibabel/testing_pytest/__init__.py | 14 ++++++++++++++ nibabel/tests/test_wrapstruct.py | 30 ++---------------------------- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/nibabel/testing_pytest/__init__.py b/nibabel/testing_pytest/__init__.py index 38143d9c38..71217612ed 100644 --- a/nibabel/testing_pytest/__init__.py +++ b/nibabel/testing_pytest/__init__.py @@ -15,6 +15,8 @@ from pkg_resources import resource_filename from os.path import dirname, abspath, join as pjoin +import unittest + import numpy as np from numpy.testing import assert_array_equal, assert_warns from numpy.testing import dec @@ -220,3 +222,15 @@ def assert_arr_dict_equal(dict1, dict2): for key, value1 in dict1.items(): value2 = dict2[key] assert_array_equal(value1, value2) + + +class BaseTestCase(unittest.TestCase): + """ TestCase that does not attempt to run if prefixed with a ``_`` + + This restores the nose-like behavior of skipping so-named test cases + in test runners like pytest. + """ + def setUp(self): + if self.__class__.__name__.startswith('_'): + raise unittest.SkipTest("Base test case - subclass to run") + super().setUp() diff --git a/nibabel/tests/test_wrapstruct.py b/nibabel/tests/test_wrapstruct.py index f627109fd0..f052098475 100644 --- a/nibabel/tests/test_wrapstruct.py +++ b/nibabel/tests/test_wrapstruct.py @@ -34,7 +34,7 @@ from ..spatialimages import HeaderDataError from .. import imageglobals -from unittest import TestCase, SkipTest +from ..testing_pytest import BaseTestCase from numpy.testing import assert_array_equal import pytest @@ -106,7 +106,7 @@ def log_chk(hdr, level): return hdrc, message, raiser -class _TestWrapStructBase(TestCase): +class _TestWrapStructBase(BaseTestCase): ''' Class implements base tests for binary headers It serves as a base class for other binary header tests @@ -119,8 +119,6 @@ def get_bad_bb(self): return None def test_general_init(self): - if not self.header_class: - pytest.skip() hdr = self.header_class() # binaryblock has length given by header data dtype binblock = hdr.binaryblock @@ -140,8 +138,6 @@ def _set_something_into_hdr(self, hdr): def test__eq__(self): # Test equal and not equal - if not self.header_class: - pytest.skip() hdr1 = self.header_class() hdr2 = self.header_class() assert hdr1 == hdr2 @@ -158,8 +154,6 @@ def test__eq__(self): def test_to_from_fileobj(self): # Successful write using write_to - if not self.header_class: - pytest.skip() hdr = self.header_class() str_io = BytesIO() hdr.write_to(str_io) @@ -169,8 +163,6 @@ def test_to_from_fileobj(self): assert hdr2.binaryblock == hdr.binaryblock def test_mappingness(self): - if not self.header_class: - pytest.skip() hdr = self.header_class() with pytest.raises(ValueError): hdr.__setitem__('nonexistent key', 0.1) @@ -207,16 +199,12 @@ def test_endianness_ro(self): endianness on initialization (or occasionally byteswapping the data) - but this is done via via the as_byteswapped method ''' - if not self.header_class: - pytest.skip() hdr = self.header_class() with pytest.raises(AttributeError): hdr.__setattr__('endianness', '<') def test_endian_guess(self): # Check guesses of endian - if not self.header_class: - pytest.skip() eh = self.header_class() assert eh.endianness == native_code hdr_data = eh.structarr.copy() @@ -231,8 +219,6 @@ def test_binblock_is_file(self): # strings following. More generally, there may be other perhaps # optional data after the binary block, in which case you will need to # override this test - if not self.header_class: - pytest.skip() hdr = self.header_class() str_io = BytesIO() hdr.write_to(str_io) @@ -240,8 +226,6 @@ def test_binblock_is_file(self): def test_structarr(self): # structarr attribute also read only - if not self.header_class: - pytest.skip() hdr = self.header_class() # Just check we can get structarr hdr.structarr @@ -260,8 +244,6 @@ def assert_no_log_err(self, hdr): def test_bytes(self): # Test get of bytes - if not self.header_class: - pytest.skip() hdr1 = self.header_class() bb = hdr1.binaryblock hdr2 = self.header_class(hdr1.binaryblock) @@ -292,8 +274,6 @@ def test_bytes(self): def test_as_byteswapped(self): # Check byte swapping - if not self.header_class: - pytest.skip() hdr = self.header_class() assert hdr.endianness == native_code # same code just returns a copy @@ -318,8 +298,6 @@ def check_fix(self, *args, **kwargs): def test_empty_check(self): # Empty header should be error free - if not self.header_class: - pytest.skip() hdr = self.header_class() hdr.check_fix(error_level=0) @@ -329,8 +307,6 @@ def _dxer(self, hdr): return self.header_class.diagnose_binaryblock(binblock) def test_str(self): - if not self.header_class: - pytest.skip() hdr = self.header_class() # Check something returns from str s1 = str(hdr) @@ -344,8 +320,6 @@ class _TestLabeledWrapStruct(_TestWrapStructBase): def test_get_value_label(self): # Test get value label method # Make a new class to avoid overwriting recoders of original - if not self.header_class: - pytest.skip() class MyHdr(self.header_class): _field_recoders = {} hdr = MyHdr()