File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change 24
24
)
25
25
26
26
27
+ @pytest .fixture (autouse = True )
28
+ def autouse_mpl_cleanup (mpl_cleanup ):
29
+ pass
30
+
31
+
27
32
@pytest .fixture
28
33
def close_figures ():
29
34
plt .close ("all" )
You can’t perform that action at this time.
0 commit comments