From 19c91c7e341373aa17e1b97ddfa0037d1b8531b0 Mon Sep 17 00:00:00 2001 From: Bill Little Date: Fri, 15 Mar 2024 15:09:41 +0000 Subject: [PATCH 1/3] pytest migration for unit.common.mixin.test_LimitedAttributeDict --- .../common/mixin/test_LimitedAttributeDict.py | 44 ++++++++----------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py b/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py index f538279bb1..4b700aa963 100644 --- a/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py +++ b/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py @@ -4,63 +4,55 @@ # See LICENSE in the root of the repository for full licensing details. """Unit tests for the :class:`iris.common.mixin.LimitedAttributeDict`.""" -# 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 pytest from iris.common.mixin import LimitedAttributeDict -class Test(tests.IrisTest): - def setUp(self): +class Test: + @pytest.fixture(autouse=True) + def _setup(self): self.forbidden_keys = LimitedAttributeDict.CF_ATTRS_FORBIDDEN self.emsg = "{!r} is not a permitted attribute" def test__invalid_keys(self): for key in self.forbidden_keys: - with self.assertRaisesRegex(ValueError, self.emsg.format(key)): + with pytest.raises(ValueError, match=self.emsg.format(key)): _ = LimitedAttributeDict(**{key: None}) - def test___eq__(self): + def test___eq__(self, mocker): values = dict( - one=mock.sentinel.one, - two=mock.sentinel.two, - three=mock.sentinel.three, + one=mocker.sentinel.one, + two=mocker.sentinel.two, + three=mocker.sentinel.three, ) left = LimitedAttributeDict(**values) right = LimitedAttributeDict(**values) - self.assertEqual(left, right) - self.assertEqual(left, values) + assert left == right + assert left == values def test___eq___numpy(self): values = dict(one=np.arange(1), two=np.arange(2), three=np.arange(3)) left = LimitedAttributeDict(**values) right = LimitedAttributeDict(**values) - self.assertEqual(left, right) - self.assertEqual(left, values) + assert left == right + assert left == values values = dict(one=np.arange(1), two=np.arange(1), three=np.arange(1)) left = LimitedAttributeDict(dict(one=0, two=0, three=0)) right = LimitedAttributeDict(**values) - self.assertEqual(left, right) - self.assertEqual(left, values) + assert left == right + assert left == values def test___setitem__(self): for key in self.forbidden_keys: item = LimitedAttributeDict() - with self.assertRaisesRegex(ValueError, self.emsg.format(key)): + with pytest.raises(ValueError, match=self.emsg.format(key)): item[key] = None def test_update(self): for key in self.forbidden_keys: item = LimitedAttributeDict() - with self.assertRaisesRegex(ValueError, self.emsg.format(key)): - other = {key: None} + other = {key: None} + with pytest.raises(ValueError, match=self.emsg.format(key)): item.update(other) - - -if __name__ == "__main__": - tests.main() From 932f8dfa776da426aaf69f094d1dafbbebe8444a Mon Sep 17 00:00:00 2001 From: Bill Little Date: Mon, 18 Mar 2024 11:15:23 +0000 Subject: [PATCH 2/3] use setup_method --- lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py b/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py index 4b700aa963..5640fddb33 100644 --- a/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py +++ b/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py @@ -11,8 +11,7 @@ class Test: - @pytest.fixture(autouse=True) - def _setup(self): + def setup_method(self): self.forbidden_keys = LimitedAttributeDict.CF_ATTRS_FORBIDDEN self.emsg = "{!r} is not a permitted attribute" From d2e77f0e1af9203d4b291fe03543f9e7caaf5788 Mon Sep 17 00:00:00 2001 From: Bill Little Date: Tue, 19 Mar 2024 10:36:12 +0000 Subject: [PATCH 3/3] use setup fixture --- lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py b/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py index 5640fddb33..4b700aa963 100644 --- a/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py +++ b/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py @@ -11,7 +11,8 @@ class Test: - def setup_method(self): + @pytest.fixture(autouse=True) + def _setup(self): self.forbidden_keys = LimitedAttributeDict.CF_ATTRS_FORBIDDEN self.emsg = "{!r} is not a permitted attribute"