diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 7025e2dd7d9..f8c38c47ca1 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -54,9 +54,9 @@ Breaking changes [...] Note that both versions are currently supported, but using the old syntax will - produce a warning encouraging users to adopt the new syntax. + produce a warning encouraging users to adopt the new syntax. By `Daniel Rothenberg `_. - + - ``repr`` and the Jupyter Notebook won't automatically compute dask variables. Datasets loaded with ``open_dataset`` won't automatically read coords from disk when calling ``repr`` (:issue:`1522`). @@ -212,6 +212,10 @@ Bug fixes the first argument was a numpy variable (:issue:`1588`). By `Guido Imperiale `_. +- Fix bug when using ``pytest`` class decorators to skiping certain unittests. + The previous behavior unintentionally causing additional tests to be skipped + (:issue:`1531`). By `Joe Hamman `_. + .. _whats-new.0.9.6: v0.9.6 (8 June 2017) diff --git a/xarray/tests/__init__.py b/xarray/tests/__init__.py index d3f448f823c..33bad7e5ebd 100644 --- a/xarray/tests/__init__.py +++ b/xarray/tests/__init__.py @@ -5,6 +5,7 @@ from contextlib import contextmanager from distutils.version import LooseVersion import re +import importlib import numpy as np from numpy.testing import assert_array_equal @@ -25,111 +26,50 @@ except ImportError: import mock -try: - import scipy - has_scipy = True -except ImportError: - has_scipy = False - -try: - import pydap.client - has_pydap = True -except ImportError: - has_pydap = False - -try: - import netCDF4 - has_netCDF4 = True -except ImportError: - has_netCDF4 = False - - -try: - import h5netcdf - has_h5netcdf = True -except ImportError: - has_h5netcdf = False - - -try: - import Nio - has_pynio = True -except ImportError: - has_pynio = False - - -try: - import dask.array - import dask - dask.set_options(get=dask.get) - has_dask = True -except ImportError: - has_dask = False - - -try: - import matplotlib - has_matplotlib = True -except ImportError: - has_matplotlib = False - -try: - import bottleneck - if LooseVersion(bottleneck.__version__) < LooseVersion('1.1'): - raise ImportError('Fall back to numpy') - has_bottleneck = True -except ImportError: - has_bottleneck = False - -try: - import rasterio - has_rasterio = True -except ImportError: - has_rasterio = False - -try: - import pathlib - has_pathlib = True -except ImportError: +def _importorskip(modname, minversion=None): try: - import pathlib2 - has_pathlib = True + mod = importlib.import_module(modname) + has = True + if minversion is not None: + if LooseVersion(mod.__version__) < LooseVersion(minversion): + raise ImportError('Minimum version not satisfied') except ImportError: - has_pathlib = False - - -# slighly simpler construction that the full functions. -# Generally `pytest.importorskip('package')` inline is even easier -requires_matplotlib = pytest.mark.skipif( - not has_matplotlib, reason='requires matplotlib') -requires_scipy = pytest.mark.skipif( - not has_scipy, reason='requires scipy') -requires_pydap = pytest.mark.skipif( - not has_pydap, reason='requires pydap') -requires_netCDF4 = pytest.mark.skipif( - not has_netCDF4, reason='requires netCDF4') -requires_h5netcdf = pytest.mark.skipif( - not has_h5netcdf, reason='requires h5netcdf') -requires_pynio = pytest.mark.skipif( - not has_pynio, reason='requires pynio') -requires_scipy_or_netCDF4 = pytest.mark.skipif( - not has_scipy and not has_netCDF4, reason='requires scipy or netCDF4') -requires_dask = pytest.mark.skipif( - not has_dask, reason='requires dask') -requires_bottleneck = pytest.mark.skipif( - not has_bottleneck, reason='requires bottleneck') -requires_rasterio = pytest.mark.skipif( - not has_rasterio, reason='requires rasterio') -requires_pathlib = pytest.mark.skipif( - not has_pathlib, reason='requires pathlib / pathlib2' -) - + has = False + # TODO: use pytest.skipif instead of unittest.skipUnless + # Using `unittest.skipUnless` is a temporary workaround for pytest#568, + # wherein class decorators stain inherited classes. + # xref: xarray#1531, implemented in xarray #1557. + func = unittest.skipUnless(has, reason='requires {}'.format(modname)) + return has, func + + +has_matplotlib, requires_matplotlib = _importorskip('matplotlib') +has_scipy, requires_scipy = _importorskip('scipy') +has_pydap, requires_pydap = _importorskip('pydap.client') +has_netCDF4, requires_netCDF4 = _importorskip('netCDF4') +has_h5netcdf, requires_h5netcdf = _importorskip('h5netcdf') +has_pynio, requires_pynio = _importorskip('pynio') +has_dask, requires_dask = _importorskip('dask') +has_bottleneck, requires_bottleneck = _importorskip('bottleneck') +has_rasterio, requires_rasterio = _importorskip('rasterio') +has_pathlib, requires_pathlib = _importorskip('pathlib') + +# some special cases +has_scipy_or_netCDF4 = has_scipy or has_netCDF4 +requires_scipy_or_netCDF4 = unittest.skipUnless( + has_scipy_or_netCDF4, reason='requires scipy or netCDF4') +if not has_pathlib: + has_pathlib, requires_pathlib = _importorskip('pathlib2') + +if has_dask: + import dask + dask.set_options(get=dask.get) try: _SKIP_FLAKY = not pytest.config.getoption("--run-flaky") _SKIP_NETWORK_TESTS = not pytest.config.getoption("--run-network-tests") -except ValueError: +except (ValueError, AttributeError): # Can't get config from pytest, e.g., because xarray is installed instead # of being run from a development version (and hence conftests.py is not # available). Don't run flaky tests. diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index a977868c7e6..828151d55a0 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -1179,7 +1179,7 @@ def test_encoding_unlimited_dims(self): # tests pending h5netcdf fix -@pytest.mark.xfail +@unittest.skip class H5NetCDFDataTestAutocloseTrue(H5NetCDFDataTest): autoclose = True