9
9
import pytest
10
10
11
11
import matplotlib as mpl
12
- from matplotlib import pyplot as plt
12
+ from matplotlib import pyplot as plt , rc_context
13
13
from matplotlib import animation
14
14
15
15
@@ -136,27 +136,14 @@ def isAvailable(cls):
136
136
(writer ,Path (output ))for writer ,output in WRITER_OUTPUT ]
137
137
138
138
139
- # Smoke test for saving animations. In the future, we should probably
140
- # design more sophisticated tests which compare resulting frames a-la
141
- # matplotlib.testing.image_comparison
142
- @pytest .mark .parametrize ('writer, output' ,WRITER_OUTPUT )
143
- def test_save_animation_smoketest (tmpdir ,writer ,output ):
144
- if not animation .writers .is_available (writer ):
145
- pytest .skip ("writer '%s' not available on this system" % writer )
139
+ @pytest .fixture ()
140
+ def simple_animation ():
146
141
fig ,ax = plt .subplots ()
147
142
line ,= ax .plot ([], [])
148
143
149
144
ax .set_xlim (0 ,10 )
150
145
ax .set_ylim (- 1 ,1 )
151
146
152
- dpi = None
153
- codec = None
154
- if writer == 'ffmpeg' :
155
- # Issue #8253
156
- fig .set_size_inches ((10.85 ,9.21 ))
157
- dpi = 100.
158
- codec = 'h264'
159
-
160
147
def init ():
161
148
line .set_data ([], [])
162
149
return line ,
@@ -167,14 +154,59 @@ def animate(i):
167
154
line .set_data (x ,y )
168
155
return line ,
169
156
157
+ return dict (fig = fig ,func = animate ,init_func = init ,frames = 5 )
158
+
159
+
160
+ # Smoke test for saving animations. In the future, we should probably
161
+ # design more sophisticated tests which compare resulting frames a-la
162
+ # matplotlib.testing.image_comparison
163
+ @pytest .mark .parametrize ('writer, output' ,WRITER_OUTPUT )
164
+ def test_save_animation_smoketest (tmpdir ,writer ,output ,simple_animation ):
165
+ if not animation .writers .is_available (writer ):
166
+ pytest .skip ("writer '%s' not available on this system" % writer )
167
+
168
+ dpi = None
169
+ codec = None
170
+ if writer == 'ffmpeg' :
171
+ # Issue #8253
172
+ simple_animation ['fig' ].set_size_inches ((10.85 ,9.21 ))
173
+ dpi = 100.
174
+ codec = 'h264'
175
+
170
176
# Use temporary directory for the file-based writers, which produce a file
171
177
# per frame with known names.
172
178
with tmpdir .as_cwd ():
173
- anim = animation .FuncAnimation (fig , animate , init_func = init , frames = 5 )
179
+ anim = animation .FuncAnimation (** simple_animation )
174
180
anim .save (output ,fps = 30 ,writer = writer ,bitrate = 500 ,dpi = dpi ,
175
181
codec = codec )
176
182
177
183
184
+ @pytest .mark .parametrize ('writer' , [
185
+ pytest .param ('ffmpeg' ,
186
+ marks = pytest .mark .skipif (
187
+ not animation .FFMpegWriter .isAvailable (),
188
+ reason = 'Requires FFMpeg' )),
189
+ pytest .param ('imagemagick' ,
190
+ marks = pytest .mark .skipif (
191
+ not animation .ImageMagickWriter .isAvailable (),
192
+ reason = 'Requires ImageMagick' )),
193
+ ])
194
+ @pytest .mark .parametrize ('html, want' , [
195
+ ('none' ,None ),
196
+ ('html5' ,'<video width' ),
197
+ ('jshtml' ,'<script ' )
198
+ ])
199
+ def test_animation_repr_html (writer ,html ,want ,simple_animation ):
200
+ anim = animation .FuncAnimation (** simple_animation )
201
+ with plt .rc_context ({'animation.writer' :writer ,
202
+ 'animation.html' :html }):
203
+ html = anim ._repr_html_ ()
204
+ if want is None :
205
+ assert html is None
206
+ else :
207
+ assert want in html
208
+
209
+
178
210
def test_no_length_frames ():
179
211
(make_animation (frames = iter (range (5 )))
180
212
.save ('unused.null' ,writer = NullMovieWriter ()))