Thanks to visit codestin.com
Credit goes to github.com

Skip to content

doc: add pandas and xarray fixtures to testing API docs #28879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 26, 2024
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
Loading