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

Skip to content

Commit 9cee283

Browse files
committed
TST: Add test for _repr_html_
1 parent 5584ba7 commit 9cee283

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

lib/matplotlib/tests/test_animation.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010

1111
import matplotlib as mpl
12-
from matplotlib import pyplot as plt
12+
from matplotlib import pyplot as plt, rc_context
1313
from matplotlib import animation
1414

1515

@@ -136,27 +136,14 @@ def isAvailable(cls):
136136
(writer, Path(output)) for writer, output in WRITER_OUTPUT]
137137

138138

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():
146141
fig, ax = plt.subplots()
147142
line, = ax.plot([], [])
148143

149144
ax.set_xlim(0, 10)
150145
ax.set_ylim(-1, 1)
151146

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-
160147
def init():
161148
line.set_data([], [])
162149
return line,
@@ -167,14 +154,59 @@ def animate(i):
167154
line.set_data(x, y)
168155
return line,
169156

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+
170176
# Use temporary directory for the file-based writers, which produce a file
171177
# per frame with known names.
172178
with tmpdir.as_cwd():
173-
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=5)
179+
anim = animation.FuncAnimation(**simple_animation)
174180
anim.save(output, fps=30, writer=writer, bitrate=500, dpi=dpi,
175181
codec=codec)
176182

177183

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+
178210
def test_no_length_frames():
179211
(make_animation(frames=iter(range(5)))
180212
.save('unused.null', writer=NullMovieWriter()))

0 commit comments

Comments
 (0)