Skip to content

TEST: Add BaseTestCase class to skip TestCases starting with _ #871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions nibabel/testing_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
30 changes: 2 additions & 28 deletions nibabel/tests/test_wrapstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -231,17 +219,13 @@ 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)
assert str_io.getvalue() == hdr.binaryblock

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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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()
Expand Down