diff --git a/lib/iris/tests/unit/common/metadata/test_hexdigest.py b/lib/iris/tests/unit/common/metadata/test_hexdigest.py index 1a0a0e0120..0ce0dcd706 100644 --- a/lib/iris/tests/unit/common/metadata/test_hexdigest.py +++ b/lib/iris/tests/unit/common/metadata/test_hexdigest.py @@ -4,21 +4,17 @@ # See LICENSE in the root of the repository for full licensing details. """Unit tests for the :func:`iris.common.metadata.hexdigest`.""" -# Import iris.tests first so that some things can be initialised before -# importing anything else. -import iris.tests as tests # isort:skip - -from unittest import mock - import numpy as np import numpy.ma as ma +import pytest from xxhash import xxh64, xxh64_hexdigest from iris.common.metadata import hexdigest -class TestBytesLikeObject(tests.IrisTest): - def setUp(self): +class TestBytesLikeObject: + @pytest.fixture(autouse=True) + def _setup(self): self.hasher = xxh64() self.hasher.reset() @@ -42,77 +38,77 @@ def test_string(self): value = "hello world" self.hasher.update(value) expected = self.hasher.hexdigest() - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_numpy_array_int(self): value = np.arange(10, dtype=np.int_) expected = self._ndarray(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_numpy_array_float(self): value = np.arange(10, dtype=np.float64) expected = self._ndarray(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_numpy_array_float_not_int(self): ivalue = np.arange(10, dtype=np.int_) fvalue = np.arange(10, dtype=np.float64) expected = self._ndarray(ivalue) - self.assertNotEqual(expected, hexdigest(fvalue)) + assert hexdigest(fvalue) != expected def test_numpy_array_reshape(self): value = np.arange(10).reshape(2, 5) expected = self._ndarray(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_numpy_array_reshape_not_flat(self): value = np.arange(10).reshape(2, 5) expected = self._ndarray(value) - self.assertNotEqual(expected, hexdigest(value.flatten())) + assert hexdigest(value.flatten()) != expected def test_masked_array_int(self): value = ma.arange(10, dtype=np.int_) expected = self._masked(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected value[0] = ma.masked - self.assertNotEqual(expected, hexdigest(value)) + assert hexdigest(value) != expected expected = self._masked(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_masked_array_float(self): value = ma.arange(10, dtype=np.float64) expected = self._masked(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected value[0] = ma.masked - self.assertNotEqual(expected, hexdigest(value)) + assert hexdigest(value) != expected expected = self._masked(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_masked_array_float_not_int(self): ivalue = ma.arange(10, dtype=np.int_) fvalue = ma.arange(10, dtype=np.float64) expected = self._masked(ivalue) - self.assertNotEqual(expected, hexdigest(fvalue)) + assert hexdigest(fvalue) != expected def test_masked_array_not_array(self): value = ma.arange(10) expected = self._masked(value) - self.assertNotEqual(expected, hexdigest(value.data)) + assert hexdigest(value.data) != expected def test_masked_array_reshape(self): value = ma.arange(10).reshape(2, 5) expected = self._masked(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_masked_array_reshape_not_flat(self): value = ma.arange(10).reshape(2, 5) expected = self._masked(value) - self.assertNotEqual(expected, hexdigest(value.flatten())) + assert hexdigest(value.flatten()) != expected -class TestNotBytesLikeObject(tests.IrisTest): +class TestNotBytesLikeObject: def _expected(self, value): parts = str((type(value), value)) return xxh64_hexdigest(parts) @@ -120,42 +116,42 @@ def _expected(self, value): def test_int(self): value = 123 expected = self._expected(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_numpy_int(self): value = int(123) expected = self._expected(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_float(self): value = 123.4 expected = self._expected(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_numpy_float(self): value = float(123.4) expected = self._expected(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_list(self): value = [1, 2, 3] expected = self._expected(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_tuple(self): value = (1, 2, 3) expected = self._expected(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_dict(self): value = dict(one=1, two=2, three=3) expected = self._expected(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected - def test_sentinel(self): - value = mock.sentinel.value + def test_sentinel(self, mocker): + value = mocker.sentinel.value expected = self._expected(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_instance(self): class Dummy: @@ -163,13 +159,9 @@ class Dummy: value = Dummy() expected = self._expected(value) - self.assertEqual(expected, hexdigest(value)) + assert hexdigest(value) == expected def test_int_not_str(self): value = 123 expected = self._expected(value) - self.assertNotEqual(expected, hexdigest(str(value))) - - -if __name__ == "__main__": - tests.main() + assert hexdigest(str(value)) != expected