8000 Convert backend tests to use pytest. · matplotlib/matplotlib@d7732cb · GitHub
[go: up one dir, main page]

Skip to content

Commit d7732cb

Browse files
committed
Convert backend tests to use pytest.
1 parent bb4d920 commit d7732cb

File tree

8 files changed

+42
-100
lines changed

8 files changed

+42
-100
lines changed

lib/matplotlib/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,13 +1474,6 @@ def _jupyter_nbextension_paths():
14741474

14751475

14761476
default_test_modules = [
1477-
'matplotlib.tests.test_backend_bases',
1478-
'matplotlib.tests.test_backend_pdf',
1479-
'matplotlib.tests.test_backend_pgf',
1480-
'matplotlib.tests.test_backend_ps',
1481-
'matplotlib.tests.test_backend_qt4',
1482-
'matplotlib.tests.test_backend_qt5',
1483-
'matplotlib.tests.test_backend_svg',
14841477
'matplotlib.tests.test_coding_standards',
14851478
'matplotlib.tests.test_dviread',
14861479
'matplotlib.tests.test_figure',

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import matplotlib.transforms as transforms
77
import matplotlib.path as path
88

9-
from nose.tools import assert_equal
10-
119
import numpy as np
1210
import os
1311
import shutil
@@ -63,7 +61,7 @@ def test_get_default_filename():
6361
fig = plt.figure()
6462
canvas = FigureCanvasBase(fig)
6563
filename = canvas.get_default_filename()
66-
assert_equal(filename, 'image.png')
64+
assert filename == 'image.png'
6765
finally:
6866
shutil.rmtree(test_dir)
6967

@@ -81,10 +79,6 @@ def test_get_default_filename_already_exists():
8179
open(os.path.join(test_dir, 'image.png'), 'w').close()
8280

8381
filename = canvas.get_default_filename()
84-
assert_equal(filename, 'image-1.png')
82+
assert filename == 'image-1.png'
8583
finally:
8684
shutil.rmtree(test_dir)
87-
88-
if __name__ == "__main__":
89-
import nose
90-
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/tests/test_backend_pdf.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
import os
1010
import tempfile
1111

12-
try:
13-
from unittest.mock import patch
14-
except ImportError:
15-
from mock import patch
16-
from nose.tools import raises
12+
import pytest
1713

1814
import numpy as np
1915
from matplotlib import checkdep_tex, cm, rcParams
@@ -194,15 +190,15 @@ def test_grayscale_alpha():
194190

195191
@cleanup
196192
@needs_tex
197-
@raises(ValueError)
198-
@patch('matplotlib.dviread.PsfontsMap.__getitem__')
199-
def test_missing_psfont(mock):
193+
def test_missing_psfont(monkeypatch):
200194
"""An error is raised if a TeX font lacks a Type-1 equivalent"""
201-
psfont = dviread.PsFont(texname='texfont', psname='Some Font',
202-
effects=None, encoding=None, filename=None)
203-
mock.configure_mock(return_value=psfont)
195+
def psfont(*args, **kwargs):
196+
return dviread.PsFont(texname='texfont', psname='Some Font',
197+
effects=None, encoding=None, filename=None)
198+
199+
monkeypatch.setattr(dviread.PsfontsMap, '__getitem__', psfont)
204200
rcParams['text.usetex'] = True
205201
fig, ax = plt.subplots()
206202
ax.text(0.5, 0.5, 'hello')
207-
with tempfile.TemporaryFile() as tmpfile:
203+
with tempfile.TemporaryFile() as tmpfile, pytest.raises(ValueError):
208204
fig.savefig(tmpfile, format='pdf')

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import shutil
77

88
import numpy as np
9-
from nose.plugins.skip import SkipTest
9+
import pytest
1010

1111
import matplotlib as mpl
1212
import matplotlib.pyplot as plt
@@ -38,6 +38,12 @@ def check_for(texsystem):
3838
return latex.returncode == 0
3939

4040

41+
needs_xelatex = pytest.mark.skipif(not check_for('xelatex'),
42+
reason='xelatex + pgf is required')
43+
needs_pdflatex = pytest.mark.skipif(not check_for('pdflatex'),
44+
reason='pdflatex + pgf is required')
45+
46+
4147
def compare_figure(fname, savefig_kwargs={}, tol=0):
4248
actual = os.path.join(result_dir, fname)
4349
plt.savefig(actual, **savefig_kwargs)
@@ -76,12 +82,10 @@ def create_figure():
7682

7783

7884
# test compiling a figure to pdf with xelatex
85+
@needs_xelatex
7986
@cleanup(style='classic')
8087
@switch_backend('pgf')
8188
def test_xelatex():
82-
if not check_for('xelatex'):
83-
raise SkipTest('xelatex + pgf is required')
84-
8589
rc_xelatex = {'font.family': 'serif',
8690
'pgf.rcfonts': False}
8791
mpl.rcParams.update(rc_xelatex)
@@ -90,6 +94,7 @@ def test_xelatex():
9094

9195

9296
# test compiling a figure to pdf with pdflatex
97+
@needs_pdflatex
9398
@cleanup(style='classic')
9499
@switch_backend('pgf')
95100
def test_pdflatex():
@@ -98,8 +103,6 @@ def test_pdflatex():
98103
from matplotlib.testing import xfail
99104
xfail("pdflatex test does not work on appveyor due "
100105
"to missing latex fonts")
101-
if not check_for('pdflatex'):
102-
raise SkipTest('pdflatex + pgf is required')
103106

104107
rc_pdflatex = {'font.family': 'serif',
105108
'pgf.rcfonts': False,
@@ -112,12 +115,11 @@ def test_pdflatex():
112115

113116

114117
# test updating the rc parameters for each figure
118+
@needs_xelatex
119+
@needs_pdflatex
115120
@cleanup(style='classic')
116121
@switch_backend('pgf')
117122
def test_rcupdate():
118-
if not check_for('xelatex') or not check_for('pdflatex'):
119-
raise SkipTest('xelatex and pdflatex + pgf required')
120-
121123
rc_sets = []
122124
rc_sets.append({'font.family': 'sans-serif',
123125
'font.size': 30,
@@ -145,12 +147,10 @@ def test_rcupdate():
145147

146148

147149
# test backend-side clipping, since large numbers are not supported by TeX
150+
@needs_xelatex
148151
@cleanup(style='classic')
149152
@switch_backend('pgf')
150153
def test_pathclip():
151-
if not check_for('xelatex'):
152-
raise SkipTest('xelatex + pgf is required')
153-
154154
rc_xelatex = {'font.family': 'serif',
155155
'pgf.rcfonts': False}
156156
mpl.rcParams.update(rc_xelatex)
@@ -164,12 +164,10 @@ def test_pathclip():
164164

165165

166166
# test mixed mode rendering
167+
@needs_xelatex
167168
@cleanup(style='classic')
168169
@switch_backend('pgf')
169170
def test_mixedmode():
170-
if not check_for('xelatex'):
171-
raise SkipTest('xelatex + pgf is required')
172-
173171
rc_xelatex = {'font.family': 'serif',
174172
'pgf.rcfonts': False}
175173
mpl.rcParams.update(rc_xelatex)
@@ -181,12 +179,10 @@ def test_mixedmode():
181179

182180

183181
# test bbox_inches clipping
182+
@needs_xelatex
184183
@cleanup(style='classic')
185184
@switch_backend('pgf')
186185
def test_bbox_inches():
187-
if not check_for('xelatex'):
188-
raise SkipTest('xelatex + pgf is required')
189-
190186
rc_xelatex = {'font.family': 'serif',
191187
'pgf.rcfonts': False}
192188
mpl.rcParams.update(rc_xelatex)
@@ -202,8 +198,3 @@ def test_bbox_inches():
202198
bbox = ax1.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
203199
compare_figure('pgf_bbox_inches.pdf', savefig_kwargs={'bbox_inches': bbox},
204200
tol=0)
205-
206-
207-
if __name__ == '__main__':
208-
import nose
209-
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/tests/test_backend_ps.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,3 @@ def test_determinism_all():
199199
def test_determinism_all_tex():
200200
"""Test for reproducible PS/tex output"""
201201
_determinism_check(format="ps", usetex=True)
202-
203-
204-
if __name__ == '__main__':
205-
import nose
206-
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/tests/test_backend_qt4.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
from six import unichr
66
from matplotlib import pyplot as plt
77
from matplotlib.testing.decorators import cleanup, switch_backend
8-
from matplotlib.testing.decorators import knownfailureif
98
from matplotlib._pylab_helpers import Gcf
109
import matplotlib
1110
import copy
1211

12+
import pytest
1313
try:
1414
# mock in python 3.3+
1515
from unittest import mock
@@ -38,9 +38,11 @@
3838
except ImportError:
3939
HAS_QT = False
4040

41+
if not HAS_QT:
42+
pytestmark = pytest.mark.xfail(reason='Qt4 is not available')
43+
4144

4245
@cleanup
43-
@knownfailureif(not HAS_QT)
4446
@switch_backend('Qt4Agg')
4547
def test_fig_close():
4648
# save the state of Gcf.figs
@@ -81,87 +83,76 @@ def receive(event):
8183

8284

8385
@cleanup
84-
@knownfailureif(not HAS_QT)
8586
def test_shift():
8687
assert_correct_key(QtCore.Qt.Key_A,
8788
ShiftModifier,
8889
'A')
8990

9091

9192
@cleanup
92-
@knownfailureif(not HAS_QT)
9393
def test_lower():
9494
assert_correct_key(QtCore.Qt.Key_A,
9595
QtCore.Qt.NoModifier,
9696
'a')
9797

9898

9999
@cleanup
100-
@knownfailureif(not HAS_QT)
101100
def test_control():
102101
assert_correct_key(QtCore.Qt.Key_A,
103102
ControlModifier,
104103
'ctrl+a')
105104

106105

107106
@cleanup
108-
@knownfailureif(not HAS_QT)
109107
def test_unicode_upper():
110108
assert_correct_key(QtCore.Qt.Key_Aacute,
111109
ShiftModifier,
112110
unichr(193))
113111

114112

115113
@cleanup
116-
@knownfailureif(not HAS_QT)
117114
def test_unicode_lower():
118115
assert_correct_key(QtCore.Qt.Key_Aacute,
119116
QtCore.Qt.NoModifier,
120117
unichr(225))
121118

122119

123120
@cleanup
124-
@knownfailureif(not HAS_QT)
125121
def test_alt_control():
126122
assert_correct_key(ControlKey,
127123
AltModifier,
128124
'alt+control')
129125

130126

131127
@cleanup
132-
@knownfailureif(not HAS_QT)
133128
def test_control_alt():
134129
assert_correct_key(AltKey,
135130
ControlModifier,
136131
'ctrl+alt')
137132

138133

139134
@cleanup
140-
@knownfailureif(not HAS_QT)
141135
def test_modifier_order():
142136
assert_correct_key(QtCore.Qt.Key_Aacute,
143137
(ControlModifier | AltModifier | SuperModifier),
144138
'ctrl+alt+super+' + unichr(225))
145139

146140

147141
@cleanup
148-
@knownfailureif(not HAS_QT)
149142
def test_backspace():
150143
assert_correct_key(QtCore.Qt.Key_Backspace,
151144
QtCore.Qt.NoModifier,
152145
'backspace')
153146

154147

155148
@cleanup
156-
@knownfailureif(not HAS_QT)
157149
def test_backspace_mod():
158150
assert_correct_key(QtCore.Qt.Key_Backspace,
159151
ControlModifier,
160152
'ctrl+backspace')
161153

162154

163155
@cleanup
164-
@knownfailureif(not HAS_QT)
165156
def test_non_unicode_key():
166157
assert_correct_key(QtCore.Qt.Key_Play,
167158
QtCore.Qt.NoModifier,

0 commit comments

Comments
 (0)
0