22
33import numpy as np
44from numpy .testing import assert_array_equal
5+ import pytest
56
67from matplotlib .testing .decorators import image_comparison
78import matplotlib .pyplot as plt
@@ -267,50 +268,36 @@ def _subplots():
267268
268269
269270def test_empty_layout ():
270- """Tests that tight layout doesn't cause an error when there are
271- no axes.
272- """
273-
271+ """Test that tight layout doesn't cause an error when there are no axes."""
274272 fig = plt .gcf ()
275273 fig .tight_layout ()
276274
277275
278- def test_verybig_decorators_horizontal ():
279- "Test that warning emitted when xlabel too big"
280- fig , ax = plt .subplots (figsize = (3 , 2 ))
281- ax .set_xlabel ('a' * 100 )
282- with warnings .catch_warnings (record = True ) as w :
283- fig .tight_layout ()
284- assert len (w ) == 1
285-
286-
287- def test_verybig_decorators_vertical ():
288- "Test that warning emitted when xlabel too big"
276+ @pytest .mark .parametrize ("label" , ["xlabel" , "ylabel" ])
277+ def test_verybig_decorators (label ):
278+ """Test that warning emitted when xlabel/ylabel too big."""
289279 fig , ax = plt .subplots (figsize = (3 , 2 ))
290- ax .set_ylabel ( 'a' * 100 )
291- with warnings . catch_warnings ( record = True ) as w :
280+ ax .set ( ** { label : 'a' * 100 } )
281+ with pytest . warns ( UserWarning ) :
292282 fig .tight_layout ()
293- assert len (w ) == 1
294283
295284
296285def test_big_decorators_horizontal ():
297286 "Test that warning emitted when xlabel too big"
298287 fig , axs = plt .subplots (1 , 2 , figsize = (3 , 2 ))
299288 axs [0 ].set_xlabel ('a' * 30 )
300289 axs [1 ].set_xlabel ('b' * 30 )
301- with warnings . catch_warnings ( record = True ) as w :
290+ with pytest . warns ( UserWarning ) :
302291 fig .tight_layout ()
303- assert len (w ) == 1
304292
305293
306294def test_big_decorators_vertical ():
307295 "Test that warning emitted when xlabel too big"
308296 fig , axs = plt .subplots (2 , 1 , figsize = (3 , 2 ))
309297 axs [0 ].set_ylabel ('a' * 20 )
310298 axs [1 ].set_ylabel ('b' * 20 )
311- with warnings . catch_warnings ( record = True ) as w :
299+ with pytest . warns ( UserWarning ) :
312300 fig .tight_layout ()
313- assert len (w ) == 1
314301
315302
316303def test_badsubplotgrid ():
@@ -319,9 +306,8 @@ def test_badsubplotgrid():
319306 ax1 = plt .subplot2grid ((4 , 5 ), (0 , 0 ))
320307 # this is the bad entry:
321308 ax5 = plt .subplot2grid ((5 , 5 ), (0 , 3 ), colspan = 3 , rowspan = 5 )
322- with warnings . catch_warnings ( record = True ) as w :
309+ with pytest . warns ( UserWarning ) :
323310 plt .tight_layout ()
324- assert len (w ) == 1
325311
326312
327313def test_collapsed ():
@@ -333,8 +319,7 @@ def test_collapsed():
333319
334320 ax .annotate ('BIG LONG STRING' , xy = (1.25 , 2 ), xytext = (10.5 , 1.75 ),)
335321 p1 = ax .get_position ()
336- with warnings . catch_warnings ( record = True ) as w :
322+ with pytest . warns ( UserWarning ) :
337323 plt .tight_layout ()
338324 p2 = ax .get_position ()
339325 assert p1 .width == p2 .width
340- assert len (w ) == 1
0 commit comments