From a23c63a24593aca804377e8837e78d9193d03724 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Fri, 7 Feb 2020 00:53:20 -0500 Subject: [PATCH 1/3] TEST: Use vanilla unittest.skip* over pytest --- nibabel/freesurfer/tests/test_io.py | 7 ++-- nibabel/tests/nibabel_data.py | 10 +++--- nibabel/tests/test_dft.py | 13 +++++--- nibabel/tests/test_image_api.py | 9 ++++-- nibabel/tests/test_parrec_data.py | 3 +- nibabel/tests/test_processing.py | 6 ++-- nibabel/tests/test_proxy_api.py | 3 +- nibabel/tests/test_scripts.py | 3 +- nibabel/tests/test_spm99analyze.py | 50 +++++++++-------------------- nibabel/tests/test_viewers.py | 3 +- 10 files changed, 49 insertions(+), 58 deletions(-) diff --git a/nibabel/freesurfer/tests/test_io.py b/nibabel/freesurfer/tests/test_io.py index 521923057f..e1ec971c65 100644 --- a/nibabel/freesurfer/tests/test_io.py +++ b/nibabel/freesurfer/tests/test_io.py @@ -6,10 +6,9 @@ import hashlib import warnings - from ...tmpdirs import InTemporaryDirectory - +import unittest import pytest import numpy as np from numpy.testing import assert_allclose, assert_array_equal @@ -36,8 +35,8 @@ data_path = pjoin(nib_data, 'nitest-freesurfer', DATA_SDIR) have_freesurfer = isdir(data_path) -freesurfer_test = pytest.mark.skipif(not have_freesurfer, - reason='cannot find freesurfer {0} directory'.format(DATA_SDIR)) +freesurfer_test = unittest.skipUnless(have_freesurfer, + 'cannot find freesurfer {0} directory'.format(DATA_SDIR)) def _hash_file_content(fname): hasher = hashlib.md5() diff --git a/nibabel/tests/nibabel_data.py b/nibabel/tests/nibabel_data.py index e2e5bc9ed3..3c1b58502d 100644 --- a/nibabel/tests/nibabel_data.py +++ b/nibabel/tests/nibabel_data.py @@ -4,7 +4,7 @@ from os import environ, listdir from os.path import dirname, realpath, join as pjoin, isdir, exists -import pytest +import unittest def get_nibabel_data(): @@ -39,11 +39,11 @@ def needs_nibabel_data(subdir=None): """ nibabel_data = get_nibabel_data() if nibabel_data == '': - return pytest.mark.skipif(True, reason="Need nibabel-data directory for this test") + return unittest.skip("Need nibabel-data directory for this test") if subdir is None: - return pytest.mark.skipif(False, reason="Don't skip") + return lambda x: x required_path = pjoin(nibabel_data, subdir) # Path should not be empty (as is the case for not-updated submodules) have_files = exists(required_path) and len(listdir(required_path)) > 0 - return pytest.mark.skipif(not have_files, - reason="Need files in {0} for these tests".format(required_path)) \ No newline at end of file + return unittest.skipUnless(have_files, + "Need files in {0} for these tests".format(required_path)) diff --git a/nibabel/tests/test_dft.py b/nibabel/tests/test_dft.py index d50ba4023e..9c3a4f5d85 100644 --- a/nibabel/tests/test_dft.py +++ b/nibabel/tests/test_dft.py @@ -10,6 +10,7 @@ from .. import dft from .. import nifti1 +import unittest import pytest # Shield optional package imports @@ -22,10 +23,12 @@ data_dir = pjoin(dirname(__file__), 'data') -pytestmark = [ - pytest.mark.skipif(os.name == 'nt', reason='FUSE not available for windows, skipping dft tests'), - pytest.mark.skipif(not have_dicom, reason='Need pydicom for dft tests, skipping') -] +def setUpModule(): + if os.name == 'nt': + raise unittest.SkipTest('FUSE not available for windows, skipping dft tests') + if not have_dicom: + raise unittest.SkipTest('Need pydicom for dft tests, skipping') + def test_init(): dft.clear_cache() @@ -76,7 +79,7 @@ def test_storage_instance(): pass -@pytest.mark.skipif(not have_pil, reason='could not import PIL.Image') +@unittest.skipUnless(have_pil, 'could not import PIL.Image') def test_png(): studies = dft.get_studies(data_dir) data = studies[0].series[0].as_png() diff --git a/nibabel/tests/test_image_api.py b/nibabel/tests/test_image_api.py index f5aeb53822..f6245aa594 100644 --- a/nibabel/tests/test_image_api.py +++ b/nibabel/tests/test_image_api.py @@ -41,6 +41,7 @@ from ..spatialimages import SpatialImage from .. import minc1, minc2, parrec, brikhead +import unittest import pytest from numpy.testing import assert_almost_equal, assert_array_equal, assert_warns, assert_allclose @@ -121,7 +122,7 @@ def validate_filenames(self, imaker, params): # Validate the filename, file_map interface if not self.can_save: - pytest.skip() + raise unittest.SkipTest img = imaker() img.set_data_dtype(np.float32) # to avoid rounding in load / save # Make sure the object does not have a file_map @@ -706,8 +707,12 @@ class TestMinc1API(ImageHeaderAPI): example_images = MINC1_EXAMPLE_IMAGES -@pytest.mark.skipif(not have_h5py, reason="Need h5py for Minc2 tests") class TestMinc2API(TestMinc1API): + + def __init__(self): + if not have_h5py: + raise unittest.SkipTest('Need h5py for these tests') + klass = image_maker = Minc2Image loader = minc2.load example_images = MINC2_EXAMPLE_IMAGES diff --git a/nibabel/tests/test_parrec_data.py b/nibabel/tests/test_parrec_data.py index 30295a269a..e626068ca1 100644 --- a/nibabel/tests/test_parrec_data.py +++ b/nibabel/tests/test_parrec_data.py @@ -12,6 +12,7 @@ from .nibabel_data import get_nibabel_data, needs_nibabel_data +import unittest import pytest from numpy.testing import assert_almost_equal @@ -60,7 +61,7 @@ def test_fieldmap(): fieldmap_nii = pjoin(BALLS, 'NIFTI', 'fieldmap.nii.gz') load(fieldmap_par) top_load(fieldmap_nii) - raise pytest.skip('Fieldmap remains puzzling') + raise unittest.SkipTest('Fieldmap remains puzzling') @needs_nibabel_data('parrec_oblique') diff --git a/nibabel/tests/test_processing.py b/nibabel/tests/test_processing.py index dea34b85a2..1dd64c6a5c 100644 --- a/nibabel/tests/test_processing.py +++ b/nibabel/tests/test_processing.py @@ -27,14 +27,14 @@ voxel_sizes) from nibabel.eulerangles import euler2mat -from numpy.testing import (assert_almost_equal, - assert_array_equal) +from numpy.testing import assert_almost_equal, assert_array_equal +import unittest import pytest from nibabel.tests.test_spaces import assert_all_in, get_outspace_params from nibabel.testing import assert_allclose_safely -needs_scipy = pytest.mark.skipif(not have_scipy, reason='These tests need scipy') +needs_scipy = unittest.skipUnless(have_scipy, 'These tests need scipy') DATA_DIR = pjoin(dirname(__file__), 'data') diff --git a/nibabel/tests/test_proxy_api.py b/nibabel/tests/test_proxy_api.py index c74d4fbdb2..f8e25c14cc 100644 --- a/nibabel/tests/test_proxy_api.py +++ b/nibabel/tests/test_proxy_api.py @@ -51,6 +51,7 @@ from ..arrayproxy import ArrayProxy, is_proxy +import unittest import pytest from numpy.testing import assert_almost_equal, assert_array_equal, assert_allclose @@ -428,7 +429,7 @@ def eg_func(): arr_out=arr_out)) def validate_header_isolated(self, pmaker, params): - raise pytest.skip('ECAT header does not support dtype get') + raise unittest.SkipTest('ECAT header does not support dtype get') class TestPARRECAPI(_TestProxyAPI): diff --git a/nibabel/tests/test_scripts.py b/nibabel/tests/test_scripts.py index 19dd5f6fd0..c8c87092bf 100644 --- a/nibabel/tests/test_scripts.py +++ b/nibabel/tests/test_scripts.py @@ -20,6 +20,7 @@ from ..loadsave import load from ..orientations import flip_axis, aff2axcodes, inv_ornt_aff +import unittest import pytest from numpy.testing import assert_almost_equal @@ -120,7 +121,7 @@ def test_nib_ls(args): check_nib_ls_example4d(*args) -@pytest.mark.skipif(not load_small_file(), reason="can't load the small.mnc file") +@unittest.skipUnless(load_small_file(), "Can't load the small.mnc file") @script_test def test_nib_ls_multiple(): # verify that correctly lists/formats for multiple files diff --git a/nibabel/tests/test_spm99analyze.py b/nibabel/tests/test_spm99analyze.py index 977fef3071..3198c43ea7 100644 --- a/nibabel/tests/test_spm99analyze.py +++ b/nibabel/tests/test_spm99analyze.py @@ -13,14 +13,15 @@ from io import BytesIO from numpy.testing import assert_array_equal, assert_array_almost_equal +import unittest import pytest -# Decorator to skip tests requiring save / load if scipy not available for mat -# files from ..optpkg import optional_package _, have_scipy, _ = optional_package('scipy') -scipy_skip = pytest.mark.skipif(not have_scipy, reason='scipy not available') +# Decorator to skip tests requiring save / load if scipy not available for mat +# files +needs_scipy = unittest.skipUnless(have_scipy, 'scipy not available') from ..spm99analyze import (Spm99AnalyzeHeader, Spm99AnalyzeImage, HeaderTypeError) @@ -409,38 +410,17 @@ class TestSpm99AnalyzeImage(test_analyze.TestAnalyzeImage, ImageScalingMixin): image_class = Spm99AnalyzeImage # Decorating the old way, before the team invented @ - test_data_hdr_cache = (scipy_skip( - test_analyze.TestAnalyzeImage.test_data_hdr_cache - )) - - test_header_updating = (scipy_skip( - test_analyze.TestAnalyzeImage.test_header_updating - )) - - test_offset_to_zero = (scipy_skip( - test_analyze.TestAnalyzeImage.test_offset_to_zero - )) - - test_big_offset_exts = (scipy_skip( - test_analyze.TestAnalyzeImage.test_big_offset_exts - )) - - test_header_scaling = scipy_skip( - ImageScalingMixin.test_header_scaling) - - test_int_int_scaling = scipy_skip( - ImageScalingMixin.test_int_int_scaling) - - test_write_scaling = scipy_skip( - ImageScalingMixin.test_write_scaling) - - test_no_scaling = scipy_skip( - ImageScalingMixin.test_no_scaling) - - test_nan2zero_range_ok = scipy_skip( - ImageScalingMixin.test_nan2zero_range_ok) - - @scipy_skip + test_data_hdr_cache = needs_scipy(test_analyze.TestAnalyzeImage.test_data_hdr_cache) + test_header_updating = needs_scipy(test_analyze.TestAnalyzeImage.test_header_updating) + test_offset_to_zero = needs_scipy(test_analyze.TestAnalyzeImage.test_offset_to_zero) + test_big_offset_exts = needs_scipy(test_analyze.TestAnalyzeImage.test_big_offset_exts) + test_header_scaling = needs_scipy(ImageScalingMixin.test_header_scaling) + test_int_int_scaling = needs_scipy(ImageScalingMixin.test_int_int_scaling) + test_write_scaling = needs_scipy(ImageScalingMixin.test_write_scaling) + test_no_scaling = needs_scipy(ImageScalingMixin.test_no_scaling) + test_nan2zero_range_ok = needs_scipy(ImageScalingMixin.test_nan2zero_range_ok) + + @needs_scipy def test_mat_read(self): # Test mat file reading and writing for the SPM analyze types img_klass = self.image_class diff --git a/nibabel/tests/test_viewers.py b/nibabel/tests/test_viewers.py index 1be6e400a2..8bcb5bda06 100644 --- a/nibabel/tests/test_viewers.py +++ b/nibabel/tests/test_viewers.py @@ -16,11 +16,12 @@ from numpy.testing import assert_array_equal, assert_equal +import unittest import pytest # Need at least MPL 1.3 for viewer tests. matplotlib, has_mpl, _ = optional_package('matplotlib', min_version='1.3') -needs_mpl = pytest.mark.skipif(not has_mpl, reason='These tests need matplotlib') +needs_mpl = unittest.skipUnless(has_mpl, 'These tests need matplotlib') if has_mpl: matplotlib.use('Agg') From 2542320e7e528c91d548f42951963b400fb9c6c7 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Fri, 7 Feb 2020 15:20:14 -0500 Subject: [PATCH 2/3] RF: Remove unused pieces of testing_pytest and switch to unittest skip --- nibabel/testing_pytest/__init__.py | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/nibabel/testing_pytest/__init__.py b/nibabel/testing_pytest/__init__.py index 71217612ed..52055ebcc3 100644 --- a/nibabel/testing_pytest/__init__.py +++ b/nibabel/testing_pytest/__init__.py @@ -13,17 +13,12 @@ import sys import warnings 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 -skipif = dec.skipif -slow = dec.slow +from numpy.testing import assert_array_equal -from ..deprecated import deprecate_with_version as _deprecate_with_version from .np_features import memmap_after_ufunc from .helpers import bytesio_filemap, bytesio_round_trip, assert_data_similar @@ -63,7 +58,7 @@ def assert_allclose_safely(a, b, match_nans=True, rtol=1e-5, atol=1e-8): a, b = np.broadcast_arrays(a, b) if match_nans: nans = np.isnan(a) - np.testing.assert_array_equal(nans, np.isnan(b)) + assert_array_equal(nans, np.isnan(b)) to_test = ~nans else: to_test = np.ones(a.shape, dtype=bool) @@ -199,20 +194,12 @@ class suppress_warnings(error_warnings): filter = 'ignore' -@_deprecate_with_version('catch_warn_reset is deprecated; use ' - 'nibabel.testing.clear_and_catch_warnings.', - since='2.1.0', until='3.0.0') -class catch_warn_reset(clear_and_catch_warnings): - pass - - EXTRA_SET = os.environ.get('NIPY_EXTRA_TESTS', '').split(',') def runif_extra_has(test_str): """Decorator checks to see if NIPY_EXTRA_TESTS env var contains test_str""" - return skipif(test_str not in EXTRA_SET, - "Skip {0} tests.".format(test_str)) + return unittest.skipUnless(test_str in EXTRA_SET, "Skip {0} tests.".format(test_str)) def assert_arr_dict_equal(dict1, dict2): From a223c071b59cd00dcf1cdd2044975746b9f8f74b Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Fri, 7 Feb 2020 21:20:23 -0500 Subject: [PATCH 3/3] CI: Only ignore files that use fixtures --- .azure-pipelines/windows.yml | 58 +----------------------------------- .travis.yml | 58 +----------------------------------- 2 files changed, 2 insertions(+), 114 deletions(-) diff --git a/.azure-pipelines/windows.yml b/.azure-pipelines/windows.yml index 8de4ed3466..fcdd542223 100644 --- a/.azure-pipelines/windows.yml +++ b/.azure-pipelines/windows.yml @@ -41,73 +41,17 @@ jobs: cd for_testing cp ../.coveragerc . nosetests --with-doctest --with-coverage --cover-package nibabel nibabel ^ - -I test_array_sequence ^ - -I test_tractogram ^ - -I test_api_validators ^ - -I test_arrayproxy ^ - -I test_arraywriters ^ - -I test_batteryrunners ^ - -I test_brikhead ^ - -I test_casting ^ - -I test_cifti2io_axes ^ - -I test_cifti2io_header ^ -I test_data ^ - -I test_deprecated ^ - -I test_deprecator ^ - -I test_dicomwrappers ^ - -I test_dft ^ - -I test_ecat ^ - -I test_ecat_data ^ - -I test_endiancodes ^ -I test_environment ^ -I test_euler ^ - -I test_filebasedimages ^ - -I test_filehandles ^ - -I test_fileholders ^ - -I test_filename_parser ^ - -I test_files_interface ^ - -I test_fileslice ^ - -I test_fileutils ^ - -I test_floating ^ - -I test_funcs ^ -I test_giftiio ^ - -I test_h5py_compat ^ - -I test_image_api ^ - -I test_image_load_save ^ - -I test_image_types ^ - -I test_imageclasses ^ - -I test_imageglobals ^ - -I test_io ^ - -I test_keywordonly ^ - -I test_loadsave ^ - -I test_minc1 ^ - -I test_minc2 ^ - -I test_minc2_data ^ - -I test_mriutils ^ -I test_netcdf ^ - -I test_nibabel_data ^ - -I test_nifti1 ^ - -I test_nifti2 ^ - -I test_openers ^ - -I test_optpkg ^ - -I test_orientations ^ - -I test_parrec ^ - -I test_parrec_data ^ -I test_pkg_info ^ - -I test_processing ^ - -I test_proxy_api ^ -I test_quaternions ^ - -I test_recoder ^ - -I test_remmovalschedule ^ - -I test_round_trip ^ - -I test_rstutils ^ -I test_scaling ^ - -I test_wrapstruct ^ - -I test_io ^ -I test_scripts ^ -I test_spaces ^ - -I test_testing ^ - -I test_wrapstruct + -I test_testing displayName: 'Nose tests' condition: and(succeeded(), eq(variables['CHECK_TYPE'], 'nosetests')) - script: | diff --git a/.travis.yml b/.travis.yml index 711601764b..857867c712 100644 --- a/.travis.yml +++ b/.travis.yml @@ -134,73 +134,17 @@ script: cd for_testing cp ../.coveragerc . nosetests --with-doctest --with-coverage --cover-package nibabel nibabel \ - -I test_array_sequence \ - -I test_tractogram \ - -I test_api_validators \ - -I test_arrayproxy \ - -I test_arraywriters \ - -I test_batteryrunners \ - -I test_brikhead \ - -I test_casting \ - -I test_cifti2io_axes \ - -I test_cifti2io_header \ -I test_data \ - -I test_deprecated \ - -I test_deprecator \ - -I test_dicomwrappers \ - -I test_dft \ - -I test_ecat \ - -I test_ecat_data \ - -I test_endiancodes \ -I test_environment \ -I test_euler \ - -I test_filebasedimages \ - -I test_filehandles \ - -I test_fileholders \ - -I test_filename_parser \ - -I test_files_interface \ - -I test_fileslice \ - -I test_fileutils \ - -I test_floating \ - -I test_funcs \ -I test_giftiio \ - -I test_h5py_compat \ - -I test_image_api \ - -I test_image_load_save \ - -I test_image_types \ - -I test_imageclasses \ - -I test_imageglobals \ - -I test_io \ - -I test_keywordonly \ - -I test_loadsave \ - -I test_minc1 \ - -I test_minc2 \ - -I test_minc2_data \ - -I test_mriutils \ -I test_netcdf \ - -I test_nibabel_data \ - -I test_nifti1 \ - -I test_nifti2 \ - -I test_openers \ - -I test_optpkg \ - -I test_orientations \ - -I test_parrec \ - -I test_parrec_data \ -I test_pkg_info \ - -I test_processing \ - -I test_proxy_api \ -I test_quaternions \ - -I test_recoder \ - -I test_remmovalschedule \ - -I test_round_trip \ - -I test_rstutils \ -I test_scaling \ - -I test_wrapstruct \ - -I test_io \ -I test_scripts \ -I test_spaces \ - -I test_testing \ - -I test_wrapstruct + -I test_testing elif [ "${CHECK_TYPE}" == "test" ]; then # Change into an innocuous directory and find tests from installation mkdir for_testing