123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import pytest
- import sys
- import matplotlib
- from matplotlib import cbook
- def pytest_configure(config):
-
-
-
-
- for key, value in [
- ("markers", "flaky: (Provided by pytest-rerunfailures.)"),
- ("markers", "timeout: (Provided by pytest-timeout.)"),
- ("markers", "backend: Set alternate Matplotlib backend temporarily."),
- ("markers", "style: Set alternate Matplotlib style temporarily."),
- ("markers", "baseline_images: Compare output against references."),
- ("markers", "pytz: Tests that require pytz to be installed."),
- ("markers", "network: Tests that reach out to the network."),
- ("filterwarnings", "error"),
- ]:
- config.addinivalue_line(key, value)
- matplotlib.use('agg', force=True)
- matplotlib._called_from_pytest = True
- matplotlib._init_tests()
- def pytest_unconfigure(config):
- matplotlib._called_from_pytest = False
- @pytest.fixture(autouse=True)
- def mpl_test_settings(request):
- from matplotlib.testing.decorators import _cleanup_cm
- with _cleanup_cm():
- backend = None
- backend_marker = request.node.get_closest_marker('backend')
- if backend_marker is not None:
- assert len(backend_marker.args) == 1, \
- "Marker 'backend' must specify 1 backend."
- backend, = backend_marker.args
- skip_on_importerror = backend_marker.kwargs.get(
- 'skip_on_importerror', False)
- prev_backend = matplotlib.get_backend()
-
- if backend.lower().startswith('qt4'):
- if any(k in sys.modules for k in ('PyQt5', 'PySide2')):
- pytest.skip('Qt5 binding already imported')
- try:
- import PyQt4
-
- except (ImportError, RuntimeError):
- try:
- import PySide
- except ImportError:
- pytest.skip("Failed to import a Qt4 binding.")
- elif backend.lower().startswith('qt5'):
- if any(k in sys.modules for k in ('PyQt4', 'PySide')):
- pytest.skip('Qt4 binding already imported')
- try:
- import PyQt5
-
- except (ImportError, RuntimeError):
- try:
- import PySide2
- except ImportError:
- pytest.skip("Failed to import a Qt5 binding.")
-
- style = ["classic", "_classic_test_patch"]
- style_marker = request.node.get_closest_marker('style')
- if style_marker is not None:
- assert len(style_marker.args) == 1, \
- "Marker 'style' must specify 1 style."
- style, = style_marker.args
- matplotlib.testing.setup()
- with cbook._suppress_matplotlib_deprecation_warning():
- if backend is not None:
-
-
- import matplotlib.pyplot as plt
- try:
- plt.switch_backend(backend)
- except ImportError as exc:
-
-
- if 'cairo' in backend.lower() or skip_on_importerror:
- pytest.skip("Failed to switch to backend {} ({})."
- .format(backend, exc))
- else:
- raise
- matplotlib.style.use(style)
- try:
- yield
- finally:
- if backend is not None:
- plt.switch_backend(prev_backend)
- @pytest.fixture
- def mpl_image_comparison_parameters(request, extension):
-
-
-
-
-
-
-
-
- baseline_images, = request.node.get_closest_marker('baseline_images').args
- if baseline_images is None:
-
-
- baseline_images = request.getfixturevalue('baseline_images')
- func = request.function
- with cbook._setattr_cm(func.__wrapped__,
- parameters=(baseline_images, extension)):
- yield
- @pytest.fixture
- def pd():
- """Fixture to import and configure pandas."""
- pd = pytest.importorskip('pandas')
- try:
- from pandas.plotting import (
- deregister_matplotlib_converters as deregister)
- deregister()
- except ImportError:
- pass
- return pd
|