Ensure Matplotlib is cleaned up around a test. Before a test is run: 1) Set the backend to "template" to avoid requiring a GUI. After a test is run: 1) Reset units registry 2) Reset rc_context 3) Close all figures See matplotlib/testing/decorators.py#L24.
()
| 1978 | |
| 1979 | @pytest.fixture |
| 1980 | def mpl_cleanup(): |
| 1981 | """ |
| 1982 | Ensure Matplotlib is cleaned up around a test. |
| 1983 | |
| 1984 | Before a test is run: |
| 1985 | |
| 1986 | 1) Set the backend to "template" to avoid requiring a GUI. |
| 1987 | |
| 1988 | After a test is run: |
| 1989 | |
| 1990 | 1) Reset units registry |
| 1991 | 2) Reset rc_context |
| 1992 | 3) Close all figures |
| 1993 | |
| 1994 | See matplotlib/testing/decorators.py#L24. |
| 1995 | """ |
| 1996 | mpl = pytest.importorskip("matplotlib") |
| 1997 | mpl_units = pytest.importorskip("matplotlib.units") |
| 1998 | plt = pytest.importorskip("matplotlib.pyplot") |
| 1999 | orig_units_registry = mpl_units.registry.copy() |
| 2000 | try: |
| 2001 | with mpl.rc_context(): |
| 2002 | mpl.use("template") |
| 2003 | yield |
| 2004 | finally: |
| 2005 | mpl_units.registry.clear() |
| 2006 | mpl_units.registry.update(orig_units_registry) |
| 2007 | plt.close("all") |
| 2008 | # https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.6.0.html#garbage-collection-is-no-longer-run-on-figure-close # noqa: E501 |
| 2009 | gc.collect(1) |
| 2010 | |
| 2011 | |
| 2012 | @pytest.fixture( |