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

Skip to content

Commit aeb40be

Browse files
story645timhoffm
andcommitted
Update doc/api/testing_api.rst
Co-authored-by: Tim Hoffmann <[email protected]>
1 parent cf4e812 commit aeb40be

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

doc/api/testing_api.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
:show-inheritance:
4040

4141

42-
Data container testing fixtures
42+
Testing with optional dependencies
4343
==================================
44+
For more information on fixtures, see :external+pytest:ref:`pytest fixtures <about-fixtures>`.
4445

4546
.. autofunction:: matplotlib.testing.conftest.pd
4647
.. autofunction:: matplotlib.testing.conftest.xr

lib/matplotlib/testing/conftest.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,20 @@ def mpl_test_settings(request):
8282

8383
@pytest.fixture
8484
def pd():
85-
"""Fixture to import and configure pandas."""
85+
"""
86+
Fixture to import and configure pandas. Using this fixture, the test is skipped when
87+
pandas is not installed. Use this fixture instead of importing pandas in test files.
88+
89+
Examples
90+
--------
91+
Request the pandas fixture by passing in `pd` as an argument to the test ::
92+
93+
def test_matshow_pandas(pd):
94+
95+
df = pd.DataFrame({'x':[1,2,3], 'y':[4,5,6]})
96+
im = plt.figure().subplots().matshow(df)
97+
np.testing.assert_array_equal(im.get_array(), df)
98+
"""
8699
pd = pytest.importorskip('pandas')
87100
try:
88101
from pandas.plotting import (
@@ -95,6 +108,20 @@ def pd():
95108

96109
@pytest.fixture
97110
def xr():
98-
"""Fixture to import xarray."""
111+
"""
112+
Fixture to import xarray so that the test is skipped when xarray is not installed.
113+
Use this fixture instead of importing xrray in test files.
114+
115+
Examples
116+
--------
117+
Request the xarray fixture by passing in `xr` as an argument to the test ::
118+
119+
def test_imshow_xarray(xr):
120+
121+
ds = xr.DataArray(np.random.randn(2, 3))
122+
im = plt.figure().subplots().imshow(ds)
123+
np.testing.assert_array_equal(im.get_array(), ds)
124+
"""
125+
99126
xr = pytest.importorskip('xarray')
100127
return xr

0 commit comments

Comments
 (0)