Skip to content

Commit

Permalink
Use pytest fixture+marker instead of switch_backend.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
QuLogic committed Jan 30, 2017
1 parent 0d01304 commit 05f4b16
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,33 @@ def mpl_test_settings(request):
original_units_registry = matplotlib.units.registry.copy()
original_settings = matplotlib.rcParams.copy()

backend = None
backend_marker = request.keywords.get('backend')
if backend_marker is not None:
assert len(backend_marker.args) == 1, \
"Marker 'backend' must specify 1 backend."
backend = backend_marker.args[0]
prev_backend = matplotlib.get_backend()

style = 'classic'
style_marker = request.keywords.get('style')
if style_marker is not None:
assert len(style_marker.args) == 1, \
"Marker 'style' must specify 1 style."
style = style_marker.args[0]

matplotlib.testing.setup()
if backend is not None:
# This import must come after setup() so it doesn't load the default
# backend prematurely.
import matplotlib.pyplot as plt
plt.switch_backend(backend)
matplotlib.style.use(style)
try:
yield
finally:
if backend is not None:
import matplotlib.pyplot as plt
plt.switch_backend(prev_backend)
_do_cleanup(original_units_registry,
original_settings)
26 changes: 13 additions & 13 deletions lib/matplotlib/tests/test_backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import matplotlib.pyplot as plt
from matplotlib.compat import subprocess
from matplotlib.testing.compare import compare_images, ImageComparisonFailure
from matplotlib.testing.decorators import _image_directories, switch_backend
from matplotlib.testing.decorators import _image_directories

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

Expand Down Expand Up @@ -82,8 +82,8 @@ def create_figure():

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

# test compiling a figure to pdf with pdflatex
@needs_pdflatex
@pytest.mark.style('classic')
@switch_backend('pgf')
@pytest.mark.style('default')
@pytest.mark.backend('pgf')
def test_pdflatex():
import os
if os.environ.get('APPVEYOR', False):
Expand All @@ -116,8 +116,8 @@ def test_pdflatex():
# test updating the rc parameters for each figure
@needs_xelatex
@needs_pdflatex
@pytest.mark.style('classic')
@switch_backend('pgf')
@pytest.mark.style('default')
@pytest.mark.backend('pgf')
def test_rcupdate():
rc_sets = []
rc_sets.append({'font.family': 'sans-serif',
Expand Down Expand Up @@ -147,8 +147,8 @@ def test_rcupdate():

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

# test mixed mode rendering
@needs_xelatex
@pytest.mark.style('classic')
@switch_backend('pgf')
@pytest.mark.style('default')
@pytest.mark.backend('pgf')
def test_mixedmode():
rc_xelatex = {'font.family': 'serif',
'pgf.rcfonts': False}
Expand All @@ -179,8 +179,8 @@ def test_mixedmode():

# test bbox_inches clipping
@needs_xelatex
@pytest.mark.style('classic')
@switch_backend('pgf')
@pytest.mark.style('default')
@pytest.mark.backend('pgf')
def test_bbox_inches():
rc_xelatex = {'font.family': 'serif',
'pgf.rcfonts': False}
Expand Down
5 changes: 2 additions & 3 deletions lib/matplotlib/tests/test_backend_qt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
unicode_literals)

from matplotlib import pyplot as plt
from matplotlib.testing.decorators import switch_backend
from matplotlib._pylab_helpers import Gcf
import matplotlib
import copy
Expand Down Expand Up @@ -34,7 +33,7 @@
pytestmark = pytest.mark.xfail(reason='Qt4 is not available')


@switch_backend('Qt4Agg')
@pytest.mark.backend('Qt4Agg')
def test_fig_close():
# save the state of Gcf.figs
init_figs = copy.copy(Gcf.figs)
Expand Down Expand Up @@ -83,7 +82,7 @@ def test_fig_close():
'non_unicode_key',
]
)
@switch_backend('Qt4Agg')
@pytest.mark.backend('Qt4Agg')
def test_correct_key(qt_key, qt_mods, answer):
"""
Make a figure
Expand Down
5 changes: 2 additions & 3 deletions lib/matplotlib/tests/test_backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
unicode_literals)

from matplotlib import pyplot as plt
from matplotlib.testing.decorators import switch_backend
from matplotlib._pylab_helpers import Gcf
import matplotlib
import copy
Expand All @@ -27,7 +26,7 @@
_, ShiftModifier, ShiftKey = MODIFIER_KEYS[SHIFT]


@switch_backend('Qt5Agg')
@pytest.mark.backend('Qt5Agg')
def test_fig_close():
# save the state of Gcf.figs
init_figs = copy.copy(Gcf.figs)
Expand Down Expand Up @@ -76,7 +75,7 @@ def test_fig_close():
'non_unicode_key',
]
)
@switch_backend('Qt5Agg')
@pytest.mark.backend('Qt5Agg')
def test_correct_key(qt_key, qt_mods, answer):
"""
Make a figure
Expand Down

0 comments on commit 05f4b16

Please sign in to comment.