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

Skip to content

Commit b583623

Browse files
authored
Merge pull request #16454 from timhoffm/pytest-raises
TST: Use pytest.raises(match=...)
2 parents 93de426 + edb19f5 commit b583623

File tree

4 files changed

+46
-42
lines changed

4 files changed

+46
-42
lines changed

lib/matplotlib/tests/test_dviread.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ def test_PsfontsMap(monkeypatch):
4444
entry = fontmap[b'TeXfont9']
4545
assert entry.filename == b'/absolute/font9.pfb'
4646
# Missing font
47-
with pytest.raises(KeyError) as exc:
47+
with pytest.raises(KeyError, match='no-such-font'):
4848
fontmap[b'no-such-font']
49-
assert 'no-such-font' in str(exc.value)
5049

5150

5251
@pytest.mark.skipif(shutil.which("kpsewhich") is None,

lib/matplotlib/tests/test_mathtext.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,8 @@ def test_fontinfo():
254254
def test_mathtext_exceptions(math, msg):
255255
parser = mathtext.MathTextParser('agg')
256256

257-
with pytest.raises(ValueError) as excinfo:
257+
with pytest.raises(ValueError, match=re.escape(msg)):
258258
parser.parse(math)
259-
excinfo.match(re.escape(msg))
260259

261260

262261
def test_single_minus_sign():

lib/matplotlib/tests/test_triangulation.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,74 +1033,82 @@ def test_internal_cpp_api():
10331033
# Following github issue 8197.
10341034

10351035
# C++ Triangulation.
1036-
with pytest.raises(TypeError) as excinfo:
1036+
with pytest.raises(
1037+
TypeError,
1038+
match=r'function takes exactly 7 arguments \(0 given\)'):
10371039
mpl._tri.Triangulation()
1038-
excinfo.match(r'function takes exactly 7 arguments \(0 given\)')
10391040

1040-
with pytest.raises(ValueError) as excinfo:
1041+
with pytest.raises(
1042+
ValueError, match=r'x and y must be 1D arrays of the same length'):
10411043
mpl._tri.Triangulation([], [1], [[]], None, None, None, False)
1042-
excinfo.match(r'x and y must be 1D arrays of the same length')
10431044

10441045
x = [0, 1, 1]
10451046
y = [0, 0, 1]
1046-
with pytest.raises(ValueError) as excinfo:
1047+
with pytest.raises(
1048+
ValueError,
1049+
match=r'triangles must be a 2D array of shape \(\?,3\)'):
10471050
mpl._tri.Triangulation(x, y, [[0, 1]], None, None, None, False)
1048-
excinfo.match(r'triangles must be a 2D array of shape \(\?,3\)')
10491051

10501052
tris = [[0, 1, 2]]
1051-
with pytest.raises(ValueError) as excinfo:
1053+
with pytest.raises(
1054+
ValueError,
1055+
match=r'mask must be a 1D array with the same length as the '
1056+
r'triangles array'):
10521057
mpl._tri.Triangulation(x, y, tris, [0, 1], None, None, False)
1053-
excinfo.match(r'mask must be a 1D array with the same length as the ' +
1054-
r'triangles array')
10551058

1056-
with pytest.raises(ValueError) as excinfo:
1059+
with pytest.raises(
1060+
ValueError, match=r'edges must be a 2D array with shape \(\?,2\)'):
10571061
mpl._tri.Triangulation(x, y, tris, None, [[1]], None, False)
1058-
excinfo.match(r'edges must be a 2D array with shape \(\?,2\)')
10591062

1060-
with pytest.raises(ValueError) as excinfo:
1063+
with pytest.raises(
1064+
ValueError,
1065+
match=r'neighbors must be a 2D array with the same shape as the '
1066+
r'triangles array'):
10611067
mpl._tri.Triangulation(x, y, tris, None, None, [[-1]], False)
1062-
excinfo.match(r'neighbors must be a 2D array with the same shape as the ' +
1063-
r'triangles array')
10641068

10651069
triang = mpl._tri.Triangulation(x, y, tris, None, None, None, False)
10661070

1067-
with pytest.raises(ValueError) as excinfo:
1071+
with pytest.raises(
1072+
ValueError,
1073+
match=r'z array must have same length as triangulation x and y '
1074+
r'array'):
10681075
triang.calculate_plane_coefficients([])
1069-
excinfo.match(r'z array must have same length as triangulation x and y ' +
1070-
r'arrays')
10711076

1072-
with pytest.raises(ValueError) as excinfo:
1077+
with pytest.raises(
1078+
ValueError,
1079+
match=r'mask must be a 1D array with the same length as the '
1080+
r'triangles array'):
10731081
triang.set_mask([0, 1])
1074-
excinfo.match(r'mask must be a 1D array with the same length as the ' +
1075-
r'triangles array')
10761082

10771083
# C++ TriContourGenerator.
1078-
with pytest.raises(TypeError) as excinfo:
1079-
tcg = mpl._tri.TriContourGenerator()
1080-
excinfo.match(r'function takes exactly 2 arguments \(0 given\)')
1084+
with pytest.raises(
1085+
TypeError,
1086+
match=r'function takes exactly 2 arguments \(0 given\)'):
1087+
mpl._tri.TriContourGenerator()
10811088

1082-
with pytest.raises(ValueError) as excinfo:
1083-
tcg = mpl._tri.TriContourGenerator(triang, [1])
1084-
excinfo.match(r'z must be a 1D array with the same length as the x and ' +
1085-
r'y arrays')
1089+
with pytest.raises(
1090+
ValueError,
1091+
match=r'z must be a 1D array with the same length as the x and y '
1092+
r'arrays'):
1093+
mpl._tri.TriContourGenerator(triang, [1])
10861094

10871095
z = [0, 1, 2]
10881096
tcg = mpl._tri.TriContourGenerator(triang, z)
10891097

1090-
with pytest.raises(ValueError) as excinfo:
1098+
with pytest.raises(
1099+
ValueError, match=r'filled contour levels must be increasing'):
10911100
tcg.create_filled_contour(1, 0)
1092-
excinfo.match(r'filled contour levels must be increasing')
10931101

10941102
# C++ TrapezoidMapTriFinder.
1095-
with pytest.raises(TypeError) as excinfo:
1096-
trifinder = mpl._tri.TrapezoidMapTriFinder()
1097-
excinfo.match(r'function takes exactly 1 argument \(0 given\)')
1103+
with pytest.raises(
1104+
TypeError, match=r'function takes exactly 1 argument \(0 given\)'):
1105+
mpl._tri.TrapezoidMapTriFinder()
10981106

10991107
trifinder = mpl._tri.TrapezoidMapTriFinder(triang)
11001108

1101-
with pytest.raises(ValueError) as excinfo:
1109+
with pytest.raises(
1110+
ValueError, match=r'x and y must be array-like with same shape'):
11021111
trifinder.find_many([0], [0, 1])
1103-
excinfo.match(r'x and y must be array-like with same shape')
11041112

11051113

11061114
def test_qhull_large_offset():

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -809,13 +809,11 @@ def test_calling_conventions(self):
809809
ax.voxels(x, y, z, filled=filled, **kw)
810810

811811
# duplicate argument
812-
with pytest.raises(TypeError) as exc:
812+
with pytest.raises(TypeError, match='voxels'):
813813
ax.voxels(x, y, z, filled, filled=filled)
814-
exc.match(".*voxels.*")
815814
# missing arguments
816-
with pytest.raises(TypeError) as exc:
815+
with pytest.raises(TypeError, match='voxels'):
817816
ax.voxels(x, y)
818-
exc.match(".*voxels.*")
819817
# x, y, z are positional only - this passes them on as attributes of
820818
# Poly3DCollection
821819
with pytest.raises(AttributeError):

0 commit comments

Comments
 (0)