Skip to content

Commit e713668

Browse files
author
Joe Hamman
authored
fix unintentional skipped tests (#1557)
* move requires_pynio to fix GH1531, refactor tests/__init__.py * remove ModuleNotFoundError * no pytest decorators on classes with multiple inheritance in test_backends.py * wrap class decorators for tests * no more class decorators in test_backends * whats new and some more cleanup * whatsnew update * use unittest skip decorators, way easier * update workaround comment
1 parent 41f4828 commit e713668

File tree

3 files changed

+45
-101
lines changed

3 files changed

+45
-101
lines changed

doc/whats-new.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ Breaking changes
5454
[...]
5555

5656
Note that both versions are currently supported, but using the old syntax will
57-
produce a warning encouraging users to adopt the new syntax.
57+
produce a warning encouraging users to adopt the new syntax.
5858
By `Daniel Rothenberg <https://github.com/darothen>`_.
59-
59+
6060
- ``repr`` and the Jupyter Notebook won't automatically compute dask variables.
6161
Datasets loaded with ``open_dataset`` won't automatically read coords from
6262
disk when calling ``repr`` (:issue:`1522`).
@@ -212,6 +212,10 @@ Bug fixes
212212
the first argument was a numpy variable (:issue:`1588`).
213213
By `Guido Imperiale <https://github.com/crusaderky>`_.
214214

215+
- Fix bug when using ``pytest`` class decorators to skiping certain unittests.
216+
The previous behavior unintentionally causing additional tests to be skipped
217+
(:issue:`1531`). By `Joe Hamman <https://github.com/jhamman>`_.
218+
215219
.. _whats-new.0.9.6:
216220

217221
v0.9.6 (8 June 2017)

xarray/tests/__init__.py

Lines changed: 38 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from contextlib import contextmanager
66
from distutils.version import LooseVersion
77
import re
8+
import importlib
89

910
import numpy as np
1011
from numpy.testing import assert_array_equal
@@ -25,111 +26,50 @@
2526
except ImportError:
2627
import mock
2728

28-
try:
29-
import scipy
30-
has_scipy = True
31-
except ImportError:
32-
has_scipy = False
33-
34-
try:
35-
import pydap.client
36-
has_pydap = True
37-
except ImportError:
38-
has_pydap = False
39-
40-
try:
41-
import netCDF4
42-
has_netCDF4 = True
43-
except ImportError:
44-
has_netCDF4 = False
45-
46-
47-
try:
48-
import h5netcdf
49-
has_h5netcdf = True
50-
except ImportError:
51-
has_h5netcdf = False
52-
53-
54-
try:
55-
import Nio
56-
has_pynio = True
57-
except ImportError:
58-
has_pynio = False
59-
60-
61-
try:
62-
import dask.array
63-
import dask
64-
dask.set_options(get=dask.get)
65-
has_dask = True
66-
except ImportError:
67-
has_dask = False
68-
69-
70-
try:
71-
import matplotlib
72-
has_matplotlib = True
73-
except ImportError:
74-
has_matplotlib = False
75-
7629

77-
try:
78-
import bottleneck
79-
if LooseVersion(bottleneck.__version__) < LooseVersion('1.1'):
80-
raise ImportError('Fall back to numpy')
81-
has_bottleneck = True
82-
except ImportError:
83-
has_bottleneck = False
84-
85-
try:
86-
import rasterio
87-
has_rasterio = True
88-
except ImportError:
89-
has_rasterio = False
90-
91-
try:
92-
import pathlib
93-
has_pathlib = True
94-
except ImportError:
30+
def _importorskip(modname, minversion=None):
9531
try:
96-
import pathlib2
97-
has_pathlib = True
32+
mod = importlib.import_module(modname)
33+
has = True
34+
if minversion is not None:
35+
if LooseVersion(mod.__version__) < LooseVersion(minversion):
36+
raise ImportError('Minimum version not satisfied')
9837
except ImportError:
99-
has_pathlib = False
100-
101-
102-
# slighly simpler construction that the full functions.
103-
# Generally `pytest.importorskip('package')` inline is even easier
104-
requires_matplotlib = pytest.mark.skipif(
105-
not has_matplotlib, reason='requires matplotlib')
106-
requires_scipy = pytest.mark.skipif(
107-
not has_scipy, reason='requires scipy')
108-
requires_pydap = pytest.mark.skipif(
109-
not has_pydap, reason='requires pydap')
110-
requires_netCDF4 = pytest.mark.skipif(
111-
not has_netCDF4, reason='requires netCDF4')
112-
requires_h5netcdf = pytest.mark.skipif(
113-
not has_h5netcdf, reason='requires h5netcdf')
114-
requires_pynio = pytest.mark.skipif(
115-
not has_pynio, reason='requires pynio')
116-
requires_scipy_or_netCDF4 = pytest.mark.skipif(
117-
not has_scipy and not has_netCDF4, reason='requires scipy or netCDF4')
118-
requires_dask = pytest.mark.skipif(
119-
not has_dask, reason='requires dask')
120-
requires_bottleneck = pytest.mark.skipif(
121-
not has_bottleneck, reason='requires bottleneck')
122-
requires_rasterio = pytest.mark.skipif(
123-
not has_rasterio, reason='requires rasterio')
124-
requires_pathlib = pytest.mark.skipif(
125-
not has_pathlib, reason='requires pathlib / pathlib2'
126-
)
127-
38+
has = False
39+
# TODO: use pytest.skipif instead of unittest.skipUnless
40+
# Using `unittest.skipUnless` is a temporary workaround for pytest#568,
41+
# wherein class decorators stain inherited classes.
42+
# xref: xarray#1531, implemented in xarray #1557.
43+
func = unittest.skipUnless(has, reason='requires {}'.format(modname))
44+
return has, func
45+
46+
47+
has_matplotlib, requires_matplotlib = _importorskip('matplotlib')
48+
has_scipy, requires_scipy = _importorskip('scipy')
49+
has_pydap, requires_pydap = _importorskip('pydap.client')
50+
has_netCDF4, requires_netCDF4 = _importorskip('netCDF4')
51+
has_h5netcdf, requires_h5netcdf = _importorskip('h5netcdf')
52+
has_pynio, requires_pynio = _importorskip('pynio')
53+
has_dask, requires_dask = _importorskip('dask')
54+
has_bottleneck, requires_bottleneck = _importorskip('bottleneck')
55+
has_rasterio, requires_rasterio = _importorskip('rasterio')
56+
has_pathlib, requires_pathlib = _importorskip('pathlib')
57+
58+
# some special cases
59+
has_scipy_or_netCDF4 = has_scipy or has_netCDF4
60+
requires_scipy_or_netCDF4 = unittest.skipUnless(
61+
has_scipy_or_netCDF4, reason='requires scipy or netCDF4')
62+
if not has_pathlib:
63+
has_pathlib, requires_pathlib = _importorskip('pathlib2')
64+
65+
if has_dask:
66+
import dask
67+
dask.set_options(get=dask.get)
12868

12969
try:
13070
_SKIP_FLAKY = not pytest.config.getoption("--run-flaky")
13171
_SKIP_NETWORK_TESTS = not pytest.config.getoption("--run-network-tests")
132-
except ValueError:
72+
except (ValueError, AttributeError):
13373
# Can't get config from pytest, e.g., because xarray is installed instead
13474
# of being run from a development version (and hence conftests.py is not
13575
# available). Don't run flaky tests.

xarray/tests/test_backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ def test_encoding_unlimited_dims(self):
11791179

11801180

11811181
# tests pending h5netcdf fix
1182-
@pytest.mark.xfail
1182+
@unittest.skip
11831183
class H5NetCDFDataTestAutocloseTrue(H5NetCDFDataTest):
11841184
autoclose = True
11851185

0 commit comments

Comments
 (0)