Skip to content

Commit da6a8c5

Browse files
authored
Merge pull request SciTools#2282 from greglucas/tst-skip-scipy
TST: Skip scipy tests if scipy isn't installed
2 parents 4e0fefb + 8c308e8 commit da6a8c5

12 files changed

+103
-9
lines changed

lib/cartopy/tests/conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
# This file is part of Cartopy and is released under the LGPL license.
44
# See COPYING and COPYING.LESSER in the root of the repository for full
55
# licensing details.
6+
import pytest
7+
8+
9+
_HAS_PYKDTREE = True
10+
_HAS_SCIPY = True
11+
12+
try:
13+
import pykdtree # noqa: F401
14+
except ImportError:
15+
_HAS_PYKDTREE = False
16+
17+
try:
18+
import scipy # noqa: F401
19+
except ImportError:
20+
_HAS_SCIPY = False
21+
22+
requires_scipy = pytest.mark.skipif(
23+
not _HAS_SCIPY,
24+
reason="scipy is required")
25+
requires_pykdtree = pytest.mark.skipif(
26+
not _HAS_PYKDTREE,
27+
reason="pykdtree is required")
28+
_HAS_PYKDTREE_OR_SCIPY = _HAS_PYKDTREE or _HAS_SCIPY
629

730

831
def pytest_configure(config):

lib/cartopy/tests/io/test_ogc_clients.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
import pytest
2222

2323
import cartopy.crs as ccrs
24+
from cartopy.tests.conftest import _HAS_PYKDTREE_OR_SCIPY
25+
26+
27+
if not _HAS_PYKDTREE_OR_SCIPY:
28+
pytest.skip('pykdtree or scipy is required', allow_module_level=True)
29+
30+
2431
import cartopy.io.ogc_clients as ogc
2532
from cartopy.io.ogc_clients import _OWSLIB_AVAILABLE
2633

lib/cartopy/tests/mpl/test_caching.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import pytest
1818

1919
import cartopy.crs as ccrs
20-
from cartopy.io.ogc_clients import _OWSLIB_AVAILABLE, WMTSRasterSource
20+
from cartopy.tests.conftest import _HAS_PYKDTREE_OR_SCIPY
21+
22+
23+
if _HAS_PYKDTREE_OR_SCIPY:
24+
from cartopy.io.ogc_clients import _OWSLIB_AVAILABLE, WMTSRasterSource
25+
2126
import cartopy.io.shapereader
2227
from cartopy.mpl import _MPL_38
2328
from cartopy.mpl.feature_artist import FeatureArtist
@@ -149,7 +154,8 @@ def test_contourf_transform_path_counting():
149154

150155
@pytest.mark.filterwarnings("ignore:TileMatrixLimits")
151156
@pytest.mark.network
152-
@pytest.mark.skipif(not _OWSLIB_AVAILABLE, reason='OWSLib is unavailable.')
157+
@pytest.mark.skipif(not _HAS_PYKDTREE_OR_SCIPY or not _OWSLIB_AVAILABLE,
158+
reason='OWSLib and at least one of pykdtree or scipy is required')
153159
def test_wmts_tile_caching():
154160
image_cache = WMTSRasterSource._shared_image_cache
155161
image_cache.clear()

lib/cartopy/tests/mpl/test_contour.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
import numpy as np
99
from numpy.testing import assert_array_almost_equal
1010
import pytest
11-
from scipy.interpolate import NearestNDInterpolator
12-
from scipy.signal import convolve2d
1311

1412
import cartopy.crs as ccrs
13+
from cartopy.tests.conftest import requires_scipy
1514

1615

1716
def test_contour_plot_bounds():
@@ -72,8 +71,12 @@ def test_plot_after_contour_doesnt_shrink(func):
7271
assert_array_almost_equal(ax.get_extent(), expected)
7372

7473

74+
@requires_scipy
7575
def test_contour_linear_ring():
7676
"""Test contourf with a section that only has 3 points."""
77+
from scipy.interpolate import NearestNDInterpolator
78+
from scipy.signal import convolve2d
79+
7780
ax = plt.axes([0.01, 0.05, 0.898, 0.85], projection=ccrs.Mercator(),
7881
aspect='equal')
7982
ax.set_extent([-99.6, -89.0, 39.8, 45.5])

lib/cartopy/tests/mpl/test_examples.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
import cartopy.crs as ccrs
1111
from cartopy.mpl import _MPL_38
12+
from cartopy.tests.conftest import _HAS_PYKDTREE_OR_SCIPY
13+
14+
15+
if not _HAS_PYKDTREE_OR_SCIPY:
16+
pytest.skip('pykdtree or scipy is required', allow_module_level=True)
1217

1318

1419
@pytest.mark.natural_earth

lib/cartopy/tests/mpl/test_features.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
import cartopy.crs as ccrs
1212
import cartopy.feature as cfeature
13-
from cartopy.io.ogc_clients import _OWSLIB_AVAILABLE
13+
from cartopy.tests.conftest import _HAS_PYKDTREE_OR_SCIPY
14+
15+
16+
if _HAS_PYKDTREE_OR_SCIPY:
17+
from cartopy.io.ogc_clients import _OWSLIB_AVAILABLE
1418

1519

1620
@pytest.mark.filterwarnings("ignore:Downloading")
@@ -43,6 +47,7 @@ def test_natural_earth_custom():
4347
return ax.figure
4448

4549

