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

Skip to content

Commit 696b031

Browse files
committed
TST: Capture all internal warnings.
These are either deprecations, or checks for old, but probably incorrect, behaviour.
1 parent 5417f2d commit 696b031

5 files changed

Lines changed: 35 additions & 16 deletions

File tree

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,13 +1677,19 @@ def _as_mpl_axes(self):
16771677
ax_via_gca = plt.gca(projection=prj)
16781678
assert ax_via_gca is ax
16791679
# try getting the axes given a different polar projection
1680-
ax_via_gca = plt.gca(projection=prj2)
1680+
with pytest.warns(UserWarning) as rec:
1681+
ax_via_gca = plt.gca(projection=prj2)
1682+
assert len(rec) == 1
1683+
assert 'Requested projection is different' in str(rec[0].message)
16811684
assert ax_via_gca is not ax
16821685
assert ax.get_theta_offset() == 0, ax.get_theta_offset()
16831686
assert ax_via_gca.get_theta_offset() == np.pi, \
16841687
ax_via_gca.get_theta_offset()
16851688
# try getting the axes given an == (not is) polar projection
1686-
ax_via_gca = plt.gca(projection=prj3)
1689+
with pytest.warns(UserWarning):
1690+
ax_via_gca = plt.gca(projection=prj3)
1691+
assert len(rec) == 1
1692+
assert 'Requested projection is different' in str(rec[0].message)
16871693
assert ax_via_gca is ax
16881694
plt.close()
16891695

lib/matplotlib/tests/test_cbook.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ def test_is_hashable():
2929

3030
def test_restrict_dict():
3131
d = {'foo': 'bar', 1: 2}
32-
d1 = cbook.restrict_dict(d, ['foo', 1])
33-
assert d1 == d
34-
d2 = cbook.restrict_dict(d, ['bar', 2])
35-
assert d2 == {}
36-
d3 = cbook.restrict_dict(d, {'foo': 1})
37-
assert d3 == {'foo': 'bar'}
38-
d4 = cbook.restrict_dict(d, {})
39-
assert d4 == {}
40-
d5 = cbook.restrict_dict(d, {'foo', 2})
41-
assert d5 == {'foo': 'bar'}
32+
with pytest.warns(cbook.deprecation.MatplotlibDeprecationWarning) as rec:
33+
d1 = cbook.restrict_dict(d, ['foo', 1])
34+
assert d1 == d
35+
d2 = cbook.restrict_dict(d, ['bar', 2])
36+
assert d2 == {}
37+
d3 = cbook.restrict_dict(d, {'foo': 1})
38+
assert d3 == {'foo': 'bar'}
39+
d4 = cbook.restrict_dict(d, {})
40+
assert d4 == {}
41+
d5 = cbook.restrict_dict(d, {'foo', 2})
42+
assert d5 == {'foo': 'bar'}
43+
assert len(rec) == 5
4244
# check that d was not modified
4345
assert d == {'foo': 'bar', 1: 2}
4446

lib/matplotlib/tests/test_colors.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def test_tableau_order():
690690
assert list(mcolors.TABLEAU_COLORS.values()) == dflt_cycle
691691

692692

693-
def test_ndarray_subclass_norm():
693+
def test_ndarray_subclass_norm(recwarn):
694694
# Emulate an ndarray subclass that handles units
695695
# which objects when adding or subtracting with other
696696
# arrays. See #6622 and #8696
@@ -707,3 +707,11 @@ def __add__(self, other):
707707
mcolors.SymLogNorm(3, vmax=5, linscale=1),
708708
mcolors.PowerNorm(1)]:
709709
assert_array_equal(norm(data.view(MyArray)), norm(data))
710+
if isinstance(norm, mcolors.PowerNorm):
711+
assert len(recwarn) == 1
712+
warn = recwarn.pop(UserWarning)
713+
assert ('Power-law scaling on negative values is ill-defined'
714+
in str(warn.message))
715+
else:
716+
assert len(recwarn) == 0
717+
recwarn.clear()

lib/matplotlib/tests/test_dates.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ def test_too_many_date_ticks():
9696
tf = datetime.datetime(2000, 1, 20)
9797
fig = plt.figure()
9898
ax = fig.add_subplot(1, 1, 1)
99-
ax.set_xlim((t0, tf), auto=True)
99+
with pytest.warns(UserWarning) as rec:
100+
ax.set_xlim((t0, tf), auto=True)
101+
assert len(rec) == 1
102+
assert 'Attempting to set identical left==right' in str(rec[0].message)
100103
ax.plot([], [])
101104
ax.xaxis.set_major_locator(mdates.DayLocator())
102105
with pytest.raises(RuntimeError):

lib/matplotlib/tests/test_image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ def test_load_from_url():
603603

604604
@image_comparison(baseline_images=['log_scale_image'],
605605
remove_text=True)
606-
def test_log_scale_image():
606+
# The recwarn fixture captures a warning in image_comparison.
607+
def test_log_scale_image(recwarn):
607608
Z = np.zeros((10, 10))
608609
Z[::2] = 1
609610

@@ -615,7 +616,6 @@ def test_log_scale_image():
615616
ax.set_yscale('log')
616617

617618

618-
619619
@image_comparison(baseline_images=['rotate_image'],
620620
remove_text=True)
621621
def test_rotate_image():

0 commit comments

Comments
 (0)