From cb073b1898044dff70988e969261fde8ff64e9c7 Mon Sep 17 00:00:00 2001 From: "David W.H. Swenson" Date: Sun, 28 Jan 2024 10:15:14 -0600 Subject: [PATCH 1/3] tests: `setup`,`teardown` -> `setup_method`, etc Pytest prefers (and now, apparently requires) the use of `setup_method` instead of `setup`. --- contact_map/tests/test_concurrence.py | 6 +++--- contact_map/tests/test_contact_count.py | 2 +- contact_map/tests/test_contact_map.py | 4 ++-- contact_map/tests/test_contact_trajectory.py | 8 ++++---- contact_map/tests/test_dask_runner.py | 4 ++-- contact_map/tests/test_frequency_task.py | 2 +- contact_map/tests/test_min_dist.py | 4 ++-- contact_map/tests/test_plot_utils.py | 2 +- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/contact_map/tests/test_concurrence.py b/contact_map/tests/test_concurrence.py index 0347b5d..92b53bb 100644 --- a/contact_map/tests/test_concurrence.py +++ b/contact_map/tests/test_concurrence.py @@ -65,7 +65,7 @@ def _test_getitem(self, concurrence, pair_to_expected): class TestAtomContactConcurrence(ContactConcurrenceTester): - def setup(self): + def setup_method(self): self.concurrence = AtomContactConcurrence( trajectory=traj, atom_contacts=contacts.atom_contacts.most_common(), @@ -105,7 +105,7 @@ def test_set_labels(self): class TestResidueContactConcurrence(ContactConcurrenceTester): - def setup(self): + def setup_method(self): self.heavy_contact_concurrence = ResidueContactConcurrence( trajectory=traj, residue_contacts=contacts.residue_contacts.most_common(), @@ -157,7 +157,7 @@ def test_getitem(self, conc_type): class TestConcurrencePlotter(object): - def setup(self): + def setup_method(self): self.concurrence = ResidueContactConcurrence( trajectory=traj, residue_contacts=contacts.residue_contacts.most_common(), diff --git a/contact_map/tests/test_contact_count.py b/contact_map/tests/test_contact_count.py index ab87a89..864a923 100644 --- a/contact_map/tests/test_contact_count.py +++ b/contact_map/tests/test_contact_count.py @@ -14,7 +14,7 @@ from contact_map.contact_count import * class TestContactCount(object): - def setup(self): + def setup_method(self): self.map = ContactFrequency(traj, cutoff=0.075, n_neighbors_ignored=0) self.topology = self.map.topology diff --git a/contact_map/tests/test_contact_map.py b/contact_map/tests/test_contact_map.py index 60f4f3d..673c2b9 100644 --- a/contact_map/tests/test_contact_map.py +++ b/contact_map/tests/test_contact_map.py @@ -117,7 +117,7 @@ class TestContactObject(object): # note: these used to be the tests for the separate single-frame # ContactMap class; however, it includes a lot of good unit tests for # ContactObject - def setup(self): + def setup_method(self): self.topology = traj.topology self.map0 = ContactFrequency(traj[0], cutoff=0.075, n_neighbors_ignored=0) @@ -336,7 +336,7 @@ def test_no_unitcell(self, idx): class TestContactFrequency(object): - def setup(self): + def setup_method(self): self.atoms = [0, 1, 4, 5, 6, 7, 8, 9] self.map = ContactFrequency(trajectory=traj, cutoff=0.075, diff --git a/contact_map/tests/test_contact_trajectory.py b/contact_map/tests/test_contact_trajectory.py index c9575a4..0857c13 100644 --- a/contact_map/tests/test_contact_trajectory.py +++ b/contact_map/tests/test_contact_trajectory.py @@ -31,7 +31,7 @@ ] class TestContactTrajectory(object): - def setup(self): + def setup_method(self): self.traj = md.load(find_testfile("trajectory.pdb")) self.map = ContactTrajectory(self.traj, cutoff=0.075, n_neighbors_ignored=0) @@ -175,7 +175,7 @@ def test_rolling_frequency(self): class TestMutableContactTrajectory(object): - def setup(self): + def setup_method(self): self.traj = md.load(find_testfile("trajectory.pdb")) self.map = MutableContactTrajectory(self.traj, cutoff=0.075, n_neighbors_ignored=0) @@ -223,7 +223,7 @@ def test_hash_eq(self): class TestWindowedIterator(object): - def setup(self): + def setup_method(self): self.iter = WindowedIterator(length=10, width=3, step=2, slow_build=False) @@ -286,7 +286,7 @@ def test_next(self, length, width, step, slow_build, expected): class TestRollingContactFrequency(object): - def setup(self): + def setup_method(self): self.traj = md.load(find_testfile("trajectory.pdb")) self.map = ContactTrajectory(self.traj, cutoff=0.075, n_neighbors_ignored=0) diff --git a/contact_map/tests/test_dask_runner.py b/contact_map/tests/test_dask_runner.py index d704db5..7717d38 100644 --- a/contact_map/tests/test_dask_runner.py +++ b/contact_map/tests/test_dask_runner.py @@ -29,7 +29,7 @@ def dask_setup_test_cluster(distributed, n_workers=4, n_attempts=3): class TestDaskRunners(object): - def setup(self): + def setup_method(self): dask = pytest.importorskip('dask') # pylint: disable=W0612 distributed = pytest.importorskip('dask.distributed') self.distributed = distributed @@ -39,7 +39,7 @@ def setup(self): self.client = distributed.Client(self.cluster) self.filename = find_testfile("trajectory.pdb") - def teardown(self): + def teardown_method(self): self.client.shutdown() @pytest.mark.parametrize("dask_cls", [DaskContactFrequency, diff --git a/contact_map/tests/test_frequency_task.py b/contact_map/tests/test_frequency_task.py index 069b8d0..6ba9dd5 100644 --- a/contact_map/tests/test_frequency_task.py +++ b/contact_map/tests/test_frequency_task.py @@ -33,7 +33,7 @@ def test_default_slice_even_split(self, inputs, results): assert default_slices(n_total, n_workers) == results class TestTasks(object): - def setup(self): + def setup_method(self): self.contact_freq_0_4 = ContactFrequency(traj[:4], cutoff=0.075, n_neighbors_ignored=0) self.contact_freq_4 = ContactFrequency(traj[4], cutoff=0.075, diff --git a/contact_map/tests/test_min_dist.py b/contact_map/tests/test_min_dist.py index 5193f3d..e2da536 100644 --- a/contact_map/tests/test_min_dist.py +++ b/contact_map/tests/test_min_dist.py @@ -14,7 +14,7 @@ @pytest.mark.parametrize("idx", [0, 4]) class TestNearestAtoms(object): - def setup(self): + def setup_method(self): self.nearest_atoms = { idx: NearestAtoms(traj, cutoff=0.075, frame_number=idx) for idx in [0, 4] @@ -107,7 +107,7 @@ def test_sorted_distances(self, idx): class TestMinimumDistanceCounter(object): - def setup(self): + def setup_method(self): self.topology = traj.topology query = [4, 5] haystack = list(set(range(10)) - set(query)) diff --git a/contact_map/tests/test_plot_utils.py b/contact_map/tests/test_plot_utils.py index 8a3d3c2..c52f9b3 100644 --- a/contact_map/tests/test_plot_utils.py +++ b/contact_map/tests/test_plot_utils.py @@ -72,7 +72,7 @@ def test_is_cmap_diverging(cmap): class TestContactRange(object): - def setup(self): + def setup_method(self): self.cr = _ContactPlotRange(5) @pytest.mark.parametrize("case", [(_ContactPlotRange(5), True), From 0e37e212d6f45776ba3118de8d17eec27adbf876 Mon Sep 17 00:00:00 2001 From: "David W.H. Swenson" Date: Sun, 28 Jan 2024 10:26:28 -0600 Subject: [PATCH 2/3] change `pytest.warns(None)` This was an old way to say that no warnings were issued. Updated behavior here, per: https://docs.pytest.org/en/7.0.x/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests --- contact_map/tests/test_contact_count.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contact_map/tests/test_contact_count.py b/contact_map/tests/test_contact_count.py index 864a923..bc3806e 100644 --- a/contact_map/tests/test_contact_count.py +++ b/contact_map/tests/test_contact_count.py @@ -62,7 +62,8 @@ def test_plot_kwargs(self): @pytest.mark.skipif(not HAS_MATPLOTLIB, reason="Missing matplotlib") def test_pixel_warning(self): # This should not raise a warning (5*2>=10) - with pytest.warns(None) as record: + with warnings.catch_warnings(): + warnings.simplefilter("error") self.atom_contacts.plot(figsize=(5, 5), dpi=2) # See if no warning was raised assert len(record) == 0 From 5dd2a2c994b7d2a489e741f1fd46362f637d12b1 Mon Sep 17 00:00:00 2001 From: "David W.H. Swenson" Date: Sun, 28 Jan 2024 10:33:54 -0600 Subject: [PATCH 3/3] remove extra line --- contact_map/tests/test_contact_count.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contact_map/tests/test_contact_count.py b/contact_map/tests/test_contact_count.py index bc3806e..99fde49 100644 --- a/contact_map/tests/test_contact_count.py +++ b/contact_map/tests/test_contact_count.py @@ -65,8 +65,7 @@ def test_pixel_warning(self): with warnings.catch_warnings(): warnings.simplefilter("error") self.atom_contacts.plot(figsize=(5, 5), dpi=2) - # See if no warning was raised - assert len(record) == 0 + # should convert to error if warning issued # Now raise the warning as 4*2 < 10 with pytest.warns(RuntimeWarning) as record: