Skip to content

Commit ed814fa

Browse files
Merge pull request #898 from sadielbartholomew/test-skips
Unit test skips based on lack of optional module availability
2 parents 88eb636 + 7c576cd commit ed814fa

File tree

6 files changed

+42
-15
lines changed

6 files changed

+42
-15
lines changed

cf/test/test_Field.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import atexit
22
import datetime
33
import faulthandler
4+
from importlib.util import find_spec
45
import itertools
56
import os
67
import re
@@ -1152,6 +1153,8 @@ def test_Field_insert_dimension(self):
11521153
with self.assertRaises(ValueError):
11531154
f.insert_dimension(1, "qwerty")
11541155

1156+
@unittest.skipUnless(
1157+
find_spec("matplotlib"), "matplotlib required but not installed")
11551158
def test_Field_indices(self):
11561159
f = cf.read(self.filename)[0]
11571160

cf/test/test_RegridOperator.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
import datetime
22
import faulthandler
3+
from importlib.util import find_spec
34
import unittest
45

56
faulthandler.enable() # to debug seg faults and timeouts
67

78
import cf
89

910

11+
# ESMF renamed its Python module to `esmpy` at ESMF version 8.4.0. Allow
12+
# either for now for backwards compatibility.
13+
esmpy_imported = False
14+
# Note: here only need esmpy for cf under-the-hood code, not in test
15+
# directly, so no need to actually import esmpy, just test it is there.
16+
if find_spec("esmpy") or find_spec("ESMF"):
17+
esmpy_imported = True
18+
19+
1020
class RegridOperatorTest(unittest.TestCase):
11-
src = cf.example_field(0)
12-
dst = cf.example_field(1)
13-
r = src.regrids(dst, "linear", return_operator=True)
1421

22+
def setUp(self):
23+
src = cf.example_field(0)
24+
dst = cf.example_field(1)
25+
self.r = src.regrids(dst, "linear", return_operator=True)
26+
27+
@unittest.skipUnless(esmpy_imported, "Requires esmpy/ESMF package.")
1528
def test_RegridOperator_attributes(self):
1629
self.assertEqual(self.r.coord_sys, "spherical")
1730
self.assertEqual(self.r.method, "linear")
@@ -39,6 +52,7 @@ def test_RegridOperator_attributes(self):
3952
self.assertIsNone(self.r.dst_z)
4053
self.assertFalse(self.r.ln_z)
4154

55+
@unittest.skipUnless(esmpy_imported, "Requires esmpy/ESMF package.")
4256
def test_RegridOperator_copy(self):
4357
self.assertIsInstance(self.r.copy(), self.r.__class__)
4458

cf/test/test_read_write.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import inspect
55
import os
66
import shutil
7+
import shutil
78
import subprocess
89
import tempfile
910
import unittest
@@ -642,6 +643,8 @@ def test_read_write_unlimited(self):
642643
self.assertTrue(domain_axes["domainaxis0"].nc_is_unlimited())
643644
self.assertTrue(domain_axes["domainaxis2"].nc_is_unlimited())
644645

646+
@unittest.skipUnless(
647+
shutil.which("ncdump"), "ncdump required - install nco")
645648
def test_read_CDL(self):
646649
subprocess.run(
647650
" ".join(["ncdump", self.filename, ">", tmpfile]),
@@ -703,6 +706,8 @@ def test_read_CDL(self):
703706
with self.assertRaises(Exception):
704707
cf.read("test_read_write.py")
705708

709+
@unittest.skipUnless(
710+
shutil.which("ncdump"), "ncdump required - install nco")
706711
def test_read_cdl_string(self):
707712
"""Test the cf.read 'cdl_string' keyword."""
708713
f = cf.read("example_field_0.nc")[0]
@@ -876,6 +881,8 @@ def test_read_url(self):
876881
f = cf.read(remote)
877882
self.assertEqual(len(f), 1)
878883

884+
@unittest.skipUnless(
885+
shutil.which("ncdump"), "ncdump required - install nco")
879886
def test_read_dataset_type(self):
880887
"""Test the cf.read 'dataset_type' keyword."""
881888
# netCDF dataset

cf/test/test_regrid_featureType.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
atol = 2e-12
3737
rtol = 0
3838

39-
meshloc = {
40-
"face": esmpy.MeshLoc.ELEMENT,
41-
"node": esmpy.MeshLoc.NODE,
42-
}
43-
4439

4540
def esmpy_regrid(coord_sys, method, src, dst, **kwargs):
4641
"""Helper function that regrids one dimension of Field data using
@@ -53,6 +48,11 @@ def esmpy_regrid(coord_sys, method, src, dst, **kwargs):
5348
Regridded numpy masked array.
5449
5550
"""
51+
meshloc = {
52+
"face": esmpy.MeshLoc.ELEMENT,
53+
"node": esmpy.MeshLoc.NODE,
54+
}
55+
5656
esmpy_regrid = cf.regrid.regrid(
5757
coord_sys,
5858
src,

cf/test/test_regrid_mesh.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939
atol = 2e-12
4040
rtol = 0
4141

42-
meshloc = {
43-
"face": esmpy.MeshLoc.ELEMENT,
44-
"node": esmpy.MeshLoc.NODE,
45-
}
46-
4742

4843
def esmpy_regrid(coord_sys, method, src, dst, **kwargs):
4944
"""Helper function that regrids one dimension of Field data using
@@ -56,6 +51,11 @@ def esmpy_regrid(coord_sys, method, src, dst, **kwargs):
5651
Regridded numpy masked array.
5752
5853
"""
54+
meshloc = {
55+
"face": esmpy.MeshLoc.ELEMENT,
56+
"node": esmpy.MeshLoc.NODE,
57+
}
58+
5959
esmpy_regrid = cf.regrid.regrid(
6060
coord_sys,
6161
src,

cf/test/test_style.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import datetime
22
import faulthandler
3+
from importlib.util import find_spec
34
import os
45
import unittest
56

6-
import pycodestyle
7-
87
faulthandler.enable() # to debug seg faults and timeouts
98

109
import cf
@@ -31,7 +30,11 @@ def setUp(self):
3130
for path in non_cf_python_files
3231
]
3332

33+
@unittest.skipUnless(
34+
find_spec("pycodestyle"), "pycodestyle required but not installed")
3435
def test_pep8_compliance(self):
36+
import pycodestyle
37+
3538
pep8_check = pycodestyle.StyleGuide()
3639

3740
# Directories to skip in the recursive walk of the directory:

0 commit comments

Comments
 (0)