From 8db674a9046bb1a9bf046abcf9193d2d2b92a025 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 14 Jun 2024 19:46:33 -0700 Subject: [PATCH] modernized tests --- tests/test_utilities.py | 46 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index e4048c5f..a06cd2e3 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -34,40 +34,42 @@ def test_realpath(path): class TestAttributeDict(object): - def setup(self): - self.d = gromacs.utilities.AttributeDict(foo="bar", baz="boing") - def test_attribute_get(self): - assert self.d.foo == "bar" + @pytest.fixture + def d(self): + return gromacs.utilities.AttributeDict(foo="bar", baz="boing") - def test_dict_get(self): - assert self.d["foo"] == "bar" - assert self.d.get("foo") == "bar" + def test_attribute_get(self, d): + assert d.foo == "bar" - def test_attribute_set(self): - self.d.gargl = "blaster" - assert self.d.gargl == "blaster" + def test_dict_get(self, d): + assert d["foo"] == "bar" + assert d.get("foo") == "bar" - def test_dict_set(self): - self.d["gargl"] = "blaster" - assert self.d["gargl"] == "blaster" + def test_attribute_set(self, d): + d.gargl = "blaster" + assert d.gargl == "blaster" - def test_dict_hasattr(self): - assert hasattr(self.d, "foo") - assert hasattr(self.d, "baz") - assert not hasattr(self.d, "bar") + def test_dict_set(self, d): + d["gargl"] = "blaster" + assert d["gargl"] == "blaster" - def test_pickle(self): + def test_dict_hasattr(self, d): + assert hasattr(d, "foo") + assert hasattr(d, "baz") + assert not hasattr(d, "bar") + + def test_pickle(self, d): try: - dump = pickle.dumps(self.d, pickle.HIGHEST_PROTOCOL) + dump = pickle.dumps(d, pickle.HIGHEST_PROTOCOL) except Exception as err: raise AssertionError("serializing failed: {}".format(err)) try: - d = pickle.loads(dump) + d_new = pickle.loads(dump) except Exception as err: raise AssertionError("de-serializing failed: {0}".format(str(err))) - assert set(d.keys()) == set(self.d.keys()) - assert set(d.values()) == set(self.d.values()) + assert set(d_new.keys()) == set(d.keys()) + assert set(d_new.values()) == set(d.values()) @pytest.fixture(