diff --git a/test/test_fourier_filters.py b/test/test_fourier_filters.py index 2e9db056..4e1d1c3c 100644 --- a/test/test_fourier_filters.py +++ b/test/test_fourier_filters.py @@ -1,6 +1,5 @@ import math as m import sys -import pytest import numpy as np import xarray as xr diff --git a/test/test_gradient.py b/test/test_gradient.py index f5a04df8..287f3b75 100644 --- a/test/test_gradient.py +++ b/test/test_gradient.py @@ -1,4 +1,5 @@ import sys +import pytest import numpy as np import xarray as xr @@ -23,8 +24,8 @@ class Test_Gradient: results_lon = None results_lat = None - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(autouse=True) + def setUpClass(cls) -> None: cls.test_data_xr = xr.load_dataset( 'test/gradient_test_data.nc').to_array().squeeze() cls.test_data_xr_nocoords = xr.DataArray(cls.test_data_xr, coords={}) diff --git a/test/test_interpolation.py b/test/test_interpolation.py index d55d6eb8..bc032b29 100644 --- a/test/test_interpolation.py +++ b/test/test_interpolation.py @@ -316,8 +316,8 @@ def test_interp_sigma_to_hybrid_wrong_method(self) -> None: class Test_interp_manually_calc: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(autouse=True) + def setUpClass(cls) -> None: cls.test_input = xr.load_dataset( gdf.get("netcdf_files/interpolation_test_input_data.nc")) @@ -454,8 +454,8 @@ class Test_interp_larger_dataset: test_lon_output = None test_data_chunked = None - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(autouse=True) + def setUpClass(cls) -> None: cls.test_input = xr.load_dataset( gdf.get("netcdf_files/spherical_noise_input.nc"))['spherical_noise'] diff --git a/test/test_meteorology.py b/test/test_meteorology.py index 3aadaf24..3ed8a37e 100644 --- a/test/test_meteorology.py +++ b/test/test_meteorology.py @@ -18,8 +18,8 @@ class Test_dewtemp: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(scope="function", autouse=True) + def setUpClass(cls) -> None: # set up ground truths cls.t_def = [ 29.3, 28.1, 23.5, 20.9, 18.4, 15.9, 13.1, 10.1, 6.7, 3.1, -0.5, @@ -41,6 +41,8 @@ def test_setUpClass(cls) -> None: # make dask client to reference in subsequent tests cls.client = dd.Client() + yield cls.client + cls.client.close() def test_float_input(self) -> None: tk = 18. + 273.15 @@ -85,15 +87,11 @@ def test_dask_lazy(self) -> None: assert isinstance((dewtemp(tk, rh) - 273.15).data, dask.array.Array) - @classmethod - def test_closeClient(cls) -> None: - cls.client.close() - class Test_heat_index: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(scope="function", autouse=True) + def setUpClass(cls) -> None: # set up ground truths cls.ncl_gt_1 = [ 137.36142, 135.86795, 104.684456, 131.25621, 105.39449, 79.78999, @@ -112,6 +110,8 @@ def test_setUpClass(cls) -> None: # make client to reference in subsequent tests cls.client = dd.Client() + yield cls.client + cls.client.close() def test_numpy_input(self) -> None: assert np.allclose(heat_index(self.t1, self.rh1, False), @@ -192,15 +192,11 @@ def test_dask_lazy(self) -> None: assert isinstance((heat_index(t, rh)).data, dask.array.Array) - @classmethod - def test_closeClient(cls) -> None: - cls.client.close() - class Test_relhum: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(scope="function", autouse=True) + def setUpClass(cls) -> None: # set up ground truths cls.p_def = [ 100800, 100000, 95000, 90000, 85000, 80000, 75000, 70000, 65000, @@ -232,6 +228,8 @@ def test_setUpClass(cls) -> None: # make dask client to reference in subsequent tests cls.client = dd.Client() + yield cls.client + cls.client.close() def test_float_input(self) -> None: p = 1000. * 100 @@ -275,10 +273,6 @@ def test_dask_lazy(self) -> None: assert isinstance(relhum(t, q, p).data, dask.array.Array) - @classmethod - def test_closeClient(cls) -> None: - cls.client.close() - class Test_relhum_water: @@ -307,8 +301,8 @@ def test_float_input(self) -> None: class Test_actual_saturation_vapor_pressure: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(scope="function", autouse=True) + def setUpClass(cls) -> None: # get ground truth from ncl run netcdf file try: @@ -325,6 +319,8 @@ def test_setUpClass(cls) -> None: # make client to reference in subsequent tests cls.client = dd.Client() + yield cls.client + cls.client.close() def test_numpy_input(self) -> None: assert np.allclose(actual_saturation_vapor_pressure( @@ -375,15 +371,11 @@ def test_dask_lazy(self) -> None: (actual_saturation_vapor_pressure(tempf, tfill=1.0000000e+20)).data, dask.array.Array) - @classmethod - def test_closeClient(cls) -> None: - cls.client.close() - class Test_max_daylight: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(scope="function", autouse=True) + def setUpClass(cls) -> None: # get ground truth from ncl run netcdf file try: @@ -401,6 +393,8 @@ def test_setUpClass(cls) -> None: # make client to reference in subsequent tests cls.client = dd.Client() + yield cls.client + cls.client.close() def test_numpy_input(self) -> None: assert np.allclose(max_daylight(self.jday_gt, self.lat_gt), @@ -450,15 +444,11 @@ def test_lat_bound_second_warning(self) -> None: with pytest.warns(UserWarning): max_daylight(10, 67) - @classmethod - def test_closeClient(cls) -> None: - cls.client.close() - class Test_psychrometric_constant: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(scope="function", autouse=True) + def setUpClass(cls) -> None: # get ground truth from ncl run netcdf file try: @@ -475,6 +465,8 @@ def test_setUpClass(cls) -> None: # make client to reference in subsequent tests cls.client = dd.Client() + yield cls.client + cls.client.close() def test_numpy_input(self) -> None: assert np.allclose(psychrometric_constant(self.pressure_gt), @@ -520,15 +512,11 @@ def test_dask_lazy(self) -> None: assert isinstance((psychrometric_constant(pressure)).data, dask.array.Array) - @classmethod - def test_closeClient(cls) -> None: - cls.client.close() - class Test_saturation_vapor_pressure: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(scope="function", autouse=True) + def setUpClass(cls) -> None: # get ground truth from ncl run netcdf file try: @@ -545,6 +533,8 @@ def test_setUpClass(cls) -> None: # make client to reference in subsequent tests cls.client = dd.Client() + yield cls.client + cls.client.close() def test_numpy_input(self) -> None: assert np.allclose(saturation_vapor_pressure(self.temp_gt, @@ -596,15 +586,11 @@ def test_dask_lazy(self) -> None: tfill=1.0000000e+20)).data, dask.array.Array) - @classmethod - def test_closeClient(cls) -> None: - cls.client.close() - class Test_saturation_vapor_pressure_slope: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(scope="function", autouse=True) + def setUpClass(cls) -> None: # get ground truth from ncl run netcdf file try: @@ -621,6 +607,8 @@ def test_setUpClass(cls) -> None: # make client to reference in subsequent tests cls.client = dd.Client() + yield cls.client + cls.client.close() def test_numpy_input(self) -> None: assert np.allclose(saturation_vapor_pressure_slope(self.temp_gt), @@ -670,15 +658,11 @@ def test_dask_lazy(self) -> None: assert isinstance((saturation_vapor_pressure_slope(tempf)).data, dask.array.Array) - @classmethod - def test_closeClient(cls) -> None: - cls.client.close() - class Test_Delta_Pressure: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(scope="function", autouse=True) + def setUpClass(cls) -> None: cls.pressure_lev = np.array([1, 5, 100, 1000]) cls.pressure_lev_da = xr.DataArray(cls.pressure_lev) cls.pressure_lev_da.attrs = { diff --git a/test/test_spherical.py b/test/test_spherical.py index 9c23f250..1d878e51 100644 --- a/test/test_spherical.py +++ b/test/test_spherical.py @@ -1,5 +1,6 @@ import math as ma import sys +import pytest import numpy as np import scipy.special as ss @@ -10,8 +11,8 @@ class Test_Spherical: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(autouse=True) + def setUpClass(cls) -> None: max_harm = 23 num_phi = 90 num_theta = 180 diff --git a/test/test_stats.py b/test/test_stats.py index 97a61e66..a45e5f17 100644 --- a/test/test_stats.py +++ b/test/test_stats.py @@ -332,8 +332,8 @@ def test_02(self) -> None: class Test_pearson_r: - @classmethod - def test_setUpClass(cls) -> None: + @pytest.fixture(autouse=True) + def setUpClass(cls) -> None: # Coordinates times = xr.cftime_range(start='2022-08-01', end='2022-08-05', freq='D') lats = np.linspace(start=-45, stop=45, num=3, dtype='float32')