Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TST: Adding an autouse test fixture for mpl tests
This takes care of all the cleanup of figures and state for each test.
- Loading branch information
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright Cartopy Contributors | ||
# | ||
# This file is part of Cartopy and is released under the LGPL license. | ||
# See COPYING and COPYING.LESSER in the root of the repository for full | ||
# licensing details. | ||
import matplotlib.pyplot as plt | ||
import pytest | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def mpl_test_cleanup(request): | ||
"""Runs tests in a context manager and closes figures after each test""" | ||
try: | ||
# Run each test in a context manager so that state does not leak out | ||
orig_backend = plt.get_backend() | ||
plt.switch_backend('Agg') | ||
with plt.rc_context(): | ||
yield | ||
finally: | ||
# Closes all open figures and switches backend back to original | ||
plt.switch_backend(orig_backend) |