From 3e2409886ffb45e10a665248d0b18c1e188528e9 Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Wed, 26 Nov 2025 14:50:37 +0000 Subject: [PATCH 1/4] converted test_CubeRep --- .../experimental/test_CubeRepresentation.py | 97 +++++++++---------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/lib/iris/tests/integration/experimental/test_CubeRepresentation.py b/lib/iris/tests/integration/experimental/test_CubeRepresentation.py index 0c1386d59f..1175e6aaa3 100644 --- a/lib/iris/tests/integration/experimental/test_CubeRepresentation.py +++ b/lib/iris/tests/integration/experimental/test_CubeRepresentation.py @@ -4,23 +4,22 @@ # See LICENSE in the root of the repository for full licensing details. """Integration tests for cube html representation.""" -# Import iris.tests first so that some things can be initialised before -# importing anything else. -import iris.tests as tests # isort:skip - from html import escape import numpy as np +import pytest from iris.cube import Cube from iris.experimental.representation import CubeRepresentation +from iris.tests import _shared_utils import iris.tests.stock as stock -@tests.skip_data -class TestNoMetadata(tests.IrisTest): +@_shared_utils.skip_data +class TestNoMetadata: # Test the situation where we have a cube with no metadata at all. - def setUp(self): + @pytest.fixture(autouse=True) + def _setup(self): self.shape = (2, 3, 4) self.cube = Cube(np.arange(24).reshape(self.shape)) self.representer = CubeRepresentation(self.cube) @@ -29,26 +28,27 @@ def setUp(self): def test_cube_name(self): expected = "Unknown" # This cube has no metadata. result = self.representer.name - self.assertEqual(expected, result) + assert expected == result def test_cube_units(self): expected = "unknown" # This cube has no metadata. result = self.representer.units - self.assertEqual(expected, result) + assert expected == result def test_dim_names(self): expected = ["--"] * len(self.shape) result = self.representer.names - self.assertEqual(expected, result) + assert expected == result def test_shape(self): result = self.representer.shapes - self.assertEqual(result, self.shape) + assert result == self.shape -@tests.skip_data -class TestMissingMetadata(tests.IrisTest): - def setUp(self): +@_shared_utils.skip_data +class TestMissingMetadata: + @pytest.fixture(autouse=True) + def _setup(self): self.cube = stock.realistic_3d() def test_no_coords(self): @@ -57,10 +57,10 @@ def test_no_coords(self): self.cube.remove_coord(coord) representer = CubeRepresentation(self.cube) result = representer.repr_html().lower() - self.assertNotIn("dimension coordinates", result) - self.assertNotIn("auxiliary coordinates", result) - self.assertNotIn("scalar coordinates", result) - self.assertIn("attributes", result) + assert "dimension coordinates" not in result + assert "auxiliary coordinates" not in result + assert "scalar coordinates" not in result + assert "attributes" in result def test_no_dim_coords(self): dim_coords = [c.name() for c in self.cube.coords(dim_coords=True)] @@ -68,10 +68,10 @@ def test_no_dim_coords(self): self.cube.remove_coord(coord) representer = CubeRepresentation(self.cube) result = representer.repr_html().lower() - self.assertNotIn("dimension coordinates", result) - self.assertIn("auxiliary coordinates", result) - self.assertIn("scalar coordinates", result) - self.assertIn("attributes", result) + assert "dimension coordinates" not in result + assert "auxiliary coordinates" in result + assert "scalar coordinates" in result + assert "attributes" in result def test_no_aux_coords(self): aux_coords = ["forecast_period"] @@ -79,10 +79,10 @@ def test_no_aux_coords(self): self.cube.remove_coord(coord) representer = CubeRepresentation(self.cube) result = representer.repr_html().lower() - self.assertIn("dimension coordinates", result) - self.assertNotIn("auxiliary coordinates", result) - self.assertIn("scalar coordinates", result) - self.assertIn("attributes", result) + assert "dimension coordinates" in result + assert "auxiliary coordinates" not in result + assert "scalar coordinates" in result + assert "attributes" in result def test_no_scalar_coords(self): aux_coords = ["air_pressure"] @@ -90,74 +90,71 @@ def test_no_scalar_coords(self): self.cube.remove_coord(coord) representer = CubeRepresentation(self.cube) result = representer.repr_html().lower() - self.assertIn("dimension coordinates", result) - self.assertIn("auxiliary coordinates", result) - self.assertNotIn("scalar coordinates", result) - self.assertIn("attributes", result) + assert "dimension coordinates" in result + assert "auxiliary coordinates" in result + assert "scalar coordinates" not in result + assert "attributes" in result def test_no_attrs(self): self.cube.attributes = {} representer = CubeRepresentation(self.cube) result = representer.repr_html().lower() - self.assertIn("dimension coordinates", result) - self.assertIn("auxiliary coordinates", result) - self.assertIn("scalar coordinates", result) - self.assertNotIn("attributes", result) + assert "dimension coordinates" in result + assert "auxiliary coordinates" in result + assert "scalar coordinates" in result + assert "attributes" not in result def test_no_cell_methods(self): representer = CubeRepresentation(self.cube) result = representer.repr_html().lower() - self.assertNotIn("cell methods", result) + assert "cell methods" not in result -@tests.skip_data -class TestScalarCube(tests.IrisTest): - def setUp(self): +@_shared_utils.skip_data +class TestScalarCube: + @pytest.fixture(autouse=True) + def _setup(self): self.cube = stock.realistic_3d()[0, 0, 0] self.representer = CubeRepresentation(self.cube) self.representer.repr_html() def test_identfication(self): # Is this scalar cube accurately identified? - self.assertTrue(self.representer.scalar_cube) + assert self.representer.scalar_cube def test_header__name(self): header = self.representer._make_header() expected_name = escape(self.cube.name().title().replace("_", " ")) - self.assertIn(expected_name, header) + assert expected_name in header def test_header__units(self): header = self.representer._make_header() expected_units = escape(self.cube.units.symbol) - self.assertIn(expected_units, header) + assert expected_units in header def test_header__scalar_str(self): # Check that 'scalar cube' is placed in the header. header = self.representer._make_header() expected_str = "(scalar cube)" - self.assertIn(expected_str, header) + assert expected_str in header def test_content__scalars(self): # Check an element "Scalar coordinates" is present in the main content. content = self.representer._make_content() expected_str = "Scalar coordinates" - self.assertIn(expected_str, content) + assert expected_str in content def test_content__specific_scalar_coord(self): # Check a specific scalar coord is present in the main content. content = self.representer._make_content() expected_coord = self.cube.coords()[0] expected_coord_name = escape(expected_coord.name()) - self.assertIn(expected_coord_name, content) + assert expected_coord_name in content expected_coord_val = escape(str(expected_coord.points[0])) - self.assertIn(expected_coord_val, content) + assert expected_coord_val in content def test_content__attributes(self): # Check an element "attributes" is present in the main content. content = self.representer._make_content() expected_str = "Attributes" - self.assertIn(expected_str, content) - - -if __name__ == "__main__": - tests.main() + assert expected_str in content From 831be1f3efb86f9fc0864fe90a4763349f90162b Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Wed, 26 Nov 2025 15:01:03 +0000 Subject: [PATCH 2/4] converted test_regrid_ProjUns --- .../test_regrid_ProjectedUnstructured.py | 55 +++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/lib/iris/tests/integration/experimental/test_regrid_ProjectedUnstructured.py b/lib/iris/tests/integration/experimental/test_regrid_ProjectedUnstructured.py index d158b09442..85f10a0c87 100644 --- a/lib/iris/tests/integration/experimental/test_regrid_ProjectedUnstructured.py +++ b/lib/iris/tests/integration/experimental/test_regrid_ProjectedUnstructured.py @@ -4,15 +4,10 @@ # See LICENSE in the root of the repository for full licensing details. """Integration tests for experimental regridding.""" -# Import iris.tests first so that some things can be initialised before -# importing anything else. -import iris.tests as tests # isort:skip - -import unittest - import cartopy.crs as ccrs from cf_units import Unit import numpy as np +import pytest import iris import iris.aux_factory @@ -21,13 +16,15 @@ ProjectedUnstructuredLinear, ProjectedUnstructuredNearest, ) +from iris.tests import _shared_utils from iris.tests.stock import global_pp -@tests.skip_data -class TestProjectedUnstructured(tests.IrisTest): - def setUp(self): - path = tests.get_data_path( +@_shared_utils.skip_data +class TestProjectedUnstructured: + @pytest.fixture(autouse=True) + def _setup(self): + path = _shared_utils.get_data_path( ("NetCDF", "unstructured_grid", "theta_nodal_not_ugrid.nc") ) self.src = iris.load_cube(path, "Potential Temperature") @@ -41,37 +38,37 @@ def setUp(self): def test_nearest(self): res = self.src.regrid(self.global_grid, ProjectedUnstructuredNearest()) - self.assertArrayShapeStats( + _shared_utils.assert_array_shape_stats( res, (1, 6, 73, 96), 315.8913582, 11.00063922733, rtol=1e-8 ) - self.assertArrayShapeStats( + _shared_utils.assert_array_shape_stats( res[:, 0], (1, 73, 96), 299.99993826, 3.9226378869e-5 ) def test_nearest_sinusoidal(self): crs = ccrs.Sinusoidal() res = self.src.regrid(self.global_grid, ProjectedUnstructuredNearest(crs)) - self.assertArrayShapeStats( + _shared_utils.assert_array_shape_stats( res, (1, 6, 73, 96), 315.891358296, 11.000639227, rtol=1e-8 ) - self.assertArrayShapeStats( + _shared_utils.assert_array_shape_stats( res[:, 0], (1, 73, 96), 299.99993826, 3.9223839688e-5 ) - @unittest.skip("Deprecated API and provenance of reference numbers unknown.") + @pytest.mark.skip("Deprecated API and provenance of reference numbers unknown.") def test_nearest_gnomonic_uk_domain(self): crs = ccrs.Gnomonic(central_latitude=60.0) uk_grid = self.global_grid.intersection(longitude=(-20, 20), latitude=(40, 80)) res = self.src.regrid(uk_grid, ProjectedUnstructuredNearest(crs)) - self.assertArrayShapeStats( + _shared_utils.assert_array_shape_stats( res, (1, 6, 17, 11), 315.8854720963427, 11.000539210625737, rtol=1e-8, ) - self.assertArrayShapeStats( + _shared_utils.assert_array_shape_stats( res[:, 0], (1, 17, 11), 299.9999985207442, @@ -84,7 +81,9 @@ def test_nearest_gnomonic_uk_domain(self): [318.92881733, 318.92881733, 318.92881733], ] ) - self.assertArrayAlmostEqual(expected_subset, res.data[0, 3, 5:8, 4:7].data) + _shared_utils.assert_array_almost_equal( + expected_subset, res.data[0, 3, 5:8, 4:7].data + ) def test_nearest_aux_factories(self): src = self.src @@ -121,20 +120,22 @@ def test_nearest_aux_factories(self): ) res = src.regrid(self.global_grid, ProjectedUnstructuredNearest()) - self.assertArrayShapeStats( + _shared_utils.assert_array_shape_stats( res, (1, 6, 73, 96), 315.8913582, 11.000639227334, rtol=1e-8 ) - self.assertArrayShapeStats( + _shared_utils.assert_array_shape_stats( res[:, 0], (1, 73, 96), 299.99993826, 3.9226378869e-5 ) - self.assertEqual(res.coord("altitude").shape, (6, 73, 96)) + assert res.coord("altitude").shape == (6, 73, 96) def test_linear_sinusoidal(self): res = self.src.regrid(self.global_grid, ProjectedUnstructuredLinear()) - self.assertArrayShapeStats( + _shared_utils.assert_array_shape_stats( res, (1, 6, 73, 96), 315.8914839, 11.0006338412, rtol=1e-8 ) - self.assertArrayShapeStats(res[:, 0], (1, 73, 96), 299.99993826, 3.775024069e-5) + _shared_utils.assert_array_shape_stats( + res[:, 0], (1, 73, 96), 299.99993826, 3.775024069e-5 + ) expected_subset = np.array( [ [299.999987, 299.999996, 299.999999], @@ -142,8 +143,6 @@ def test_linear_sinusoidal(self): [299.999973, 299.999977, 299.999982], ] ) - self.assertArrayAlmostEqual(expected_subset, res.data[0, 0, 20:23, 40:43].data) - - -if __name__ == "__main__": - tests.main() + _shared_utils.assert_array_almost_equal( + expected_subset, res.data[0, 0, 20:23, 40:43].data + ) From aed8a11500a877de2744d21faeaf693424271091 Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Wed, 26 Nov 2025 15:04:18 +0000 Subject: [PATCH 3/4] converted geovista/test_cube_to_poly --- .../experimental/geovista/test_cube_to_poly.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/iris/tests/integration/experimental/geovista/test_cube_to_poly.py b/lib/iris/tests/integration/experimental/geovista/test_cube_to_poly.py index 26ea8c48ea..f250973235 100644 --- a/lib/iris/tests/integration/experimental/geovista/test_cube_to_poly.py +++ b/lib/iris/tests/integration/experimental/geovista/test_cube_to_poly.py @@ -4,15 +4,13 @@ # See LICENSE in the root of the repository for full licensing details. """Integration tests for the `iris.experimental.geovista.cube_to_polydata` function.""" -import numpy as np - from iris import load_cube from iris.experimental.geovista import cube_to_polydata -from iris.tests import get_data_path +from iris.tests import _shared_utils def test_integration_2d(): - file_path = get_data_path( + file_path = _shared_utils.get_data_path( [ "NetCDF", "ORCA2", @@ -26,11 +24,13 @@ def test_integration_2d(): # This is a known good output, we have plotted the result and checked it. assert polydata.GetNumberOfCells() == 26640 assert polydata.GetNumberOfPoints() == 26969 - np.testing.assert_array_equal(cube[0, 1, :].data.flatten(), polydata.active_scalars) + _shared_utils.assert_array_equal( + cube[0, 1, :].data.flatten(), polydata.active_scalars + ) def test_integration_1d(): - file_path = get_data_path( + file_path = _shared_utils.get_data_path( [ "NetCDF", "global", @@ -44,11 +44,11 @@ def test_integration_1d(): # This is a known good output, we have plotted the result and checked it. assert polydata.GetNumberOfCells() == 51200 assert polydata.GetNumberOfPoints() == 51681 - np.testing.assert_array_equal(cube[0, :].data.flatten(), polydata.active_scalars) + _shared_utils.assert_array_equal(cube[0, :].data.flatten(), polydata.active_scalars) def test_integration_mesh(): - file_path = get_data_path( + file_path = _shared_utils.get_data_path( [ "NetCDF", "unstructured_grid", @@ -62,4 +62,4 @@ def test_integration_mesh(): # This is a known good output, we have plotted the result and checked it. assert polydata.GetNumberOfCells() == 864 assert polydata.GetNumberOfPoints() == 866 - np.testing.assert_array_equal(polydata.active_scalars, cube[0, :].data) + _shared_utils.assert_array_equal(polydata.active_scalars, cube[0, :].data) From e65307ff8a537a8c5ef5fa7aa0dad22198d4ee7e Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Wed, 26 Nov 2025 15:06:37 +0000 Subject: [PATCH 4/4] converted geovista/test_extract_uns_reg --- .../experimental/geovista/test_extract_unstructured_region.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/iris/tests/integration/experimental/geovista/test_extract_unstructured_region.py b/lib/iris/tests/integration/experimental/geovista/test_extract_unstructured_region.py index 7343809cdc..ca637bef91 100644 --- a/lib/iris/tests/integration/experimental/geovista/test_extract_unstructured_region.py +++ b/lib/iris/tests/integration/experimental/geovista/test_extract_unstructured_region.py @@ -8,11 +8,11 @@ from iris import load_cube from iris.experimental.geovista import cube_to_polydata, extract_unstructured_region -from iris.tests import get_data_path +from iris.tests import _shared_utils def test_face_region_extraction(): - file_path = get_data_path( + file_path = _shared_utils.get_data_path( [ "NetCDF", "unstructured_grid",