Skip to content

Commit d716c7b

Browse files
committed
pytest changes
1 parent 4c8e12c commit d716c7b

File tree

3 files changed

+71
-99
lines changed

3 files changed

+71
-99
lines changed

lib/matplotlib/tests/test_contour.py

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ def test_contour_shape_mismatch_1():
4646
fig = plt.figure()
4747
ax = fig.add_subplot(111)
4848

49-
try:
49+
with pytest.raises(TypeError) as excinfo:
5050
ax.contour(x, y, z)
51-
except TypeError as exc:
52-
assert exc.args[0] == 'Length of x must be number of columns in z.'
51+
excinfo.match(r'Length of x must be number of columns in z.')
5352

5453

5554
def test_contour_shape_mismatch_2():
@@ -61,10 +60,9 @@ def test_contour_shape_mismatch_2():
6160
fig = plt.figure()
6261
ax = fig.add_subplot(111)
6362

64-
try:
63+
with pytest.raises(TypeError) as excinfo:
6564
ax.contour(x, y, z)
66-
except TypeError as exc:
67-
assert exc.args[0] == 'Length of y must be number of rows in z.'
65+
excinfo.match(r'Length of y must be number of rows in z.')
6866

6967

7068
def test_contour_shape_mismatch_3():
@@ -77,15 +75,13 @@ def test_contour_shape_mismatch_3():
7775
fig = plt.figure()
7876
ax = fig.add_subplot(111)
7977

80-
try:
78+
with pytest.raises(TypeError) as excinfo:
8179
ax.contour(xg, y, z)
82-
except TypeError as exc:
83-
assert exc.args[0] == 'Number of dimensions of x and y should match.'
80+
excinfo.match(r'Number of dimensions of x and y should match.')
8481

85-
try:
82+
with pytest.raises(TypeError) as excinfo:
8683
ax.contour(x, yg, z)
87-
except TypeError as exc:
88-
assert exc.args[0] == 'Number of dimensions of x and y should match.'
84+
excinfo.match(r'Number of dimensions of x and y should match.')
8985

9086

9187
def test_contour_shape_mismatch_4():
@@ -97,21 +93,15 @@ def test_contour_shape_mismatch_4():
9793
fig = plt.figure()
9894
ax = fig.add_subplot(111)
9995

100-
try:
96+
with pytest.raises(TypeError) as excinfo:
10197
ax.contour(b, g, z)
102-
except TypeError as exc:
103-
assert re.match(
104-
r'Shape of x does not match that of z: ' +
105-
r'found \(9L?, 9L?\) instead of \(9L?, 10L?\)\.',
106-
exc.args[0]) is not None, exc.args[0]
98+
excinfo.match(r'Shape of x does not match that of z: found \(9L?, 9L?\) ' +
99+
r'instead of \(9L?, 10L?\)')
107100

108-
try:
101+
with pytest.raises(TypeError) as excinfo:
109102
ax.contour(g, b, z)
110-
except TypeError as exc:
111-
assert re.match(
112-
r'Shape of y does not match that of z: ' +
113-
r'found \(9L?, 9L?\) instead of \(9L?, 10L?\)\.',
114-
exc.args[0]) is not None, exc.args[0]
103+
excinfo.match(r'Shape of y does not match that of z: found \(9L?, 9L?\) ' +
104+
r'instead of \(9L?, 10L?\)')
115105

116106

117107
def test_contour_shape_invalid_1():
@@ -123,10 +113,9 @@ def test_contour_shape_invalid_1():
123113
fig = plt.figure()
124114
ax = fig.add_subplot(111)
125115

126-
try:
116+
with pytest.raises(TypeError) as excinfo:
127117
ax.contour(x, y, z)
128-
except TypeError as exc:
129-
assert exc.args[0] == 'Inputs x and y must be 1D or 2D.'
118+
excinfo.match(r'Inputs x and y must be 1D or 2D.')
130119

131120

132121
def test_contour_shape_invalid_2():
@@ -138,10 +127,9 @@ def test_contour_shape_invalid_2():
138127
fig = plt.figure()
139128
ax = fig.add_subplot(111)
140129

141-
try:
130+
with pytest.raises(TypeError) as excinfo:
142131
ax.contour(x, y, z)
143-
except TypeError as exc:
144-
assert exc.args[0] == 'Input z must be a 2D array.'
132+
excinfo.match(r'Input z must be a 2D array.')
145133

146134

147135
@image_comparison(baseline_images=['contour_manual_labels'])
@@ -313,49 +301,42 @@ def test_contourf_symmetric_locator():
313301

314302
def test_contour_1x1_array():
315303
# github issue 8197
316-
try:
304+
with pytest.raises(TypeError) as excinfo:
317305
plt.contour([[0]])
318-
except TypeError as exc:
319-
assert exc.args[0] == 'Input z must be at least a 2x2 array.'
306+
excinfo.match(r'Input z must be at least a 2x2 array.')
320307

