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

Skip to content

Commit f84d715

Browse files
authored
Merge pull request #8567 from QuLogic/pytest-parametrize
Minor pytest parametrization
2 parents dc9c93d + 8083562 commit f84d715

8 files changed

Lines changed: 79 additions & 113 deletions

File tree

lib/matplotlib/tests/test_backend_ps.py

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,26 @@
2727
reason="This test needs a TeX installation")
2828

2929

30-
def _test_savefig_to_stringio(format='ps', use_log=False):
30+
@pytest.mark.parametrize('format, use_log, rcParams', [
31+
('ps', False, {}),
32+
needs_ghostscript(('ps', False, {'ps.usedistiller': 'ghostscript'})),
33+
needs_tex(needs_ghostscript(('ps', False, {'text.latex.unicode': True,
34+
'text.usetex': True}))),
35+
('eps', False, {}),
36+
('eps', True, {'ps.useafm': True}),
37+
needs_tex(needs_ghostscript(('eps', False, {'text.latex.unicode': True,
38+
'text.usetex': True}))),
39+
], ids=[
40+
'ps',
41+
'ps with distiller',
42+
'ps with usetex',
43+
'eps',
44+
'eps afm',
45+
'eps with usetex'
46+
])
47+
def test_savefig_to_stringio(format, use_log, rcParams):
48+
matplotlib.rcParams.update(rcParams)
49+
3150
fig, ax = plt.subplots()
3251
buffers = [
3352
six.moves.StringIO(),
@@ -60,41 +79,6 @@ def _test_savefig_to_stringio(format='ps', use_log=False):
6079
buffer.close()
6180

6281

63-
def test_savefig_to_stringio():
64-
_test_savefig_to_stringio()
65-
66-
67-
@needs_ghostscript
68-
def test_savefig_to_stringio_with_distiller():
69-
matplotlib.rcParams['ps.usedistiller'] = 'ghostscript'
70-
_test_savefig_to_stringio()
71-
72-
73-
@needs_tex
74-
@needs_ghostscript
75-
def test_savefig_to_stringio_with_usetex():
76-
matplotlib.rcParams['text.latex.unicode'] = True
77-
matplotlib.rcParams['text.usetex'] = True
78-
_test_savefig_to_stringio()
79-
80-
81-
def test_savefig_to_stringio_eps():
82-
_test_savefig_to_stringio(format='eps')
83-
84-
85-
def test_savefig_to_stringio_eps_afm():
86-
matplotlib.rcParams['ps.useafm'] = True
87-
_test_savefig_to_stringio(format='eps', use_log=True)
88-
89-
90-
@needs_tex
91-
@needs_ghostscript
92-
def test_savefig_to_stringio_with_usetex_eps():
93-
matplotlib.rcParams['text.latex.unicode'] = True
94-
matplotlib.rcParams['text.usetex'] = True
95-
_test_savefig_to_stringio(format='eps')
96-
97-
9882
def test_composite_image():
9983
# Test that figures can be saved with and without combining multiple images
10084
# (on a single set of axes) into a single composite image.

lib/matplotlib/tests/test_colorbar.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
unicode_literals)
33

44
import numpy as np
5+
import pytest
6+
57
from matplotlib import rc_context
68
from matplotlib.testing.decorators import image_comparison
79
import matplotlib.pyplot as plt
@@ -207,7 +209,9 @@ def test_colorbar_single_scatter():
207209
plt.colorbar(cs)
208210

209211

210-
def _test_remove_from_figure(use_gridspec):
212+
@pytest.mark.parametrize('use_gridspec', [False, True],
213+
ids=['no gridspec', 'with gridspec'])
214+
def test_remove_from_figure(use_gridspec):
211215
"""
212216
Test `remove_from_figure` with the specified ``use_gridspec`` setting
213217
"""
@@ -224,22 +228,6 @@ def _test_remove_from_figure(use_gridspec):
224228
assert (pre_figbox == post_figbox).all()
225229

226230

