Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b1d896

Browse files
authored
Merge pull request #7863 from QuLogic/pytest-backends
Convert backend tests to use pytest
2 parents 0384b85 + 9e3b260 commit 0b1d896

10 files changed

Lines changed: 137 additions & 297 deletions

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/testing/decorators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ def switch_backend(backend):
486486
# Local import to avoid a hard nose dependency and only incur the
487487
# import time overhead at actual test-time.
488488
def switch_backend_decorator(func):
489+
@functools.wraps(func)
489490
def backend_switcher(*args, **kwargs):
490491
try:
491492
prev_backend = mpl.get_backend()

lib/matplotlib/testing/determinism.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
import sys
1414
from subprocess import check_output
1515

16+
import pytest
17+
1618
import matplotlib
1719
from matplotlib import pyplot as plt
1820

19-
from nose.plugins.skip import SkipTest
20-
2121

2222
def _determinism_save(objects='mhi', format="pdf", usetex=False):
2323
# save current value of SOURCE_DATE_EPOCH and set it
@@ -92,11 +92,11 @@ def _determinism_check(objects='mhi', format="pdf", usetex=False):
9292
format : str
9393
format string. The default value is "pdf".
9494
"""
95-
from nose.tools import assert_equal
9695
plots = []
9796
for i in range(3):
9897
result = check_output([sys.executable, '-R', '-c',
9998
'import matplotlib; '
99+
'matplotlib._called_from_pytest = True; '
100100
'matplotlib.use(%r); '
101101
'from matplotlib.testing.determinism '
102102
'import _determinism_save;'
@@ -106,9 +106,9 @@ def _determinism_check(objects='mhi', format="pdf", usetex=False):
106106
for p in plots[1:]:
107107
if usetex:
108108
if p != plots[0]:
109-
raise SkipTest("failed, maybe due to ghostscript timestamps")
109+
pytest.skip("failed, maybe due to ghostscript timestamps")
110110
else:
111-
assert_equal(p, plots[0])
111+
assert p == plots[0]
112112

113113

114114
def _determinism_source_date_epoch(format, string, keyword=b"CreationDate"):
@@ -130,6 +130,7 @@ def _determinism_source_date_epoch(format, string, keyword=b"CreationDate"):
130130
"""
131131
buff = check_output([sys.executable, '-R', '-c',
132132
'import matplotlib; '
133+
'matplotlib._called_from_pytest = True; '
133134
'matplotlib.use(%r); '
134135
'from matplotlib.testing.determinism '
135136
'import _determinism_save;'

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)

0 commit comments

Comments
 (0)