File tree 2 files changed +58
-1
lines changed
2 files changed +58
-1
lines changed Original file line number Diff line number Diff line change 1
1
"""
2
2
Helper functions for testing.
3
3
"""
4
+ import importlib
4
5
import inspect
5
6
import os
6
7
import string
7
8
9
+ import pytest
8
10
from pygmt .exceptions import GMTImageComparisonFailure
9
11
from pygmt .io import load_dataarray
10
12
from pygmt .src import which
@@ -237,3 +239,42 @@ def load_static_earth_relief():
237
239
"""
238
240
fname = which ("@static_earth_relief.nc" , download = "c" )
239
241
return load_dataarray (fname )
242
+
243
+
244
+ def skip_if_no (package ):
245
+ """
246
+ Generic function to help skip tests when required packages are not present
247
+ on the testing system.
248
+
249
+ This function returns a pytest mark with a skip condition that will be
250
+ evaluated during test collection. An attempt will be made to import the
251
+ specified ``package``.
252
+
253
+ The mark can be used as either a decorator for a test class or to be
254
+ applied to parameters in pytest.mark.parametrize calls or parametrized
255
+ fixtures. Use pytest.importorskip if an imported moduled is later needed
256
+ or for test functions.
257
+
258
+ If the import is unsuccessful, then the test function (or test case when
259
+ used in conjunction with parametrization) will be skipped.
260
+
261
+ Adapted from
262
+ https://github.com/pandas-dev/pandas/blob/v2.1.4/pandas/util/_test_decorators.py#L121
263
+
264
+ Parameters
265
+ ----------
266
+ package : str
267
+ The name of the required package.
268
+
269
+ Returns
270
+ -------
271
+ pytest.MarkDecorator
272
+ A pytest.mark.skipif to use as either a test decorator or a
273
+ parametrization mark.
274
+ """
275
+ try :
276
+ _ = importlib .import_module (name = package )
277
+ has_package = True
278
+ except ImportError :
279
+ has_package = False
280
+ return pytest .mark .skipif (not has_package , reason = f"Could not import '{ package } '" )
Original file line number Diff line number Diff line change 15
15
kwargs_to_strings ,
16
16
unique_name ,
17
17
)
18
- from pygmt .helpers .testing import load_static_earth_relief
18
+ from pygmt .helpers .testing import load_static_earth_relief , skip_if_no
19
19
20
20
21
21
def test_load_static_earth_relief ():
@@ -147,3 +147,19 @@ def test_args_in_kwargs():
147
147
# Failing list of arguments
148
148
failing_args = ["D" , "E" , "F" ]
149
149
assert not args_in_kwargs (args = failing_args , kwargs = kwargs )
150
+
151
+
152
+ def test_skip_if_no ():
153
+ """
154
+ Test that the skip_if_no helper testing function returns a
155
+ pytest.mask.skipif mark decorator.
156
+ """
157
+ # Check pytest.mark with a dependency that can be imported
158
+ mark_decorator = skip_if_no (package = "numpy" )
159
+ assert mark_decorator .args [0 ] is False
160
+
161
+ # Check pytest.mark with a dependency that cannot be imported
162
+ mark_decorator = skip_if_no (package = "nullpackage" )
163
+ assert mark_decorator .args [0 ] is True
164
+ assert mark_decorator .kwargs ["reason" ] == "Could not import 'nullpackage'"
165
+ assert mark_decorator .markname == "skipif"
You can’t perform that action at this time.
0 commit comments