227-
def test_remove_from_figure_with_gridspec():
228-
"""
229-
Make sure that `remove_from_figure` removes the colorbar and properly
230-
restores the gridspec
231-
"""
232-
_test_remove_from_figure(True)
233-
234-
235-
def test_remove_from_figure_no_gridspec():
236-
"""
237-
Make sure that `remove_from_figure` removes a colorbar that was created
238-
without modifying the gridspec
239-
"""
240-
_test_remove_from_figure(False)
241-
242-
243231
def test_colorbarbase():
244232
# smoke test from #3805
245233
ax = plt.gca()

lib/matplotlib/tests/test_colors.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import numpy as np
1010
import pytest
1111

12-
from numpy.testing import assert_equal
1312
from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
1413

1514
from matplotlib import cycler
@@ -160,25 +159,25 @@ def test_PowerNorm():
160159
expected = [0, 0, 1/16, 1/4, 1]
161160
pnorm = mcolors.PowerNorm(2, vmin=0, vmax=8)
162161
assert_array_almost_equal(pnorm(a), expected)
163-
assert_equal(pnorm(a[0]), expected[0])
164-
assert_equal(pnorm(a[2]), expected[2])
162+
assert pnorm(a[0]) == expected[0]
163+
assert pnorm(a[2]) == expected[2]
165164
assert_array_almost_equal(a[1:], pnorm.inverse(pnorm(a))[1:])
166165

167166
# Clip = True
168167
a = np.array([-0.5, 0, 1, 8, 16], dtype=float)
169168
expected = [0, 0, 0, 1, 1]
170169
pnorm = mcolors.PowerNorm(2, vmin=2, vmax=8, clip=True)
171170
assert_array_almost_equal(pnorm(a), expected)
172-
assert_equal(pnorm(a[0]), expected[0])
173-
assert_equal(pnorm(a[-1]), expected[-1])
171+
assert pnorm(a[0]) == expected[0]
172+
assert pnorm(a[-1]) == expected[-1]
174173

175174
# Clip = True at call time
176175
a = np.array([-0.5, 0, 1, 8, 16], dtype=float)
177176
expected = [0, 0, 0, 1, 1]
178177
pnorm = mcolors.PowerNorm(2, vmin=2, vmax=8, clip=False)
179178
assert_array_almost_equal(pnorm(a, clip=True), expected)
180-
assert_equal(pnorm(a[0], clip=True), expected[0])
181-
assert_equal(pnorm(a[-1], clip=True), expected[-1])
179+
assert pnorm(a[0], clip=True) == expected[0]
180+
assert pnorm(a[-1], clip=True) == expected[-1]
182181

183182

184183
def test_Normalize():
@@ -652,14 +651,14 @@ def test_conversions():
652651
mcolors.to_rgba_array([".2", ".5", ".8"]),
653652
np.vstack([mcolors.to_rgba(c) for c in [".2", ".5", ".8"]]))
654653
# alpha is properly set.
655-
assert_equal(mcolors.to_rgba((1, 1, 1), .5), (1, 1, 1, .5))
656-
assert_equal(mcolors.to_rgba(".1", .5), (.1, .1, .1, .5))
654+
assert mcolors.to_rgba((1, 1, 1), .5) == (1, 1, 1, .5)
655+
assert mcolors.to_rgba(".1", .5) == (.1, .1, .1, .5)
657656
# builtin round differs between py2 and py3.
658-
assert_equal(mcolors.to_hex((.7, .7, .7)), "#b2b2b2")
657+
assert mcolors.to_hex((.7, .7, .7)) == "#b2b2b2"
659658
# hex roundtrip.
660659
hex_color = "#1234abcd"
661-
assert_equal(mcolors.to_hex(mcolors.to_rgba(hex_color), keep_alpha=True),
662-
hex_color)
660+
assert mcolors.to_hex(mcolors.to_rgba(hex_color), keep_alpha=True) == \
661+
hex_color
663662

664663

665664
def test_grey_gray():

lib/matplotlib/tests/test_compare_images.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import shutil
99
import warnings
1010

11-
from numpy.testing import assert_equal, assert_almost_equal
11+
from numpy.testing import assert_almost_equal
1212
import pytest
1313

1414
from matplotlib.testing.compare import compare_images
@@ -41,7 +41,7 @@ def image_comparison_expect_rms(im1, im2, tol, expect_rms):
4141
results = compare_images(im1, im2, tol=tol, in_decorator=True)
4242

