Skip to content

Commit f858cc5

Browse files
authored
Merge pull request #7973 from QuLogic/pytest-fixture
TST: Convert test decorators to pytest fixtures
2 parents 6d3610b + 05f4b16 commit f858cc5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+114
-344
lines changed

lib/matplotlib/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,6 @@ def _jupyter_nbextension_paths():
14761476
default_test_modules = [
14771477
'matplotlib.tests.test_png',
14781478
'matplotlib.tests.test_units',
1479-
'matplotlib.tests.test_widgets',
14801479
]
14811480

14821481

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from __future__ import (absolute_import, division, print_function,
2+
unicode_literals)
3+
4+
from matplotlib.tests.conftest import mpl_test_settings

lib/matplotlib/tests/conftest.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from __future__ import (absolute_import, division, print_function,
2+
unicode_literals)
3+
4+
import pytest
5+
6+
import matplotlib
7+
8+
9+
@pytest.fixture(autouse=True)
10+
def mpl_test_settings(request):
11+
from matplotlib.testing.decorators import _do_cleanup
12+
13+
original_units_registry = matplotlib.units.registry.copy()
14+
original_settings = matplotlib.rcParams.copy()
15+
16+
backend = None
17+
backend_marker = request.keywords.get('backend')
18+
if backend_marker is not None:
19+
assert len(backend_marker.args) == 1, \
20+
"Marker 'backend' must specify 1 backend."
21+
backend = backend_marker.args[0]
22+
prev_backend = matplotlib.get_backend()
23+
24+
style = 'classic'
25+
style_marker = request.keywords.get('style')
26+
if style_marker is not None:
27+
assert len(style_marker.args) == 1, \
28+
"Marker 'style' must specify 1 style."
29+
style = style_marker.args[0]
30+
31+
matplotlib.testing.setup()
32+
if backend is not None:
33+
# This import must come after setup() so it doesn't load the default
34+
# backend prematurely.
35+
import matplotlib.pyplot as plt
36+
plt.switch_backend(backend)
37+
matplotlib.style.use(style)
38+
try:
39+
yield
40+
finally:
41+
if backend is not None:
42+
import matplotlib.pyplot as plt
43+
plt.switch_backend(prev_backend)
44+
_do_cleanup(original_units_registry,
45+
original_settings)

lib/matplotlib/tests/test_agg.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
from matplotlib.image import imread
1212
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
1313
from matplotlib.figure import Figure
14-
from matplotlib.testing.decorators import cleanup, image_comparison
14+
from matplotlib.testing.decorators import image_comparison
1515
from matplotlib import pyplot as plt
1616
from matplotlib import collections
1717
from matplotlib import path
1818
from matplotlib import transforms as mtransforms
1919

2020

21-
@cleanup
2221
def test_repeated_save_with_alpha():
2322
# We want an image which has a background color of bluish green, with an
2423
# alpha of 0.25.
@@ -51,7 +50,6 @@ def test_repeated_save_with_alpha():
5150
decimal=3)
5251

5352

54-
@cleanup
5553
def test_large_single_path_collection():
5654
buff = io.BytesIO()
5755

@@ -66,7 +64,6 @@ def test_large_single_path_collection():
6664
plt.savefig(buff)
6765

6866

69-
@cleanup
7067
def test_marker_with_nan():
7168
# This creates a marker with nans in it, which was segfaulting the
7269
# Agg backend (see #3722)
@@ -79,7 +76,6 @@ def test_marker_with_nan():
7976
fig.savefig(buf, format='png')
8077

8178

82-
@cleanup
8379
def test_long_path():
8480
buff = io.BytesIO()
8581

@@ -218,7 +214,6 @@ def process_image(self, padded_src, dpi):
218214
ax.yaxis.set_visible(False)
219215

220216

221-
@cleanup
222217
def test_too_large_image():
223218
fig = plt.figure(figsize=(300, 1000))
224219
buff = io.BytesIO()

lib/matplotlib/tests/test_animation.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import matplotlib as mpl
1414
from matplotlib import pyplot as plt
1515
from matplotlib import animation
16-
from matplotlib.testing.decorators import cleanup
1716

1817

1918
class NullMovieWriter(animation.AbstractMovieWriter):
@@ -109,7 +108,6 @@ def isAvailable(self):
109108
# Smoke test for saving animations. In the future, we should probably
110109
# design more sophisticated tests which compare resulting frames a-la
111110
# matplotlib.testing.image_comparison
112-
@cleanup
113111
@pytest.mark.parametrize('writer, extension', WRITER_OUTPUT)
114112
def test_save_animation_smoketest(tmpdir, writer, extension):
115113
try:
@@ -148,7 +146,6 @@ def animate(i):
148146
"see issues #1891 and #2679")
149147

150148

151-
@cleanup
152149
def test_no_length_frames():
153150
fig, ax = plt.subplots()
154151
line, = ax.plot([], [])

lib/matplotlib/tests/test_artist.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
import matplotlib.transforms as mtrans
1515
import matplotlib.collections as mcollections
1616
import matplotlib.artist as martist
17-
from matplotlib.testing.decorators import image_comparison, cleanup
17+
from matplotlib.testing.decorators import image_comparison
1818

1919

20-
@cleanup
2120
def test_patch_transform_of_none():
2221
# tests the behaviour of patches added to an Axes with various transform
2322
# specifications
@@ -60,7 +59,6 @@ def test_patch_transform_of_none():
6059
assert e._transform == ax.transData
6160

6261

63-
@cleanup
6462
def test_collection_transform_of_none():
6563
# tests the behaviour of collections added to an Axes with various
6664
# transform specifications
@@ -127,7 +125,6 @@ def test_clipping():
127125
ax1.set_ylim([-3, 3])
128126

129127

130-
@cleanup
131128
def test_cull_markers():
132129
x = np.random.random(20000)
133130
y = np.random.random(20000)
@@ -175,7 +172,6 @@ def test_hatching():
175172
ax.set_ylim(0, 9)
176173

177174

178-
@cleanup
179175
def test_remove():
180176
fig, ax = plt.subplots()
181177
im = ax.imshow(np.arange(36).reshape(6, 6))
@@ -224,7 +220,6 @@ def test_default_edges():
224220
ax4.add_patch(pp1)
225221

226222

227-
@cleanup
228223
def test_properties():
229224
ln = mlines.Line2D([], [])
230225
with warnings.catch_warnings(record=True) as w:
@@ -234,7 +229,6 @@ def test_properties():
234229
assert len(w) == 0
235230

236231

237-
@cleanup
238232
def test_setp():
239233
# Check empty list
240234
plt.setp([])

0 commit comments

Comments
 (0)