Skip to content

Commit e50ae3f

Browse files
authored
Use pytest.importorskip for sphinx_gallery test (#1525)
Completes code coverage of the sphinx_gallery.py file which had the `except ImportError: figure_rst = None` lines not covered by Continuous Integration tests.
1 parent f7d5526 commit e50ae3f

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

pygmt/sphinx_gallery.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
"""
22
Utilities for using pygmt with sphinx-gallery.
33
"""
4-
try:
5-
from sphinx_gallery.scrapers import figure_rst
6-
except ImportError:
7-
figure_rst = None
84

5+
import sphinx_gallery.scrapers
96
from pygmt.figure import SHOWED_FIGURES
107

118

@@ -32,4 +29,4 @@ def __call__(self, block, block_vars, gallery_conf):
3229
fig = figures.pop(0)
3330
fig.savefig(fname, transparent=True, dpi=200)
3431
image_names.append(fname)
35-
return figure_rst(image_names, gallery_conf["src_dir"])
32+
return sphinx_gallery.scrapers.figure_rst(image_names, gallery_conf["src_dir"])

pygmt/tests/test_sphinx_gallery.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
from tempfile import TemporaryDirectory
66

77
import pytest
8-
9-
try:
10-
import sphinx_gallery
11-
except ImportError:
12-
sphinx_gallery = None
13-
148
from pygmt.figure import SHOWED_FIGURES, Figure
15-
from pygmt.sphinx_gallery import PyGMTScraper
9+
10+
pygmt_sphinx_gallery = pytest.importorskip(
11+
"pygmt.sphinx_gallery", reason="requires sphinx-gallery to be installed"
12+
)
1613

1714

18-
@pytest.mark.skipif(sphinx_gallery is None, reason="requires sphinx-gallery")
1915
def test_pygmtscraper():
2016
"""
2117
Make sure the scraper finds the figures and removes them from the pool.
@@ -30,7 +26,7 @@ def test_pygmtscraper():
3026
fig.show()
3127
assert len(SHOWED_FIGURES) == 1
3228
assert SHOWED_FIGURES[0] is fig
33-
scraper = PyGMTScraper()
29+
scraper = pygmt_sphinx_gallery.PyGMTScraper()
3430
with TemporaryDirectory(dir=os.getcwd()) as tmpdir:
3531
conf = {"src_dir": "meh"}
3632
fname = os.path.join(tmpdir, "meh.png")

0 commit comments

Comments
 (0)