Skip to content

Commit

Permalink
Merge pull request #7973 from QuLogic/pytest-fixture
Browse files Browse the repository at this point in the history
TST: Convert test decorators to pytest fixtures
  • Loading branch information
tacaswell committed Feb 1, 2017
2 parents 6d3610b + 05f4b16 commit f858cc5
Show file tree
Hide file tree
Showing 49 changed files with 114 additions and 344 deletions.
1 change: 0 additions & 1 deletion lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,6 @@ def _jupyter_nbextension_paths():
default_test_modules = [
'matplotlib.tests.test_png',
'matplotlib.tests.test_units',
'matplotlib.tests.test_widgets',
]


Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/sphinxext/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from matplotlib.tests.conftest import mpl_test_settings
45 changes: 45 additions & 0 deletions lib/matplotlib/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import pytest

import matplotlib


@pytest.fixture(autouse=True)
def mpl_test_settings(request):
from matplotlib.testing.decorators import _do_cleanup

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)
7 changes: 1 addition & 6 deletions lib/matplotlib/tests/test_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
from matplotlib.image import imread
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.testing.decorators import cleanup, image_comparison
from matplotlib.testing.decorators import image_comparison
from matplotlib import pyplot as plt
from matplotlib import collections
from matplotlib import path
from matplotlib import transforms as mtransforms


@cleanup
def test_repeated_save_with_alpha():
# We want an image which has a background color of bluish green, with an
# alpha of 0.25.
Expand Down Expand Up @@ -51,7 +50,6 @@ def test_repeated_save_with_alpha():
decimal=3)


@cleanup
def test_large_single_path_collection():
buff = io.BytesIO()

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


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


@cleanup
def test_long_path():
buff = io.BytesIO()

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


@cleanup
def test_too_large_image():
fig = plt.figure(figsize=(300, 1000))
buff = io.BytesIO()
Expand Down
3 changes: 0 additions & 3 deletions lib/matplotlib/tests/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib.testing.decorators import cleanup


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


@cleanup
def test_no_length_frames():
fig, ax = plt.subplots()
line, = ax.plot([], [])
Expand Down
8 changes: 1 addition & 7 deletions lib/matplotlib/tests/test_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
import matplotlib.transforms as mtrans
import matplotlib.collections as mcollections
import matplotlib.artist as martist
from matplotlib.testing.decorators import image_comparison, cleanup
from matplotlib.testing.decorators import image_comparison


@cleanup
def test_patch_transform_of_none():
# tests the behaviour of patches added to an Axes with various transform
# specifications
Expand Down Expand Up @@ -60,7 +59,6 @@ def test_patch_transform_of_none():
assert e._transform == ax.transData


@cleanup
def test_collection_transform_of_none():
# tests the behaviour of collections added to an Axes with various
# transform specifications
Expand Down Expand Up @@ -127,7 +125,6 @@ def test_clipping():
ax1.set_ylim([-3, 3])


@cleanup
def test_cull_markers():
x = np.random.random(20000)
y = np.random.random(20000)
Expand Down Expand Up @@ -175,7 +172,6 @@ def test_hatching():
ax.set_ylim(0, 9)


@cleanup
def test_remove():
fig, ax = plt.subplots()
im = ax.imshow(np.arange(36).reshape(6, 6))
Expand Down Expand Up @@ -224,7 +220,6 @@ def test_default_edges():
ax4.add_patch(pp1)


@cleanup
def test_properties():
ln = mlines.Line2D([], [])
with warnings.catch_warnings(record=True) as w:
Expand All @@ -234,7 +229,6 @@ def test_properties():
assert len(w) == 0


@cleanup
def test_setp():
# Check empty list
plt.setp([])
Expand Down

0 comments on commit f858cc5

Please sign in to comment.