Skip to content

Commit 87b0a2c

Browse files
committed
TST: only partially skip image tests when pytest-mpl is missing
1 parent 3fce42a commit 87b0a2c

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

tests/conftest.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import textwrap
22
from importlib.util import find_spec
33

4-
import pytest
54
from runtime_introspect import runtime_feature_set
65

76
import gpgi
@@ -16,17 +15,11 @@ def pytest_configure(config):
1615
mpl.use("Agg")
1716
else: # pragma: no cover
1817
config.addinivalue_line(
19-
"markers", "mpl_image_compare: skip (missing requirement: pytest_mpl)"
18+
"markers",
19+
"mpl_image_compare: partial skip (missing requirement: pytest_mpl)",
2020
)
2121

2222

23-
def pytest_runtest_setup(item):
24-
if HAVE_PYTEST_MPL:
25-
return
26-
if any(item.iter_markers(name="mpl_image_compare")): # pragma: no cover
27-
pytest.skip("missing requirement: pytest_mpl")
28-
29-
3023
def pytest_report_header(config, start_path) -> list[str]:
3124
fs = runtime_feature_set()
3225
diagnostics = fs.diagnostics(features=["free-threading", "JIT"])

tests/test_deposit.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from contextlib import nullcontext
33
from copy import deepcopy
44
from functools import partial
5+
from importlib.util import find_spec
56
from threading import Lock
67

78
import numpy as np
@@ -11,6 +12,8 @@
1112
import gpgi
1213
from gpgi._lib import _deposit_ngp_2D
1314

15+
HAVE_PYTEST_MPL = find_spec("pytest_mpl") is not None
16+
1417

1518
@pytest.fixture()
1619
def sample_2D_dataset():
@@ -120,11 +123,13 @@ def fake_dep(*args, metadata=None, **kwargs):
120123
@pytest.mark.parametrize("method", ["ngp", "cic", "tsc"])
121124
@pytest.mark.mpl_image_compare
122125
def test_2D_deposit(sample_2D_dataset, method):
123-
from matplotlib.figure import Figure
124-
125126
ds = sample_2D_dataset
126127
particle_density = ds.deposit("mass", method=method)
127128

129+
if not HAVE_PYTEST_MPL:
130+
pytest.skip(reason="pytest-mpl is not available")
131+
from matplotlib.figure import Figure
132+
128133
fig = Figure()
129134
ax = fig.add_subplot()
130135

@@ -153,8 +158,6 @@ def test_2D_deposit(sample_2D_dataset, method):
153158
@pytest.mark.parametrize("grid_type", ["linear", "geometric"])
154159
@pytest.mark.mpl_image_compare
155160
def test_1D_deposit(method, grid_type):
156-
from matplotlib.figure import Figure
157-
158161
if grid_type == "linear":
159162
xedges = np.linspace(1, 2, 6)
160163
elif grid_type == "geometric":
@@ -178,6 +181,11 @@ def test_1D_deposit(method, grid_type):
178181
if method == "ngp":
179182
assert mass.sum() == ds.particles.count
180183

184+
if not HAVE_PYTEST_MPL:
185+
pytest.skip(reason="pytest-mpl is not available")
186+
187+
from matplotlib.figure import Figure
188+
181189
fig = Figure()
182190
ax = fig.add_subplot()
183191
ax.set(xlabel="x", ylabel="particle mass", title=f"Deposition method '{method}'")
@@ -243,8 +251,6 @@ def test_3D_deposit(method, dtype):
243251

244252
@pytest.mark.mpl_image_compare
245253
def test_readme_example():
246-
from matplotlib.figure import Figure
247-
248254
nx = ny = 64
249255
nparticles = 600_000
250256

@@ -271,6 +277,10 @@ def test_readme_example():
271277

272278
particle_mass = ds.deposit("mass", method="nearest_grid_point")
273279

280+
if not HAVE_PYTEST_MPL:
281+
pytest.skip(reason="pytest-mpl is not available")
282+
from matplotlib.figure import Figure
283+
274284
fig = Figure()
275285
ax = fig.add_subplot()
276286
ax.set(aspect=1, xlabel="x", ylabel="y")

0 commit comments

Comments
 (0)