50+
@pytest.mark.skipif(not _HAS_PYKDTREE_OR_SCIPY, reason='pykdtree or scipy is required')
4651
@pytest.mark.mpl_image_compare(filename='gshhs_coastlines.png', tolerance=0.95)
4752
def test_gshhs():
4853
ax = plt.axes(projection=ccrs.Mollweide())
@@ -58,7 +63,8 @@ def test_gshhs():
5863

5964

6065
@pytest.mark.network
61-
@pytest.mark.skipif(not _OWSLIB_AVAILABLE, reason='OWSLib is unavailable.')
66+
@pytest.mark.skipif(not _HAS_PYKDTREE_OR_SCIPY or not _OWSLIB_AVAILABLE,
67+
reason='OWSLib and at least one of pykdtree or scipy is required')
6268
@pytest.mark.xfail(raises=ParseError,
6369
reason="Bad XML returned from the URL")
6470
@pytest.mark.mpl_image_compare(filename='wfs.png')
@@ -73,7 +79,8 @@ def test_wfs():
7379

7480

7581
@pytest.mark.network
76-
@pytest.mark.skipif(not _OWSLIB_AVAILABLE, reason='OWSLib is unavailable.')
82+
@pytest.mark.skipif(not _HAS_PYKDTREE_OR_SCIPY or not _OWSLIB_AVAILABLE,
83+
reason='OWSLib and at least one of pykdtree or scipy is required')
7784
@pytest.mark.xfail(raises=ParseError,
7885
reason="Bad XML returned from the URL")
7986
@pytest.mark.mpl_image_compare(filename='wfs_france.png')

lib/cartopy/tests/mpl/test_images.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
from cartopy import config
1717
import cartopy.crs as ccrs
1818
import cartopy.io.img_tiles as cimgt
19+
from cartopy.tests.conftest import _HAS_PYKDTREE_OR_SCIPY
1920
import cartopy.tests.test_img_tiles as ctest_tiles
2021

2122

23+
if not _HAS_PYKDTREE_OR_SCIPY:
24+
pytest.skip('pykdtree or scipy is required', allow_module_level=True)
25+
26+
2227
NATURAL_EARTH_IMG = (config["repo_data_dir"] / 'raster' / 'natural_earth'
2328
/ '50-natural-earth-1-downsampled.png')
2429
REGIONAL_IMG = (config['repo_data_dir'] / 'raster' / 'sample'

lib/cartopy/tests/mpl/test_img_transform.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
from cartopy import config
1616
import cartopy.crs as ccrs
17+
from cartopy.tests.conftest import _HAS_PYKDTREE_OR_SCIPY
18+
19+
20+
if not _HAS_PYKDTREE_OR_SCIPY:
21+
pytest.skip('pykdtree or scipy is required', allow_module_level=True)
22+
1723
import cartopy.img_transform as im_trans
1824

1925

lib/cartopy/tests/mpl/test_mpl_integration.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import cartopy.crs as ccrs
1515
from cartopy.mpl import _MPL_38
16+
from cartopy.tests.conftest import requires_scipy
1617

1718

1819
@pytest.mark.natural_earth
@@ -817,6 +818,7 @@ def test_quiver_rotated_pole():
817818
return fig
818819

819820

821+
@requires_scipy
820822
@pytest.mark.natural_earth
821823
@pytest.mark.mpl_image_compare(filename='quiver_regrid.png')
822824
def test_quiver_regrid():
@@ -836,6 +838,7 @@ def test_quiver_regrid():
836838
return fig
837839

838840

841+
@requires_scipy
839842
@pytest.mark.natural_earth
840843
@pytest.mark.mpl_image_compare(filename='quiver_regrid_with_extent.png',
841844
tolerance=0.54)
@@ -857,6 +860,7 @@ def test_quiver_regrid_with_extent():
857860
return fig
858861

859862

863+
@requires_scipy
860864
@pytest.mark.natural_earth
861865
@pytest.mark.mpl_image_compare(filename='barbs_plate_carree.png')
862866
def test_barbs():
@@ -880,6 +884,7 @@ def test_barbs():
880884
return fig
881885

882886

887+
@requires_scipy
883888
@pytest.mark.natural_earth
884889
@pytest.mark.mpl_image_compare(filename='barbs_regrid.png')
885890
def test_barbs_regrid():
@@ -899,6 +904,7 @@ def test_barbs_regrid():
899904
return fig
900905

901906

907+
@requires_scipy
902908
@pytest.mark.natural_earth
903909
@pytest.mark.mpl_image_compare(filename='barbs_regrid_with_extent.png',
904910
tolerance=0.54)
@@ -955,6 +961,7 @@ def test_barbs_1d_transformed():
955961
return fig
956962

957963

964+
@requires_scipy
958965
@pytest.mark.natural_earth
959966
@pytest.mark.mpl_image_compare(filename='streamplot.png', style='mpl20')
960967
def test_streamplot():

lib/cartopy/tests/mpl/test_web_services.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
import matplotlib.pyplot as plt
88
import pytest
99

10+
from cartopy.tests.conftest import _HAS_PYKDTREE_OR_SCIPY
11+
12+
13+
if not _HAS_PYKDTREE_OR_SCIPY:
14+
pytest.skip('pykdtree or scipy is required', allow_module_level=True)
15+
16+
1017
import cartopy.crs as ccrs
1118
from cartopy.io.ogc_clients import _OWSLIB_AVAILABLE
1219

0 commit comments

Comments
 (0)