Skip to content

Commit

Permalink
TST: Adding an autouse test fixture for mpl tests
Browse files Browse the repository at this point in the history
This takes care of all the cleanup of figures and state
for each test.
  • Loading branch information
greglucas committed Aug 7, 2020
1 parent 640774d commit cc846d4
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/cartopy/tests/mpl/conftest.py
@@ -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)

0 comments on commit cc846d4

Please sign in to comment.