diff --git a/lib/iris/tests/_shared_utils.py b/lib/iris/tests/_shared_utils.py index d483b0c0bc..91a2556cf5 100644 --- a/lib/iris/tests/_shared_utils.py +++ b/lib/iris/tests/_shared_utils.py @@ -926,25 +926,25 @@ class MyGeoTiffTests(test.IrisTest): skip_sample_data = pytest.mark.skipif( not SAMPLE_DATA_AVAILABLE, - ('Test(s) require "iris-sample-data", ' "which is not available."), + reason=('Test(s) require "iris-sample-data", ' "which is not available."), ) skip_nc_time_axis = pytest.mark.skipif( not NC_TIME_AXIS_AVAILABLE, - 'Test(s) require "nc_time_axis", which is not available.', + reason='Test(s) require "nc_time_axis", which is not available.', ) skip_inet = pytest.mark.skipif( not INET_AVAILABLE, - ('Test(s) require an "internet connection", ' "which is not available."), + reason=('Test(s) require an "internet connection", ' "which is not available."), ) skip_stratify = pytest.mark.skipif( not STRATIFY_AVAILABLE, - 'Test(s) require "python-stratify", which is not available.', + reason='Test(s) require "python-stratify", which is not available.', ) diff --git a/lib/iris/tests/integration/plot/test_animate.py b/lib/iris/tests/integration/plot/test_animate.py index 4afee0c463..9008a53b84 100644 --- a/lib/iris/tests/integration/plot/test_animate.py +++ b/lib/iris/tests/integration/plot/test_animate.py @@ -3,25 +3,22 @@ # This file is part of Iris and is released under the BSD license. # See LICENSE in the root of the repository for full licensing details. """Integration tests for :func:`iris.plot.animate`.""" - -# import iris tests first so that some things can be initialised before -# importing anything else -import iris.tests as tests # isort:skip - import numpy as np +import pytest import iris from iris.coord_systems import GeogCS +from iris.tests import _shared_utils # Run tests in no graphics mode if matplotlib is not available. -if tests.MPL_AVAILABLE: +if _shared_utils.MPL_AVAILABLE: import iris.plot as iplt -@tests.skip_plot -class IntegrationTest(tests.GraphicsTest): - def setUp(self): - super().setUp() +@_shared_utils.skip_plot +class IntegrationTest(_shared_utils.GraphicsTest): + @pytest.fixture(autouse=True) + def _setup(self): cube = iris.cube.Cube(np.arange(36, dtype=np.int32).reshape((3, 3, 4))) cs = GeogCS(6371229) @@ -68,7 +65,3 @@ def test_cube_animation(self): for anim, d in zip(ani, data): anim._draw_next_frame(d, blit=False) self.check_graphic() - - -if __name__ == "__main__": - tests.main() diff --git a/lib/iris/tests/integration/plot/test_colorbar.py b/lib/iris/tests/integration/plot/test_colorbar.py index 4a3fd27a80..a1d498f200 100644 --- a/lib/iris/tests/integration/plot/test_colorbar.py +++ b/lib/iris/tests/integration/plot/test_colorbar.py @@ -6,27 +6,24 @@ :func:`matplotlib.pyplot.colorbar`. """ - -# import iris tests first so that some things can be initialised before -# importing anything else -import iris.tests as tests # isort:skip - import numpy as np +import pytest from iris.coords import AuxCoord +from iris.tests import _shared_utils import iris.tests.stock # Run tests in no graphics mode if matplotlib is not available. -if tests.MPL_AVAILABLE: +if _shared_utils.MPL_AVAILABLE: import matplotlib.pyplot as plt from iris.plot import contour, contourf, pcolor, pcolormesh, points, scatter -@tests.skip_plot -class TestColorBarCreation(tests.GraphicsTest): - def setUp(self): - super().setUp() +@_shared_utils.skip_plot +class TestColorBarCreation(_shared_utils.GraphicsTest): + @pytest.fixture(autouse=True) + def _setup(self): self.draw_functions = (contour, contourf, pcolormesh, pcolor) self.cube = iris.tests.stock.lat_lon_cube() self.cube.coord("longitude").guess_bounds() @@ -46,49 +43,37 @@ def test_common_draw_functions(self): for draw_function in self.draw_functions: mappable = draw_function(self.cube) cbar = plt.colorbar() - self.assertIs( - cbar.mappable, - mappable, - msg="Problem with draw function iris.plot.{}".format( - draw_function.__name__ - ), - ) + assert ( + cbar.mappable is mappable + ), "Problem with draw function iris.plot.{}".format(draw_function.__name__) def test_common_draw_functions_specified_mappable(self): for draw_function in self.draw_functions: mappable_initial = draw_function(self.cube, cmap="cool") _ = draw_function(self.cube) cbar = plt.colorbar(mappable_initial) - self.assertIs( - cbar.mappable, - mappable_initial, - msg="Problem with draw function iris.plot.{}".format( - draw_function.__name__ - ), - ) + assert ( + cbar.mappable is mappable_initial + ), "Problem with draw function iris.plot.{}".format(draw_function.__name__) def test_points_with_c_kwarg(self): mappable = points(self.cube, c=self.cube.data) cbar = plt.colorbar() - self.assertIs(cbar.mappable, mappable) + assert cbar.mappable is mappable def test_points_with_c_kwarg_specified_mappable(self): mappable_initial = points(self.cube, c=self.cube.data, cmap="cool") _ = points(self.cube, c=self.cube.data) cbar = plt.colorbar(mappable_initial) - self.assertIs(cbar.mappable, mappable_initial) + assert cbar.mappable is mappable_initial def test_scatter_with_c_kwarg(self): mappable = scatter(self.traj_lon, self.traj_lat, c=self.traj_lon.points) cbar = plt.colorbar() - self.assertIs(cbar.mappable, mappable) + assert cbar.mappable is mappable def test_scatter_with_c_kwarg_specified_mappable(self): mappable_initial = scatter(self.traj_lon, self.traj_lat, c=self.traj_lon.points) _ = scatter(self.traj_lon, self.traj_lat, c=self.traj_lon.points, cmap="cool") cbar = plt.colorbar(mappable_initial) - self.assertIs(cbar.mappable, mappable_initial) - - -if __name__ == "__main__": - tests.main() + assert cbar.mappable is mappable_initial diff --git a/lib/iris/tests/integration/plot/test_netcdftime.py b/lib/iris/tests/integration/plot/test_netcdftime.py index 750de9fdf3..9c1d4cf5e6 100644 --- a/lib/iris/tests/integration/plot/test_netcdftime.py +++ b/lib/iris/tests/integration/plot/test_netcdftime.py @@ -3,25 +3,21 @@ # This file is part of Iris and is released under the BSD license. # See LICENSE in the root of the repository for full licensing details. """Test plot of time coord with non-standard calendar.""" - -# import iris tests first so that some things can be initialised before -# importing anything else -import iris.tests as tests # isort:skip - from cf_units import Unit import cftime import numpy as np from iris.coords import AuxCoord +from iris.tests import _shared_utils # Run tests in no graphics mode if matplotlib is not available. -if tests.MPL_AVAILABLE: +if _shared_utils.MPL_AVAILABLE: import iris.plot as iplt -@tests.skip_nc_time_axis -@tests.skip_plot -class Test(tests.GraphicsTest): +@_shared_utils.skip_nc_time_axis +@_shared_utils.skip_plot +class Test(_shared_utils.GraphicsTest): def test_360_day_calendar(self): n = 360 calendar = "360_day" @@ -44,8 +40,4 @@ def test_360_day_calendar(self): expected_ydata = times (line1,) = iplt.plot(time_coord) result_ydata = line1.get_ydata() - self.assertArrayEqual(expected_ydata, result_ydata) - - -if __name__ == "__main__": - tests.main() + _shared_utils.assert_array_equal(expected_ydata, result_ydata) diff --git a/lib/iris/tests/integration/plot/test_nzdateline.py b/lib/iris/tests/integration/plot/test_nzdateline.py index cb119f5b27..6e2241863e 100644 --- a/lib/iris/tests/integration/plot/test_nzdateline.py +++ b/lib/iris/tests/integration/plot/test_nzdateline.py @@ -4,33 +4,25 @@ # See LICENSE in the root of the repository for full licensing details. """Test set up of limited area map extents which bridge the date line.""" -# import iris tests first so that some things can be initialised before -# importing anything else -import iris.tests as tests # isort:skip - import iris +from iris.tests import _shared_utils # Run tests in no graphics mode if matplotlib is not available. -if tests.MPL_AVAILABLE: +if _shared_utils.MPL_AVAILABLE: import matplotlib.pyplot as plt from iris.plot import pcolormesh -@tests.skip_plot -@tests.skip_data -class TestExtent(tests.IrisTest): +@_shared_utils.skip_plot +@_shared_utils.skip_data +class TestExtent: def test_dateline(self): - dpath = tests.get_data_path(["PP", "nzgust.pp"]) + dpath = _shared_utils.get_data_path(["PP", "nzgust.pp"]) cube = iris.load_cube(dpath) pcolormesh(cube) # Ensure that the limited area expected for NZ is set. # This is set in longitudes with the datum set to the # International Date Line. - self.assertTrue( - -10 < plt.gca().get_xlim()[0] < -5 and 5 < plt.gca().get_xlim()[1] < 10 - ) - - -if __name__ == "__main__": - tests.main() + assert -10 < plt.gca().get_xlim()[0] < -5 + assert 5 < plt.gca().get_xlim()[1] < 10 diff --git a/lib/iris/tests/integration/plot/test_plot_2d_coords.py b/lib/iris/tests/integration/plot/test_plot_2d_coords.py index 43cd051f46..1b3c850bbe 100644 --- a/lib/iris/tests/integration/plot/test_plot_2d_coords.py +++ b/lib/iris/tests/integration/plot/test_plot_2d_coords.py @@ -4,9 +4,6 @@ # See LICENSE in the root of the repository for full licensing details. """Test plots with two dimensional coordinates.""" -# import iris tests first so that some things can be initialised before -# importing anything else -import iris.tests as tests # isort:skip import cartopy.crs as ccrs import matplotlib.pyplot as plt @@ -16,22 +13,23 @@ from iris.analysis.cartography import unrotate_pole from iris.coords import AuxCoord from iris.cube import Cube +from iris.tests import _shared_utils # Run tests in no graphics mode if matplotlib is not available. -if tests.MPL_AVAILABLE: +if _shared_utils.MPL_AVAILABLE: import iris.quickplot as qplt -@tests.skip_data +@_shared_utils.skip_data def simple_cube_w_2d_coords(): - path = tests.get_data_path(("NetCDF", "ORCA2", "votemper.nc")) + path = _shared_utils.get_data_path(("NetCDF", "ORCA2", "votemper.nc")) cube = iris.load_cube(path) return cube -@tests.skip_plot -@tests.skip_data -class Test(tests.GraphicsTest): +@_shared_utils.skip_plot +@_shared_utils.skip_data +class Test(_shared_utils.GraphicsTest): def test_2d_coord_bounds_platecarree(self): # To avoid a problem with Cartopy smearing the data where the # longitude wraps, we set the central_longitude. @@ -56,8 +54,8 @@ def test_2d_coord_bounds_northpolarstereo(self): self.check_graphic() -@tests.skip_plot -class Test2dContour(tests.GraphicsTest): +@_shared_utils.skip_plot +class Test2dContour(_shared_utils.GraphicsTest): def test_2d_coords_contour(self): ny, nx = 4, 6 x1 = np.linspace(-20, 70, nx) @@ -77,7 +75,3 @@ def test_2d_coords_contour(self): ax.gridlines(draw_labels=True) ax.set_extent((0, 180, 0, 90)) self.check_graphic() - - -if __name__ == "__main__": - tests.main() diff --git a/lib/iris/tests/integration/plot/test_vector_plots.py b/lib/iris/tests/integration/plot/test_vector_plots.py index 5419dc182f..0f8ac11a2d 100644 --- a/lib/iris/tests/integration/plot/test_vector_plots.py +++ b/lib/iris/tests/integration/plot/test_vector_plots.py @@ -4,26 +4,24 @@ # See LICENSE in the root of the repository for full licensing details. """Test some key usages of :func:`iris.plot.quiver`.""" -# import iris tests first so that some things can be initialised before -# importing anything else -import iris.tests as tests # isort:skip - import cartopy.crs as ccrs import numpy as np +import pytest from iris.coord_systems import Mercator from iris.coords import AuxCoord, DimCoord from iris.cube import Cube +from iris.tests import _shared_utils from iris.tests.stock import sample_2d_latlons # Run tests in no graphics mode if matplotlib is not available. -if tests.MPL_AVAILABLE: +if _shared_utils.MPL_AVAILABLE: import matplotlib.pyplot as plt from iris.plot import barbs, quiver -@tests.skip_plot +@_shared_utils.skip_plot class MixinVectorPlotCases: """Test examples mixin, used by separate barb, quiver + streamplot classes. @@ -147,7 +145,7 @@ def test_fail_unsupported_coord_system(self): r"Can only plot .* lat-lon projection, .* " r"This .* translates as Cartopy \+proj=merc .*" ) - with self.assertRaisesRegex(ValueError, re_msg): + with pytest.raises(ValueError, match=re_msg): self.plot("2d_rotated", u_cube, v_cube, coords=("longitude", "latitude")) def test_circular_longitude(self): @@ -178,10 +176,7 @@ def test_circular_longitude(self): self.plot("circular", u_cube, v_cube, coords=("longitude", "latitude")) -class TestBarbs(MixinVectorPlotCases, tests.GraphicsTest): - def setUp(self): - super().setUp() - +class TestBarbs(MixinVectorPlotCases, _shared_utils.GraphicsTest): @staticmethod def _nonlatlon_xyuv(): # Increase the range of wind speeds used in the barbs test to test more @@ -206,13 +201,6 @@ def plot_function_to_test(self): return barbs -class TestQuiver(MixinVectorPlotCases, tests.GraphicsTest): - def setUp(self): - super().setUp() - +class TestQuiver(MixinVectorPlotCases, _shared_utils.GraphicsTest): def plot_function_to_test(self): return quiver - - -if __name__ == "__main__": - tests.main() diff --git a/lib/iris/tests/test_mapping.py b/lib/iris/tests/test_mapping.py index 4f59bf8d31..2c3c2fc0a0 100644 --- a/lib/iris/tests/test_mapping.py +++ b/lib/iris/tests/test_mapping.py @@ -4,21 +4,18 @@ # See LICENSE in the root of the repository for full licensing details. """Tests map creation.""" -# import iris tests first so that some things can be initialised before -# importing anything else -import iris.tests as tests # isort:skip - import cartopy.crs as ccrs import numpy as np -import numpy.testing as np_testing +import pytest import iris import iris.coord_systems import iris.cube +from iris.tests import _shared_utils import iris.tests.stock # Run tests in no graphics mode if matplotlib is not available. -if tests.MPL_AVAILABLE: +if _shared_utils.MPL_AVAILABLE: import matplotlib.pyplot as plt import iris.plot as iplt @@ -30,11 +27,11 @@ ) -@tests.skip_plot -@tests.skip_data -class TestBasic(tests.GraphicsTest): - def setUp(self): - super().setUp() +@_shared_utils.skip_plot +@_shared_utils.skip_data +class TestBasic(_shared_utils.GraphicsTest): + @pytest.fixture(autouse=True) + def _setup(self): self.cube = iris.tests.stock.realistic_4d() def test_contourf(self): @@ -54,23 +51,22 @@ def test_unmappable(self): self.check_graphic() def test_default_projection_and_extent(self): - self.assertEqual( - iplt.default_projection(self.cube), - ccrs.RotatedPole(357.5 - 180, 37.5, globe=_DEFAULT_GLOBE), + assert iplt.default_projection(self.cube) == ccrs.RotatedPole( + 357.5 - 180, 37.5, globe=_DEFAULT_GLOBE ) - np_testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( iplt.default_projection_extent(self.cube), (3.59579163e02, 3.59669159e02, -1.28250003e-01, -3.82499993e-02), decimal=3, ) -@tests.skip_data -@tests.skip_plot -class TestUnmappable(tests.GraphicsTest): - def setUp(self): - super().setUp() +@_shared_utils.skip_data +@_shared_utils.skip_plot +class TestUnmappable(_shared_utils.GraphicsTest): + @pytest.fixture(autouse=True) + def _setup(self): src_cube = iris.tests.stock.global_pp() # Make a cube that can't be located on the globe. @@ -96,12 +92,14 @@ def test_simple(self): self.check_graphic() -@tests.skip_data -@tests.skip_plot -class TestMappingSubRegion(tests.GraphicsTest): - def setUp(self): - super().setUp() - cube_path = tests.get_data_path(("PP", "aPProt1", "rotatedMHtimecube.pp")) +@_shared_utils.skip_data +@_shared_utils.skip_plot +class TestMappingSubRegion(_shared_utils.GraphicsTest): + @pytest.fixture(autouse=True) + def _setup(self): + cube_path = _shared_utils.get_data_path( + ("PP", "aPProt1", "rotatedMHtimecube.pp") + ) cube = iris.load_cube(cube_path)[0] # make the data smaller to speed things up. self.cube = cube[::10, ::10] @@ -135,22 +133,21 @@ def test_simple(self): self.check_graphic() def test_default_projection_and_extent(self): - self.assertEqual( - iplt.default_projection(self.cube), - ccrs.RotatedPole(357.5 - 180, 37.5, globe=_DEFAULT_GLOBE), + assert iplt.default_projection(self.cube) == ccrs.RotatedPole( + 357.5 - 180, 37.5, globe=_DEFAULT_GLOBE ) - np_testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( iplt.default_projection_extent(self.cube), (313.01998901, 391.11999512, -22.48999977, 24.80999947), ) -@tests.skip_data -@tests.skip_plot -class TestLowLevel(tests.GraphicsTest): - def setUp(self): - super().setUp() +@_shared_utils.skip_data +@_shared_utils.skip_plot +class TestLowLevel(_shared_utils.GraphicsTest): + @pytest.fixture(autouse=True) + def _setup(self): self.cube = iris.tests.stock.global_pp() self.few = 4 self.few_levels = list(range(280, 300, 5)) @@ -178,11 +175,11 @@ def test_keywords(self): self.check_graphic() -@tests.skip_data -@tests.skip_plot -class TestBoundedCube(tests.GraphicsTest): - def setUp(self): - super().setUp() +@_shared_utils.skip_data +@_shared_utils.skip_plot +class TestBoundedCube(_shared_utils.GraphicsTest): + @pytest.fixture(autouse=True) + def _setup(self): self.cube = iris.tests.stock.global_pp() # Add some bounds to this data (this will actually make the bounds # invalid as they will straddle the north pole and overlap on the @@ -202,28 +199,25 @@ def test_grid(self): self.check_graphic() def test_default_projection_and_extent(self): - self.assertEqual( - iplt.default_projection(self.cube), - ccrs.PlateCarree( - globe=self.cube.coord_system("CoordSystem").as_cartopy_globe() - ), + assert iplt.default_projection(self.cube) == ccrs.PlateCarree( + globe=self.cube.coord_system("CoordSystem").as_cartopy_globe() ) - np_testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( iplt.default_projection_extent(self.cube), [0.0, 360.0, -89.99995422, 89.99998474], ) - np_testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( iplt.default_projection_extent(self.cube, mode=iris.coords.BOUND_MODE), [-1.875046, 358.124954, -90, 90], ) -@tests.skip_data -@tests.skip_plot -class TestLimitedAreaCube(tests.GraphicsTest): - def setUp(self): - super().setUp() - cube_path = tests.get_data_path(("PP", "aPProt1", "rotated.pp")) +@_shared_utils.skip_data +@_shared_utils.skip_plot +class TestLimitedAreaCube(_shared_utils.GraphicsTest): + @pytest.fixture(autouse=True) + def _setup(self): + cube_path = _shared_utils.get_data_path(("PP", "aPProt1", "rotated.pp")) self.cube = iris.load_cube(cube_path)[::20, ::20] self.cube.coord("grid_latitude").guess_bounds() self.cube.coord("grid_longitude").guess_bounds() @@ -240,7 +234,3 @@ def test_scatter(self): iplt.points(self.cube) plt.gca().coastlines("110m") self.check_graphic() - - -if __name__ == "__main__": - tests.main()