321-
try:
308+
with pytest.raises(TypeError) as excinfo:
322309
plt.contour([0], [0], [[0]])
323-
except TypeError as exc:
324-
assert exc.args[0] == 'Input z must be at least a 2x2 array.'
310+
excinfo.match(r'Input z must be at least a 2x2 array.')
325311

326312

327313
def test_internal_cpp_api():
328314
# Following github issue 8197.
329315
import matplotlib._contour as _contour
330316

331-
try:
317+
with pytest.raises(TypeError) as excinfo:
332318
qcg = _contour.QuadContourGenerator()
333-
except TypeError as exc:
334-
assert exc.args[0] == 'function takes exactly 6 arguments (0 given)'
319+
excinfo.match(r'function takes exactly 6 arguments \(0 given\)')
335320

336-
try:
321+
with pytest.raises(ValueError) as excinfo:
337322
qcg = _contour.QuadContourGenerator(1, 2, 3, 4, 5, 6)
338-
except ValueError as exc:
339-
assert exc.args[0] == 'Expected 2-dimensional array, got 0'
323+
excinfo.match(r'Expected 2-dimensional array, got 0')
340324

341-
try:
325+
with pytest.raises(ValueError) as excinfo:
342326
qcg = _contour.QuadContourGenerator([[0]], [[0]], [[]], None, True, 0)
343-
except ValueError as exc:
344-
assert exc.args[0] == 'x, y and z must all be 2D arrays with the same dimensions'
327+
excinfo.match(r'x, y and z must all be 2D arrays with the same dimensions')
345328

346-
try:
329+
with pytest.raises(ValueError) as excinfo:
347330
qcg = _contour.QuadContourGenerator([[0]], [[0]], [[0]], None, True, 0)
348-
except ValueError as exc:
349-
assert exc.args[0] == 'x, y and z must all be at least 2x2 arrays'
331+
excinfo.match(r'x, y and z must all be at least 2x2 arrays')
350332

351333
arr = [[0, 1], [2, 3]]
352-
try:
334+
with pytest.raises(ValueError) as excinfo:
353335
qcg = _contour.QuadContourGenerator(arr, arr, arr, [[0]], True, 0)
354-
except ValueError as exc:
355-
assert exc.args[0] == 'If mask is set it must be a 2D array with the same dimensions as x.'
336+
excinfo.match(r'If mask is set it must be a 2D array with the same ' +
337+
r'dimensions as x.')
356338

357339
qcg = _contour.QuadContourGenerator(arr, arr, arr, None, True, 0)
358-
try:
340+
with pytest.raises(ValueError) as excinfo:
359341
qcg.create_filled_contour(1, 0)
360-
except ValueError as exc:
361-
assert exc.args[0] == 'filled contour levels must be increasing'
342+
excinfo.match(r'filled contour levels must be increasing')

lib/matplotlib/tests/test_triangulation.py

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,78 +1055,71 @@ def test_internal_cpp_api():
10551055
import matplotlib._tri as _tri
10561056

10571057
# C++ Triangulation.
1058-
try:
1058+
with pytest.raises(TypeError) as excinfo:
10591059
triang = _tri.Triangulation()
1060-
except TypeError as exc:
1061-
assert exc.args[0] == 'function takes exactly 7 arguments (0 given)'
1060+
excinfo.match(r'function takes exactly 7 arguments \(0 given\)')
10621061

1063-
try:
1062+
with pytest.raises(ValueError) as excinfo:
10641063
triang = _tri.Triangulation([], [1], [[]], None, None, None, False)
1065-
except ValueError as exc:
1066-
assert exc.args[0] == 'x and y must be 1D arrays of the same length'
1064+
excinfo.match(r'x and y must be 1D arrays of the same length')
10671065

10681066
x = [0, 1, 1]
10691067
y = [0, 0, 1]
1070-
try:
1068+
with pytest.raises(ValueError) as excinfo:
10711069
triang = _tri.Triangulation(x, y, [[0, 1]], None, None, None, False)
1072-
except ValueError as exc:
1073-
assert exc.args[0] == 'triangles must be a 2D array of shape (?,3)'
1070+
excinfo.match(r'triangles must be a 2D array of shape \(\?,3\)')
10741071

1075-
try:
1076-
triang = _tri.Triangulation(x, y, [[0, 1, 2]], [0, 1], None, None, False)
1077-
except ValueError as exc:
1078-
assert exc.args[0] == 'mask must be a 1D array with the same length as the triangles array'
1072+
tris = [[0, 1, 2]]
1073+
with pytest.raises(ValueError) as excinfo:
1074+
triang = _tri.Triangulation(x, y, tris, [0, 1], None, None, False)
1075+
excinfo.match(r'mask must be a 1D array with the same length as the ' +
1076+
r'triangles array')
10791077

