Skip to content

Convert test decorators to pytest fixtures #7973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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.
  • Loading branch information
QuLogic committed Jan 30, 2017
commit 05f4b16c3e4e28500d9e87192b1a2e1a68ee2553
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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @QuLogic
This is the place were I noticed that we were changing stylesheet. Can you confirm this is not normal?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or explain to me why it runs fine :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so @dopplershift explained it to me, and I now understand.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For those reading along at home, the outer decorator that was here was being ignored and the last time we generated the images for these test we accidentally switched to the default style.

This was 'fixed' as part of the pytest migration so we need to switch the mark to 'default' style.

@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