4343
if expect_rms is None:
44-
assert_equal(None, results)
44+
assert results is None
4545
else:
4646
assert results is not None
4747
assert_almost_equal(expect_rms, results['rms'], decimal=4)

lib/matplotlib/tests/test_dates.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
except ImportError:
1818
import mock
1919

20-
from numpy.testing import assert_equal
21-
2220
from matplotlib.testing.decorators import image_comparison
2321
import matplotlib.pyplot as plt
2422
import matplotlib.dates as mdates
@@ -184,16 +182,16 @@ def test_strftime_fields(dt):
184182
minute=dt.minute,
185183
second=dt.second,
186184
microsecond=dt.microsecond))
187-
assert_equal(formatter.strftime(dt), formatted_date_str)
185+
assert formatter.strftime(dt) == formatted_date_str
188186

189187
try:
190188
# Test strftime("%x") with the current locale.
191189
import locale # Might not exist on some platforms, such as Windows
192190
locale_formatter = mdates.DateFormatter("%x")
193191
locale_d_fmt = locale.nl_langinfo(locale.D_FMT)
194192
expanded_formatter = mdates.DateFormatter(locale_d_fmt)
195-
assert_equal(locale_formatter.strftime(dt),
196-
expanded_formatter.strftime(dt))
193+
assert locale_formatter.strftime(dt) == \
194+
expanded_formatter.strftime(dt)
197195
except (ImportError, AttributeError):
198196
pass
199197

@@ -211,8 +209,7 @@ def test_date_formatter_callable():
211209

212210
formatter = mdates.AutoDateFormatter(locator)
213211
formatter.scaled[-10] = callable_formatting_function
214-
assert_equal(formatter([datetime.datetime(2014, 12, 25)]),
215-
['25-12//2014'])
212+
assert formatter([datetime.datetime(2014, 12, 25)]) == ['25-12//2014']
216213

217214

218215
def test_drange():
@@ -225,12 +222,12 @@ def test_drange():
225222
delta = datetime.timedelta(hours=1)
226223
# We expect 24 values in drange(start, end, delta), because drange returns
227224
# dates from an half open interval [start, end)
228-
assert_equal(24, len(mdates.drange(start, end, delta)))
225+
assert len(mdates.drange(start, end, delta)) == 24
229226

230227
# if end is a little bit later, we expect the range to contain one element
231228
# more
232229
end = end + datetime.timedelta(microseconds=1)
233-
assert_equal(25, len(mdates.drange(start, end, delta)))
230+
assert len(mdates.drange(start, end, delta)) == 25
234231

235232
# reset end
236233
end = datetime.datetime(2011, 1, 2, tzinfo=mdates.UTC)
@@ -239,8 +236,8 @@ def test_drange():
239236
# 4 hours = 1/6 day, this is an "dangerous" float
240237
delta = datetime.timedelta(hours=4)
241238
daterange = mdates.drange(start, end, delta)
242-
assert_equal(6, len(daterange))
243-
assert_equal(mdates.num2date(daterange[-1]), end - delta)
239+
assert len(daterange) == 6
240+
assert mdates.num2date(daterange[-1]) == (end - delta)
244241

245242

246243
def test_empty_date_with_year_formatter():
@@ -331,8 +328,7 @@ def _create_auto_date_locator(date1, date2):
331328
for t_delta, expected in results:
332329
d2 = d1 + t_delta
333330
locator = _create_auto_date_locator(d1, d2)
334-
assert_equal(list(map(str, mdates.num2date(locator()))),
335-
expected)
331+
assert list(map(str, mdates.num2date(locator()))) == expected
336332

337333

338334
@image_comparison(baseline_images=['date_inverted_limit'],
@@ -368,7 +364,7 @@ def _test_date2num_dst(date_range, tz_convert):
368364
expected_ordinalf = [735322.0 + (i * interval_days) for i in range(N)]
369365
actual_ordinalf = list(mdates.date2num(dt_bxl))
370366

371-
assert_equal(actual_ordinalf, expected_ordinalf)
367+
assert actual_ordinalf == expected_ordinalf
372368

373369

374370
def test_date2num_dst():

0 commit comments

Comments
 (0)