1080-
try:
1081-
triang = _tri.Triangulation(x, y, [[0, 1, 2]], None, [[1]], None, False)
1082-
except ValueError as exc:
1083-
assert exc.args[0] == 'edges must be a 2D array with shape (?,2)'
1078+
with pytest.raises(ValueError) as excinfo:
1079+
triang = _tri.Triangulation(x, y, tris, None, [[1]], None, False)
1080+
excinfo.match(r'edges must be a 2D array with shape \(\?,2\)')
10841081

1085-
try:
1086-
triang = _tri.Triangulation(x, y, [[0, 1, 2]], None, None, [[-1]], False)
1087-
except ValueError as exc:
1088-
assert exc.args[0] == 'neighbors must be a 2D array with the same shape as the triangles array'
1082+
with pytest.raises(ValueError) as excinfo:
1083+
triang = _tri.Triangulation(x, y, tris, None, None, [[-1]], False)
1084+
excinfo.match(r'neighbors must be a 2D array with the same shape as the ' +
1085+
r'triangles array')
10891086

1090-
triang = _tri.Triangulation(x, y, [[0, 1, 2]], None, None, None, False)
1087+
triang = _tri.Triangulation(x, y, tris, None, None, None, False)
10911088

1092-
try:
1089+
with pytest.raises(ValueError) as excinfo:
10931090
triang.calculate_plane_coefficients([])
1094-
except ValueError as exc:
1095-
assert exc.args[0] == 'z array must have same length as triangulation x and y arrays'
1091+
excinfo.match(r'z array must have same length as triangulation x and y ' +
1092+
r'arrays')
10961093

1097-
try:
1094+
with pytest.raises(ValueError) as excinfo:
10981095
triang.set_mask([0, 1])
1099-
except ValueError as exc:
1100-
assert exc.args[0] == 'mask must be a 1D array with the same length as the triangles array'
1096+
excinfo.match(r'mask must be a 1D array with the same length as the ' +
1097+
r'triangles array')
11011098

11021099
# C++ TriContourGenerator.
1103-
try:
1100+
with pytest.raises(TypeError) as excinfo:
11041101
tcg = _tri.TriContourGenerator()
1105-
except TypeError as exc:
1106-
assert exc.args[0] == 'function takes exactly 2 arguments (0 given)'
1102+
excinfo.match(r'function takes exactly 2 arguments \(0 given\)')
11071103

1108-
try:
1104+
with pytest.raises(ValueError) as excinfo:
11091105
tcg = _tri.TriContourGenerator(triang, [1])
1110-
except ValueError as exc:
1111-
assert exc.args[0] == 'z must be a 1D array with the same length as the x and y arrays'
1106+
excinfo.match(r'z must be a 1D array with the same length as the x and ' +
1107+
r'y arrays')
11121108

11131109
z = [0, 1, 2]
11141110
tcg = _tri.TriContourGenerator(triang, z)
11151111

1116-
try:
1112+
with pytest.raises(ValueError) as excinfo:
11171113
tcg.create_filled_contour(1, 0)
1118-
except ValueError as exc:
1119-
assert exc.args[0] == 'filled contour levels must be increasing'
1114+
excinfo.match(r'filled contour levels must be increasing')
11201115

11211116
# C++ TrapezoidMapTriFinder.
1122-
try:
1117+
with pytest.raises(TypeError) as excinfo:
11231118
trifinder = _tri.TrapezoidMapTriFinder()
1124-
except TypeError as exc:
1125-
assert exc.args[0] == 'function takes exactly 1 argument (0 given)'
1119+
excinfo.match(r'function takes exactly 1 argument \(0 given\)')
11261120

11271121
trifinder = _tri.TrapezoidMapTriFinder(triang)
11281122

1129-
try:
1123+
with pytest.raises(ValueError) as excinfo:
11301124
trifinder.find_many([0], [0, 1])
1131-
except ValueError as exc:
1132-
assert exc.args[0] == 'x and y must be array_like with same shape'
1125+
excinfo.match(r'x and y must be array_like with same shape')

pytest.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ pep8ignore =
5050
matplotlib/testing/jpl_units/UnitDbl.py E201 E202 E203
5151
matplotlib/testing/jpl_units/UnitDblConverter.py E201 E202 E203 E251 E302 E501 E711
5252
matplotlib/testing/jpl_units/UnitDblFormatter.py E201 E202 E251 E302
53-
matplotlib/tests/test_contour.py E501
5453
matplotlib/tests/test_image.py E225 E231 E251 E302 E303 E501 E502
5554
matplotlib/tests/test_lines.py E231 E261
5655
matplotlib/tests/test_mathtext.py E261 E302 E501
5756
matplotlib/tests/test_rcparams.py E231 E501
5857
matplotlib/tests/test_tightlayout.py E302
59-
matplotlib/tests/test_triangulation.py E501
6058
matplotlib/tri/triinterpolate.py E201 E221
6159
matplotlib/_cm.py E101 E202 E203 W191
6260
matplotlib/_mathtext_data.py E203 E231 E261 E501

0 commit comments

Comments
 (0)