Skip to content

Commit 7af719d

Browse files
timhoffmstory645
andcommitted
Apply suggestions from code review
Co-authored-by: hannah <story645@gmail.com>
1 parent efb3382 commit 7af719d

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

doc/devel/testing.rst

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,22 @@ to the folder where the baseline test images are stored. The triage tool require
103103
Writing tests
104104
-------------
105105
Tests are located in :file:`lib/matplotlib/tests`. They are organized to mirror
106-
the structure of the main code in :file:`lib/matplotlib`. For example, tests for
106+
the structure of the code in :file:`lib/matplotlib`. For example, tests for
107107
the ``mathtext.py`` module are in :file:`lib/matplotlib/tests/test_mathtext.py`.
108108

109109
Naming follows standard pytest conventions:
110110

111111
- files begin with ``"test_"``
112112
- test functions begin with ``"test_"``
113-
- test class begin ``"Test"``.
113+
- test classes begin with ``"Test"``.
114114

115115
We prefer simple test functions, but test classes are also acceptable.
116116
Test function names should be descriptive of what they are testing, and long names
117117
like ``test_to_rgba_array_accepts_color_alpha_tuple_with_multiple_colors()`` are
118118
perfectly fine.
119119

120-
Simple unit tests
121-
^^^^^^^^^^^^^^^^^
120+
Unit tests
121+
^^^^^^^^^^
122122

123123
Many elements of Matplotlib can be tested using simple unit tests, e.g. ::
124124

@@ -127,17 +127,17 @@ Many elements of Matplotlib can be tested using simple unit tests, e.g. ::
127127

128128
Data in tests
129129
^^^^^^^^^^^^^
130-
Try to use minimal explicit data. Often you can use explicit values like
131-
``[1, 2, 3]``, ``range(5)`` or ``np.arange(5)``. This is cheap and
130+
Try to use minimal explicit data, such as
131+
``[1, 2, 3]``, ``range(5)`` or ``np.arange(5)``, because it
132132
makes the test more readable.
133133

134134
When you need more and non-trivial data, generate it programmatically, e.g. ::
135135

136136
x = np.linspace(0, 2*np.pi, 101)
137137
y = 2 * np.sin(x) + 1
138138

139-
Use random numbers only, if an algorihmical way to generate the data is not
140-
possible or too cumbersome. In this case, set the seed to a fixed value to make
139+
Use random numbers only when an algorithmic way to generate the data is too
140+
cumbersome or impossible. In this case, set the seed to a fixed value to make
141141
the test deterministic. For numpy's default random number generator use ::
142142

143143
import numpy as np
@@ -149,9 +149,10 @@ The seed is :ref:`John Hunter's <project_history>` birthday.
149149

150150
Test cleanup
151151
^^^^^^^^^^^^
152-
We often need figures or modify `.rcParams` to test some functionality. Cleanup
153-
of such side effects is handled automatically through the the pytest fixture
154-
``matplotlib.testing.conftest.mpl_test_settings``.
152+
We often need to create figures or to modify `.rcParams` to test some functionality.
153+
Cleanup of such side effects is handled automatically through a pytest fixture
154+
(``matplotlib.testing.conftest.mpl_test_settings``) so that no manual cleanup is
155+
necessary.
155156

156157
In particular, you don't need to call ``plt.close()``.
157158

@@ -161,8 +162,9 @@ When you need figures and/or Axes, create them through the standard methods
161162
(``plt.figure()``, ``plt.subplots()``, etc.).
162163

163164
Creating figures and Axes is rather expensive (>100ms). Only create as many as you need for
164-
the test, and reuse them if possible. It is perfectly fine to do multiple operations
165-
and asserts in one test, e.g.::
165+
the test, and reuse them if possible. It is perfectly fine to test multiple parametrizations
166+
or related functionality in one test; i.e. extend the classical test structure
167+
*Arrange–Act–Assert* with multiple *Act-Assert* blocks, e.g. ::
166168

167169
def test_stackplot_facecolor():
168170
# Test that facecolors are properly passed and take precedence over colors parameter
@@ -172,17 +174,19 @@ and asserts in one test, e.g.::
172174

173175
fig, ax = plt.subplots()
174176

175-
colls = ax.stackplot(x, y1, y2, facecolor=['r', 'b'], colors=['c', 'm'])
177+
facecolors = ['r', 'b']
178+
179+
colls = ax.stackplot(x, y1, y2, facecolor=facecolors, colors=['c', 'm'])
176180
for coll, fcolor in zip(colls, facecolors):
177181
assert mcolors.same_color(coll.get_facecolor(), fcolor)
178182

179183
# Plural alias should also work
180-
colls = ax.stackplot(x, y1, y2, facecolors=['r', 'b'], colors=['c', 'm'])
184+
colls = ax.stackplot(x, y1, y2, facecolors=facecolors, colors=['c', 'm'])
181185
for coll, fcolor in zip(colls, facecolors):
182186
assert mcolors.same_color(coll.get_facecolor(), fcolor)
183187

184188
Assert values rather than visual results when feasible. This is clearer,
185-
less expensive and less fragile than comparing images, e.g. ::
189+
less computationally expensive and less fragile than comparing images, e.g. ::
186190

187191
def test_savefig_preserve_layout_engine():
188192
fig = plt.figure(layout='compressed')

0 commit comments

Comments
 (0)