diff --git a/lib/matplotlib/testing/conftest.py b/lib/matplotlib/testing/conftest.py index d2f699c2be81..8cb90e083648 100644 --- a/lib/matplotlib/testing/conftest.py +++ b/lib/matplotlib/testing/conftest.py @@ -43,7 +43,16 @@ def mpl_test_settings(request): # This import must come after setup() so it doesn't load the # default backend prematurely. import matplotlib.pyplot as plt - plt.switch_backend(backend) + try: + plt.switch_backend(backend) + except ImportError as exc: + # Should only occur for the cairo backend tests, if neither + # pycairo nor cairocffi are installed. + if 'cairo' in backend.lower(): + pytest.skip("Failed to switch to backend {} ({})." + .format(backend, exc)) + else: + raise with warnings.catch_warnings(): warnings.simplefilter("ignore", MatplotlibDeprecationWarning) matplotlib.style.use(style)