Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/api/testing_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@
:members:
:undoc-members:
:show-inheritance:


Testing with optional dependencies
==================================
For more information on fixtures, see :external+pytest:ref:`pytest fixtures <about-fixtures>`.

.. autofunction:: matplotlib.testing.conftest.pd
.. autofunction:: matplotlib.testing.conftest.xr
1 change: 1 addition & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def tutorials_download_error(record):

autosummary_generate = True
autodoc_typehints = "none"
autodoc_mock_imports = ["pytest"]

# we should ignore warnings coming from importing deprecated modules for
# autodoc purposes, as this will disappear automatically when they are removed
Expand Down
31 changes: 29 additions & 2 deletions lib/matplotlib/testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,20 @@ def mpl_test_settings(request):

@pytest.fixture
def pd():
"""Fixture to import and configure pandas."""
"""
Fixture to import and configure pandas. Using this fixture, the test is skipped when
pandas is not installed. Use this fixture instead of importing pandas in test files.

Examples
--------
Request the pandas fixture by passing in ``pd`` as an argument to the test ::

def test_matshow_pandas(pd):

df = pd.DataFrame({'x':[1,2,3], 'y':[4,5,6]})
im = plt.figure().subplots().matshow(df)
np.testing.assert_array_equal(im.get_array(), df)
"""
pd = pytest.importorskip('pandas')
try:
from pandas.plotting import (
Expand All @@ -95,6 +108,20 @@ def pd():

@pytest.fixture
def xr():
"""Fixture to import xarray."""
"""
Fixture to import xarray so that the test is skipped when xarray is not installed.
Use this fixture instead of importing xrray in test files.

Examples
--------
Request the xarray fixture by passing in ``xr`` as an argument to the test ::

def test_imshow_xarray(xr):

ds = xr.DataArray(np.random.randn(2, 3))
im = plt.figure().subplots().imshow(ds)
np.testing.assert_array_equal(im.get_array(), ds)
"""

xr = pytest.importorskip('xarray')
return xr