Skip to content

Commit 8ccb587

Browse files
authored
Convert tests in unit/util to pytest (#5853)
* pytestify, and test__coord_regular * search and replace * ruff * fixed most failures * renamed result dir * fixed review comments * whoops, incorrectly called assert_original_metadata with request after removing it * corrected assert
1 parent ecd1ca3 commit 8ccb587

23 files changed

+335
-435
lines changed

lib/iris/tests/_shared_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,8 @@ def assert_string(request: pytest.FixtureRequest, string, reference_filename=Non
536536
_check_same(string, reference_path, type_comparison_name="Strings")
537537

538538

539-
def assert_repr(obj, reference_filename):
540-
assert_string(repr(obj), reference_filename)
539+
def assert_repr(request: pytest.FixtureRequest, obj, reference_filename):
540+
assert_string(request, repr(obj), reference_filename)
541541

542542

543543
def _check_same(item, reference_path, type_comparison_name="CML"):

lib/iris/tests/unit/util/test__coord_regular.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,99 +12,92 @@
1212
1313
"""
1414

15-
# import iris tests first so that some things can be initialised before
16-
# importing anything else
17-
import iris.tests as tests # isort:skip
18-
1915
import numpy as np
16+
import pytest
2017

2118
from iris.coords import AuxCoord, DimCoord
2219
from iris.exceptions import CoordinateMultiDimError, CoordinateNotRegularError
2320
from iris.util import is_regular, points_step, regular_step
2421

2522

26-
class Test_is_regular(tests.IrisTest):
23+
class Test_is_regular:
2724
def test_coord_with_regular_step(self):
2825
coord = DimCoord(np.arange(5))
2926
result = is_regular(coord)
30-
self.assertTrue(result)
27+
assert result
3128

3229
def test_coord_with_irregular_step(self):
3330
# Check that a `CoordinateNotRegularError` is captured.
3431
coord = AuxCoord(np.array([2, 5, 1, 4]))
3532
result = is_regular(coord)
36-
self.assertFalse(result)
33+
assert not result
3734

3835
def test_scalar_coord(self):
3936
# Check that a `ValueError` is captured.
4037
coord = DimCoord(5)
4138
result = is_regular(coord)
42-
self.assertFalse(result)
39+
assert not result
4340

4441
def test_coord_with_string_points(self):
4542
# Check that a `TypeError` is captured.
4643
coord = AuxCoord(["a", "b", "c"])
4744
result = is_regular(coord)
48-
self.assertFalse(result)
45+
assert not result
4946

5047

51-
class Test_regular_step(tests.IrisTest):
48+
class Test_regular_step:
5249
def test_basic(self):
5350
dtype = np.float64
5451
points = np.arange(5, dtype=dtype)
5552
coord = DimCoord(points)
5653
expected = np.mean(np.diff(points))
5754
result = regular_step(coord)
58-
self.assertEqual(expected, result)
59-
self.assertEqual(result.dtype, dtype)
55+
assert expected == result
56+
assert result.dtype == dtype
6057

6158
def test_2d_coord(self):
6259
coord = AuxCoord(np.arange(8).reshape(2, 4))
6360
exp_emsg = "Expected 1D coord"
64-
with self.assertRaisesRegex(CoordinateMultiDimError, exp_emsg):
61+
with pytest.raises(CoordinateMultiDimError, match=exp_emsg):
6562
regular_step(coord)
6663

6764
def test_scalar_coord(self):
6865
coord = DimCoord(5)
6966
exp_emsg = "non-scalar coord"
70-
with self.assertRaisesRegex(ValueError, exp_emsg):
67+
with pytest.raises(ValueError, match=exp_emsg):
7168
regular_step(coord)
7269

7370
def test_coord_with_irregular_step(self):
7471
name = "latitude"
7572
coord = AuxCoord(np.array([2, 5, 1, 4]), standard_name=name)
7673
exp_emsg = "{} is not regular".format(name)
77-
with self.assertRaisesRegex(CoordinateNotRegularError, exp_emsg):
74+
with pytest.raises(CoordinateNotRegularError, match=exp_emsg):
7875
regular_step(coord)
7976

8077

81-
class Test_points_step(tests.IrisTest):
78+
class Test_points_step:
8279
def test_regular_points(self):
8380
regular_points = np.arange(5)
8481
exp_avdiff = np.mean(np.diff(regular_points))
8582
result_avdiff, result = points_step(regular_points)
86-
self.assertEqual(exp_avdiff, result_avdiff)
87-
self.assertTrue(result)
83+
assert exp_avdiff == result_avdiff
84+
assert result
8885

8986
def test_irregular_points(self):
9087
irregular_points = np.array([2, 5, 1, 4])
9188
exp_avdiff = np.mean(np.diff(irregular_points))
9289
result_avdiff, result = points_step(irregular_points)
93-
self.assertEqual(exp_avdiff, result_avdiff)
94-
self.assertFalse(result)
90+
assert exp_avdiff == result_avdiff
91+
assert not result
9592

9693
def test_single_point(self):
9794
lone_point = np.array([4])
9895
result_avdiff, result = points_step(lone_point)
99-
self.assertTrue(np.isnan(result_avdiff))
100-
self.assertTrue(result)
96+
assert np.isnan(result_avdiff)
97+
assert result
10198

10299
def test_no_points(self):
103100
no_points = np.array([])
104101
result_avdiff, result = points_step(no_points)
105-
self.assertTrue(np.isnan(result_avdiff))
106-
self.assertTrue(result)
107-
108-
109-
if __name__ == "__main__":
110-
tests.main()
102+
assert np.isnan(result_avdiff)
103+
assert result

lib/iris/tests/unit/util/test__is_circular.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,16 @@
44
# See LICENSE in the root of the repository for full licensing details.
55
"""Test function :func:`iris.util._is_circular`."""
66

7-
# import iris tests first so that some things can be initialised before
8-
# importing anything else
9-
import iris.tests as tests # isort:skip
10-
117
import numpy as np
128

139
from iris.util import _is_circular
1410

1511

16-
class Test(tests.IrisTest):
12+
class Test:
1713
def test_simple(self):
1814
data = np.arange(12) * 30
19-
self.assertTrue(_is_circular(data, 360))
15+
assert _is_circular(data, 360)
2016

2117
def test_negative_diff(self):
2218
data = (np.arange(96) * -3.749998) + 3.56249908e02
23-
self.assertTrue(_is_circular(data, 360))
24-
25-
26-
if __name__ == "__main__":
27-
tests.main()
19+
assert _is_circular(data, 360)

lib/iris/tests/unit/util/test__mask_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
)
3535
@pytest.mark.parametrize("lazy_mask", [False, True], ids=["real", "lazy"])
3636
@pytest.mark.parametrize(
37-
"array, expected", array_choices, ids=["plain-array", "masked-array"]
37+
("array", "expected"), array_choices, ids=["plain-array", "masked-array"]
3838
)
3939
@pytest.mark.parametrize("lazy_array", [False, True], ids=["real", "lazy"])
4040
def test_1d_not_in_place(array, mask, expected, lazy_array, lazy_mask):

lib/iris/tests/unit/util/test__slice_data_with_keys.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
1111
in combination.
1212
1313
"""
14-
15-
# import iris tests first so that some things can be initialised before
16-
# importing anything else
17-
import iris.tests as tests # isort:skip
18-
1914
import numpy as np
15+
import pytest
2016

2117
from iris._lazy_data import as_concrete_data, as_lazy_data
18+
from iris.tests import _shared_utils
2219
from iris.util import _slice_data_with_keys
2320

2421

@@ -77,15 +74,12 @@ def showkeys(keys_list):
7774
msg += "\n]"
7875
return msg
7976

80-
self.assertTrue(
81-
equal,
82-
errmsg.format(showkeys(calls_got), showkeys(expect_call_keys)),
83-
)
77+
assert equal, errmsg.format(showkeys(calls_got), showkeys(expect_call_keys))
8478
if expect_map is not None:
85-
self.assertEqual(dim_map, expect_map)
79+
assert dim_map == expect_map
8680

8781

88-
class Test_indexing(MixinIndexingTest, tests.IrisTest):
82+
class Test_indexing(MixinIndexingTest):
8983
# Check the indexing operations performed for various requested keys.
9084

9185
def test_0d_nokeys(self):
@@ -105,12 +99,12 @@ def test_1d_tuple(self):
10599

106100
def test_fail_1d_2keys(self):
107101
msg = "More slices .* than dimensions"
108-
with self.assertRaisesRegex(IndexError, msg):
102+
with pytest.raises(IndexError, match=msg):
109103
self.check((3,), Index[1, 2])
110104

111105
def test_fail_empty_slice(self):
112106
msg = "Cannot index with zero length slice"
113-
with self.assertRaisesRegex(IndexError, msg):
107+
with pytest.raises(IndexError, match=msg):
114108
self.check((3,), Index[1:1])
115109

116110
def test_2d_tuple(self):
@@ -192,7 +186,7 @@ def test_3d_multiple_tuples(self):
192186
# That's just what it does at present.
193187

194188

195-
class Test_dimensions_mapping(MixinIndexingTest, tests.IrisTest):
189+
class Test_dimensions_mapping(MixinIndexingTest):
196190
# Check the dimensions map returned for various requested keys.
197191

198192
def test_1d_nochange(self):
@@ -236,7 +230,7 @@ def test_3d_losedim1(self):
236230
)
237231

238232

239-
class TestResults(tests.IrisTest):
233+
class TestResults:
240234
# Integration-style test, exercising (mostly) the same cases as above,
241235
# but checking actual results, for both real and lazy array inputs.
242236

@@ -246,10 +240,10 @@ def check(self, real_data, keys, expect_result, expect_map):
246240
real_dim_map, real_result = _slice_data_with_keys(real_data, keys)
247241
lazy_dim_map, lazy_result = _slice_data_with_keys(lazy_data, keys)
248242
lazy_result = as_concrete_data(lazy_result)
249-
self.assertArrayEqual(real_result, expect_result)
250-
self.assertArrayEqual(lazy_result, expect_result)
251-
self.assertEqual(real_dim_map, expect_map)
252-
self.assertEqual(lazy_dim_map, expect_map)
243+
_shared_utils.assert_array_equal(real_result, expect_result)
244+
_shared_utils.assert_array_equal(lazy_result, expect_result)
245+
assert real_dim_map == expect_map
246+
assert lazy_dim_map == expect_map
253247

254248
def test_1d_int(self):
255249
self.check([1, 2, 3, 4], Index[2], [3], {None: None, 0: None})
@@ -262,12 +256,12 @@ def test_1d_tuple(self):
262256

263257
def test_fail_1d_2keys(self):
264258
msg = "More slices .* than dimensions"
265-
with self.assertRaisesRegex(IndexError, msg):
259+
with pytest.raises(IndexError, match=msg):
266260
self.check([1, 2, 3], Index[1, 2], None, None)
267261

268262
def test_fail_empty_slice(self):
269263
msg = "Cannot index with zero length slice"
270-
with self.assertRaisesRegex(IndexError, msg):
264+
with pytest.raises(IndexError, match=msg):
271265
self.check([1, 2, 3], Index[1:1], None, None)
272266

273267
def test_2d_tuple(self):
@@ -418,7 +412,3 @@ def test_3d_multiple_tuples(self):
418412
)
419413
# NOTE: there seem to be an extra initial [:, :, :].
420414
# That's just what it does at present.
421-
422-
423-
if __name__ == "__main__":
424-
tests.main()

0 commit comments

Comments
 (0)