Skip to content

Commit cf0e145

Browse files
committed
fix matplotlib tests for Windows 11
1 parent 1160e89 commit cf0e145

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import gc
2+
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def mpl_cleanup():
8+
"""
9+
Ensure Matplotlib is cleaned up around a test.
10+
11+
Before a test is run:
12+
13+
1) Set the backend to "template" to avoid requiring a GUI.
14+
15+
After a test is run:
16+
17+
1) Reset units registry
18+
2) Reset rc_context
19+
3) Close all figures
20+
21+
See matplotlib/testing/decorators.py#L24.
22+
"""
23+
mpl = pytest.importorskip("matplotlib")
24+
mpl_units = pytest.importorskip("matplotlib.units")
25+
plt = pytest.importorskip("matplotlib.pyplot")
26+
orig_units_registry = mpl_units.registry.copy()
27+
try:
28+
with mpl.rc_context():
29+
mpl.use("template")
30+
yield
31+
finally:
32+
mpl_units.registry.clear()
33+
mpl_units.registry.update(orig_units_registry)
34+
plt.close("all")
35+
# https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.6.0.html#garbage-collection-is-no-longer-run-on-figure-close
36+
gc.collect(1)

tests/test_plotting.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
)
2525

2626

27+
@pytest.fixture(autouse=True)
28+
def autouse_mpl_cleanup(mpl_cleanup):
29+
pass
30+
31+
2732
@pytest.fixture
2833
def close_figures():
2934
plt.close("all")

0 commit comments

Comments
 (0)