Skip to content

Commit eef58c5

Browse files
committed
Use pytest fixture+marker instead of switch_backend.
Non default backends can be specified by marking test functions with `@pytest.mark.backend('backend')`. Note that because switch_backend calls `matplotlib.testing.setup`, and it appears _below_ the previous `@cleanup` decorator in the PGF tests, the specified 'classic' style is not actually used for those tests.
1 parent a6ca70d commit eef58c5

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

lib/matplotlib/tests/conftest.py

+17
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,33 @@ def mpl_test_settings(request):
1313
original_units_registry = matplotlib.units.registry.copy()
1414
original_settings = matplotlib.rcParams.copy()
1515

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+
1624
style = 'classic'
1725
style_marker = request.keywords.get('style')
1826
if style_marker is not None:
1927
assert len(style_marker.args) == 1, \
2028
"Marker 'style' must specify 1 style."
2129
style = style_marker.args[0]
2230

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)
2337
matplotlib.style.use(style)
2438
try:
2539
yield
2640
finally:
41+
if backend is not None:
42+
import matplotlib.pyplot as plt
43+
plt.switch_backend(prev_backend)
2744
_do_cleanup(original_units_registry,
2845
original_settings)

lib/matplotlib/tests/test_backend_pgf.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import matplotlib.pyplot as plt
1313
from matplotlib.compat import subprocess
1414
from matplotlib.testing.compare import compare_images, ImageComparisonFailure
15-
from matplotlib.testing.decorators import _image_directories, switch_backend
15+
from matplotlib.testing.decorators import _image_directories
1616

1717
baseline_dir, result_dir = _image_directories(lambda: 'dummy func')
1818

@@ -82,8 +82,8 @@ def create_figure():
8282

8383
# test compiling a figure to pdf with xelatex
8484
@needs_xelatex
85-
@pytest.mark.style('classic')
86-
@switch_backend('pgf')
85+
@pytest.mark.style('default')
86+
@pytest.mark.backend('pgf')
8787
def test_xelatex():
8888
rc_xelatex = {'font.family': 'serif',
8989
'pgf.rcfonts': False}
@@ -94,8 +94,8 @@ def test_xelatex():
9494

9595
# test compiling a figure to pdf with pdflatex
9696
@needs_pdflatex
97-
@pytest.mark.style('classic')
98-
@switch_backend('pgf')
97+
@pytest.mark.style('default')
98+
@pytest.mark.backend('pgf')
9999
def test_pdflatex():
100100
import os
101101
if os.environ.get('APPVEYOR', False):
@@ -116,8 +116,8 @@ def test_pdflatex():
116116
# test updating the rc parameters for each figure
117117
@needs_xelatex
118118
@needs_pdflatex
119-
@pytest.mark.style('classic')
120-
@switch_backend('pgf')
119+
@pytest.mark.style('default')
120+
@pytest.mark.backend('pgf')
121121
def test_rcupdate():
122122
rc_sets = []
123123
rc_sets.append({'font.family': 'sans-serif',
@@ -147,8 +147,8 @@ def test_rcupdate():
147147

148148
# test backend-side clipping, since large numbers are not supported by TeX
149149
@needs_xelatex
150-
@pytest.mark.style('classic')
151-
@switch_backend('pgf')
150+
@pytest.mark.style('default')
151+
@pytest.mark.backend('pgf')
152152
def test_pathclip():
153153
rc_xelatex = {'font.family': 'serif',
154154
'pgf.rcfonts': False}
@@ -164,8 +164,8 @@ def test_pathclip():
164164

165165
# test mixed mode rendering
166166
@needs_xelatex
167-
@pytest.mark.style('classic')
168-
@switch_backend('pgf')
167+
@pytest.mark.style('default')
168+
@pytest.mark.backend('pgf')
169169
def test_mixedmode():
170170
rc_xelatex = {'font.family': 'serif',
171171
'pgf.rcfonts': False}
@@ -179,8 +179,8 @@ def test_mixedmode():
179179

180180
# test bbox_inches clipping
181181
@needs_xelatex
182-
@pytest.mark.style('classic')
183-
@switch_backend('pgf')
182+
@pytest.mark.style('default')
183+
@pytest.mark.backend('pgf')
184184
def test_bbox_inches():
185185
rc_xelatex = {'font.family': 'serif',
186186
'pgf.rcfonts': False}

lib/matplotlib/tests/test_backend_qt4.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
unicode_literals)
33

44
from matplotlib import pyplot as plt
5-
from matplotlib.testing.decorators import switch_backend
65
from matplotlib._pylab_helpers import Gcf
76
import matplotlib
87
import copy
@@ -34,7 +33,7 @@
3433
pytestmark = pytest.mark.xfail(reason='Qt4 is not available')
3534

3635

37-
@switch_backend('Qt4Agg')
36+
@pytest.mark.backend('Qt4Agg')
3837
def test_fig_close():
3938
# save the state of Gcf.figs
4039
init_figs = copy.copy(Gcf.figs)
@@ -83,7 +82,7 @@ def test_fig_close():
8382
'non_unicode_key',
8483
]
8584
)
86-
@switch_backend('Qt4Agg')
85+
@pytest.mark.backend('Qt4Agg')
8786
def test_correct_key(qt_key, qt_mods, answer):
8887
"""
8988
Make a figure

lib/matplotlib/tests/test_backend_qt5.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
unicode_literals)
33

44
from matplotlib import pyplot as plt
5-
from matplotlib.testing.decorators import switch_backend
65
from matplotlib._pylab_helpers import Gcf
76
import matplotlib
87
import copy
@@ -27,7 +26,7 @@
2726
_, ShiftModifier, ShiftKey = MODIFIER_KEYS[SHIFT]
2827

2928

30-
@switch_backend('Qt5Agg')
29+
@pytest.mark.backend('Qt5Agg')
3130
def test_fig_close():
3231
# save the state of Gcf.figs
3332
init_figs = copy.copy(Gcf.figs)
@@ -76,7 +75,7 @@ def test_fig_close():
7675
'non_unicode_key',
7776
]
7877
)
79-
@switch_backend('Qt5Agg')
78+
@pytest.mark.backend('Qt5Agg')
8079
def test_correct_key(qt_key, qt_mods, answer):
8180
"""
8281
Make a figure

0 commit comments

Comments
 (0)