diff --git a/lib/iris/tests/unit/common/metadata/test__NamedTupleMeta.py b/lib/iris/tests/unit/common/metadata/test__NamedTupleMeta.py index 5099645082..6009cb85f7 100644 --- a/lib/iris/tests/unit/common/metadata/test__NamedTupleMeta.py +++ b/lib/iris/tests/unit/common/metadata/test__NamedTupleMeta.py @@ -4,16 +4,14 @@ # See LICENSE in the root of the repository for full licensing details. """Unit tests for the :class:`iris.common.metadata._NamedTupleMeta`.""" -# Import iris.tests first so that some things can be initialised before -# importing anything else. -import iris.tests as tests # isort:skip - from abc import abstractmethod +import pytest + from iris.common.metadata import _NamedTupleMeta -class Test(tests.IrisTest): +class Test: @staticmethod def names(classes): return [cls.__name__ for cls in classes] @@ -42,11 +40,11 @@ def _members(self): pass expected = ["object"] - self.assertEqual(self.names(Metadata.__bases__), expected) + assert self.names(Metadata.__bases__) == expected expected = ["Metadata", "object"] - self.assertEqual(self.names(Metadata.__mro__), expected) + assert self.names(Metadata.__mro__) == expected emsg = "Can't instantiate abstract class .* with abstract method.* _members" - with self.assertRaisesRegex(TypeError, emsg): + with pytest.raises(TypeError, match=emsg): _ = Metadata() def test__no_bases_single_member(self): @@ -56,15 +54,15 @@ class Metadata(metaclass=_NamedTupleMeta): _members = member expected = ["MetadataNamedtuple"] - self.assertEqual(self.names(Metadata.__bases__), expected) + assert self.names(Metadata.__bases__) == expected expected = ["Metadata", "MetadataNamedtuple", "tuple", "object"] - self.assertEqual(self.names(Metadata.__mro__), expected) + assert self.names(Metadata.__mro__) == expected emsg = self.emsg_generate(member) - with self.assertRaisesRegex(TypeError, emsg): + with pytest.raises(TypeError, match=emsg): _ = Metadata() metadata = Metadata(1) - self.assertEqual(metadata._fields, (member,)) - self.assertEqual(metadata.arg_one, 1) + assert metadata._fields == (member,) + assert metadata.arg_one == 1 def test__no_bases_multiple_members(self): members = ("arg_one", "arg_two") @@ -73,17 +71,17 @@ class Metadata(metaclass=_NamedTupleMeta): _members = members expected = ["MetadataNamedtuple"] - self.assertEqual(self.names(Metadata.__bases__), expected) + assert self.names(Metadata.__bases__) == expected expected = ["Metadata", "MetadataNamedtuple", "tuple", "object"] - self.assertEqual(self.names(Metadata.__mro__), expected) + assert self.names(Metadata.__mro__) == expected emsg = self.emsg_generate(members) - with self.assertRaisesRegex(TypeError, emsg): + with pytest.raises(TypeError, match=emsg): _ = Metadata() values = range(len(members)) metadata = Metadata(*values) - self.assertEqual(metadata._fields, members) + assert metadata._fields == members expected = dict(zip(members, values)) - self.assertEqual(metadata._asdict(), expected) + assert metadata._asdict() == expected def test__multiple_bases_multiple_members(self): members_parent = ("arg_one", "arg_two") @@ -97,26 +95,26 @@ class MetadataChild(MetadataParent): # Check the parent class... expected = ["MetadataParentNamedtuple"] - self.assertEqual(self.names(MetadataParent.__bases__), expected) + assert self.names(MetadataParent.__bases__) == expected expected = [ "MetadataParent", "MetadataParentNamedtuple", "tuple", "object", ] - self.assertEqual(self.names(MetadataParent.__mro__), expected) + assert self.names(MetadataParent.__mro__) == expected emsg = self.emsg_generate(members_parent) - with self.assertRaisesRegex(TypeError, emsg): + with pytest.raises(TypeError, match=emsg): _ = MetadataParent() values_parent = range(len(members_parent)) metadata_parent = MetadataParent(*values_parent) - self.assertEqual(metadata_parent._fields, members_parent) + assert metadata_parent._fields == members_parent expected = dict(zip(members_parent, values_parent)) - self.assertEqual(metadata_parent._asdict(), expected) + assert metadata_parent._asdict() == expected # Check the dependent child class... expected = ["MetadataChildNamedtuple", "MetadataParent"] - self.assertEqual(self.names(MetadataChild.__bases__), expected) + assert self.names(MetadataChild.__bases__) == expected expected = [ "MetadataChild", "MetadataChildNamedtuple", @@ -125,17 +123,13 @@ class MetadataChild(MetadataParent): "tuple", "object", ] - self.assertEqual(self.names(MetadataChild.__mro__), expected) + assert self.names(MetadataChild.__mro__) == expected emsg = self.emsg_generate((*members_parent, *members_child)) - with self.assertRaisesRegex(TypeError, emsg): + with pytest.raises(TypeError, match=emsg): _ = MetadataChild() fields_child = (*members_parent, *members_child) values_child = range(len(fields_child)) metadata_child = MetadataChild(*values_child) - self.assertEqual(metadata_child._fields, fields_child) + assert metadata_child._fields == fields_child expected = dict(zip(fields_child, values_child)) - self.assertEqual(metadata_child._asdict(), expected) - - -if __name__ == "__main__": - tests.main() + assert metadata_child._asdict() == expected