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

Skip to content

Commit d97d67d

Browse files
authored
Merge pull request #16528 from anntzer/failwarn
TST: Improve test failure messages on warnings.
2 parents 2b52ad5 + 335152f commit d97d67d

9 files changed

+25
-40
lines changed

lib/matplotlib/tests/test_artist.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,9 @@ def test_default_edges():
219219
ax4.add_patch(pp1)
220220

221221

222-
def test_properties(recwarn):
222+
def test_properties():
223223
ln = mlines.Line2D([], [])
224-
ln.properties()
225-
assert len(recwarn) == 0
224+
ln.properties() # Check that no warning is emitted.
226225

227226

228227
def test_setp():

lib/matplotlib/tests/test_axes.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4723,13 +4723,12 @@ def test_length_one_hist():
47234723
ax.hist([1])
47244724

47254725

4726-
def test_pathological_hexbin(recwarn):
4726+
def test_pathological_hexbin():
47274727
# issue #2863
47284728
mylist = [10] * 100
47294729
fig, ax = plt.subplots(1, 1)
47304730
ax.hexbin(mylist, mylist)
4731-
fig.savefig(io.BytesIO())
4732-
assert len(recwarn) == 0
4731+
fig.savefig(io.BytesIO()) # Check that no warning is emitted.
47334732

47344733

47354734
def test_color_None():

lib/matplotlib/tests/test_cbook.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,10 @@ def test_normalize_kwargs_fail(inp, kwargs_to_norm):
335335

336336
@pytest.mark.parametrize('inp, expected, kwargs_to_norm',
337337
pass_mapping)
338-
def test_normalize_kwargs_pass(inp, expected, kwargs_to_norm, recwarn):
338+
def test_normalize_kwargs_pass(inp, expected, kwargs_to_norm):
339339
with cbook._suppress_matplotlib_deprecation_warning():
340+
# No other warning should be emitted.
340341
assert expected == cbook.normalize_kwargs(inp, **kwargs_to_norm)
341-
assert len(recwarn) == 0
342342

343343

344344
def test_warn_external_frame_embedded_python():
@@ -574,13 +574,12 @@ def test_safe_first_element_pandas_series(pd):
574574
assert actual == 0
575575

576576

577-
def test_make_keyword_only(recwarn):
577+
def test_make_keyword_only():
578578
@cbook._make_keyword_only("3.0", "arg")
579579
def func(pre, arg, post=None):
580580
pass
581581

582-
func(1, arg=2)
583-
assert len(recwarn) == 0
582+
func(1, arg=2) # Check that no warning is emitted.
584583

585584
with pytest.warns(MatplotlibDeprecationWarning):
586585
func(1, 2)

lib/matplotlib/tests/test_colors.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ def test_tableau_order():
904904
assert list(mcolors.TABLEAU_COLORS.values()) == dflt_cycle
905905

906906

907-
def test_ndarray_subclass_norm(recwarn):
907+
def test_ndarray_subclass_norm():
908908
# Emulate an ndarray subclass that handles units
909909
# which objects when adding or subtracting with other
910910
# arrays. See #6622 and #8696
@@ -927,9 +927,7 @@ def __add__(self, other):
927927
assert_array_equal(norm(mydata), norm(data))
928928
fig, ax = plt.subplots()
929929
ax.imshow(mydata, norm=norm)
930-
fig.canvas.draw()
931-
assert len(recwarn) == 0
932-
recwarn.clear()
930+
fig.canvas.draw() # Check that no warning is emitted.
933931

934932

935933
def test_same_color():

lib/matplotlib/tests/test_contour.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,11 @@ def test_internal_cpp_api_2():
279279

280280
def test_circular_contour_warning():
281281
# Check that almost circular contours don't throw a warning
282-
with pytest.warns(None) as record:
283-
x, y = np.meshgrid(np.linspace(-2, 2, 4), np.linspace(-2, 2, 4))
284-
r = np.hypot(x, y)
285-
286-
plt.figure()
287-
cs = plt.contour(x, y, r)
288-
plt.clabel(cs)
289-
assert len(record) == 0
282+
x, y = np.meshgrid(np.linspace(-2, 2, 4), np.linspace(-2, 2, 4))
283+
r = np.hypot(x, y)
284+
plt.figure()
285+
cs = plt.contour(x, y, r)
286+
plt.clabel(cs)
290287

291288

292289
@pytest.mark.parametrize("use_clabeltext, contour_zorder, clabel_zorder",

lib/matplotlib/tests/test_image.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -975,9 +975,8 @@ def test_imshow_masked_interpolation():
975975
ax.axis('off')
976976

977977

978-
def test_imshow_no_warn_invalid(recwarn):
979-
plt.imshow([[1, 2], [3, np.nan]])
980-
assert len(recwarn) == 0
978+
def test_imshow_no_warn_invalid():
979+
plt.imshow([[1, 2], [3, np.nan]]) # Check that no warning is emitted.
981980

982981

983982
@pytest.mark.parametrize(

lib/matplotlib/tests/test_legend.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,4 @@ def test_no_warn_big_data_when_loc_specified():
570570
for idx in range(1000):
571571
ax.plot(np.arange(5000), label=idx)
572572
legend = ax.legend('best')
573-
with pytest.warns(None) as records:
574-
fig.draw_artist(legend)
575-
assert len(records) == 0
573+
fig.draw_artist(legend) # Check that no warning is emitted.

lib/matplotlib/tests/test_quiver.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,23 @@ def test_quiver_arg_sizes():
7171
plt.quiver(X2, X2, X2, X2, X3)
7272

7373

74-
def test_no_warnings(recwarn):
74+
def test_no_warnings():
7575
fig, ax = plt.subplots()
7676
X, Y = np.meshgrid(np.arange(15), np.arange(10))
7777
U = V = np.ones_like(X)
7878
phi = (np.random.rand(15, 10) - .5) * 150
7979
ax.quiver(X, Y, U, V, angles=phi)
80-
fig.canvas.draw()
81-
assert len(recwarn) == 0
80+
fig.canvas.draw() # Check that no warning is emitted.
8281

8382

84-
def test_zero_headlength(recwarn):
83+
def test_zero_headlength():
8584
# Based on report by Doug McNeil:
8685
# http://matplotlib.1069221.n5.nabble.com/quiver-warnings-td28107.html
8786
fig, ax = plt.subplots()
8887
X, Y = np.meshgrid(np.arange(10), np.arange(10))
8988
U, V = np.cos(X), np.sin(Y)
9089
ax.quiver(U, V, headlength=0, headaxislength=0)
91-
fig.canvas.draw()
92-
assert len(recwarn) == 0
90+
fig.canvas.draw() # Check that no warning is emitted.
9391

9492

9593
@image_comparison(['quiver_animated_test_image.png'])

lib/matplotlib/tests/test_rcparams.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,9 @@ def test_if_rctemplate_would_be_valid(tmpdir):
506506
fname = str(d.join('testrcvalid.temp'))
507507
with open(fname, "w") as f:
508508
f.writelines(newlines)
509-
with pytest.warns(None) as record:
510-
mpl.rc_params_from_file(fname,
511-
fail_on_error=True,
512-
use_default_template=False)
513-
assert len(record) == 0
509+
mpl.rc_params_from_file(fname,
510+
fail_on_error=True, # Test also fails on warning.
511+
use_default_template=False)
514512

515513

516514
@pytest.mark.skipif(sys.platform != "linux", reason="Linux only")

0 commit comments

Comments
 (0)