Skip to content

Commit a48f9df

Browse files
committed
Improve tests
1 parent cfb69d2 commit a48f9df

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

pygmt/tests/test_tilemap.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
Test Figure.tilemap.
33
"""
44

5+
import importlib
6+
57
import pytest
68
from pygmt import Figure
79

8-
contextily = pytest.importorskip("contextily")
9-
rioxarray = pytest.importorskip("rioxarray")
10+
try:
11+
import contextily
12+
13+
_HAS_CONTEXTILY = True
14+
except ImportError:
15+
_HAS_CONTEXTILY = False
16+
17+
_HAS_RIOXARRAY = bool(importlib.util.find_spec("rioxarray"))
1018

1119

1220
@pytest.mark.mpl_image_compare
21+
@pytest.mark.skipif(not _HAS_CONTEXTILY, reason="contextily is not installed")
1322
def test_tilemap_web_mercator():
1423
"""
1524
Create a tilemap plot in Spherical Mercator projection (EPSG:3857).
@@ -27,6 +36,10 @@ def test_tilemap_web_mercator():
2736

2837
@pytest.mark.benchmark
2938
@pytest.mark.mpl_image_compare
39+
@pytest.mark.skipif(
40+
not (_HAS_CONTEXTILY and _HAS_RIOXARRAY),
41+
reason="contextily and rioxarray are not installed",
42+
)
3043
def test_tilemap_ogc_wgs84():
3144
"""
3245
Create a tilemap plot using longitude/latitude coordinates (OGC:WGS84), centred on
@@ -45,6 +58,10 @@ def test_tilemap_ogc_wgs84():
4558

4659
@pytest.mark.mpl_image_compare
4760
@pytest.mark.parametrize("no_clip", [False, True])
61+
@pytest.mark.skipif(
62+
not (_HAS_CONTEXTILY and _HAS_RIOXARRAY),
63+
reason="contextily and rioxarray are not installed",
64+
)
4865
def test_tilemap_no_clip(no_clip):
4966
"""
5067
Create a tilemap plot clipped to the Southern Hemisphere when no_clip is False, but
@@ -60,3 +77,30 @@ def test_tilemap_no_clip(no_clip):
6077
no_clip=no_clip,
6178
)
6279
return fig
80+
81+
82+
@pytest.mark.skipif(_HAS_CONTEXTILY, reason="contextily is installed.")
83+
def test_tilemap_no_contextily():
84+
"""
85+
Raise an ImportError when contextily is not installed.
86+
"""
87+
fig = Figure()
88+
with pytest.raises(ImportError, match="Package `contextily` is required"):
89+
fig.tilemap(
90+
region=[-20000000.0, 20000000.0, -20000000.0, 20000000.0],
91+
zoom=0,
92+
lonlat=False,
93+
frame="afg",
94+
)
95+
return fig
96+
97+
98+
@pytest.mark.skipif(_HAS_RIOXARRAY, reason="rioxarray is installed.")
99+
def test_tilemap_no_rioxarray():
100+
"""
101+
Raise an ImportError when rioxarray is not installed.
102+
"""
103+
fig = Figure()
104+
with pytest.raises(ImportError, match="Package `rioxarray` is required"):
105+
fig.tilemap(region=[-180.0, 180.0, -90, 90], zoom=0, lonlat=True, frame="afg")
106+
return fig

0 commit comments

Comments
 (0)