Skip to content

Commit d23ab4a

Browse files
Mark Grahamluckyvs1
Mark Graham
authored andcommitted
TST: Assert msg with pytest raises in pandas/tests/extension/base (pandas-dev#38232)
1 parent 268ca52 commit d23ab4a

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

pandas/tests/extension/base/getitem.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,12 @@ def test_getitem_mask(self, data):
160160

161161
def test_getitem_mask_raises(self, data):
162162
mask = np.array([True, False])
163-
with pytest.raises(IndexError):
163+
msg = f"Boolean index has wrong length: 2 instead of {len(data)}"
164+
with pytest.raises(IndexError, match=msg):
164165
data[mask]
165166

166167
mask = pd.array(mask, dtype="boolean")
167-
with pytest.raises(IndexError):
168+
with pytest.raises(IndexError, match=msg):
168169
data[mask]
169170

170171
def test_getitem_boolean_array_mask(self, data):
@@ -305,7 +306,9 @@ def test_take_empty(self, data, na_value, na_cmp):
305306
result = empty.take([-1], allow_fill=True)
306307
assert na_cmp(result[0], na_value)
307308

308-
with pytest.raises(IndexError):
309+
msg = "cannot do a non-empty take from an empty axes|out of bounds"
310+
311+
with pytest.raises(IndexError, match=msg):
309312
empty.take([-1])
310313

311314
with pytest.raises(IndexError, match="cannot do a non-empty take"):
@@ -330,13 +333,14 @@ def test_take_non_na_fill_value(self, data_missing):
330333
self.assert_extension_array_equal(result, expected)
331334

332335
def test_take_pandas_style_negative_raises(self, data, na_value):
333-
with pytest.raises(ValueError):
336+
with pytest.raises(ValueError, match=""):
334337
data.take([0, -2], fill_value=na_value, allow_fill=True)
335338

336339
@pytest.mark.parametrize("allow_fill", [True, False])
337340
def test_take_out_of_bounds_raises(self, data, allow_fill):
338341
arr = data[:3]
339-
with pytest.raises(IndexError):
342+
343+
with pytest.raises(IndexError, match="out of bounds|out-of-bounds"):
340344
arr.take(np.asarray([0, 3]), allow_fill=allow_fill)
341345

342346
def test_take_series(self, data):

pandas/tests/extension/base/reduce.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,25 @@ def test_reduce_series_numeric(self, data, all_numeric_reductions, skipna):
2828
op_name = all_numeric_reductions
2929
s = pd.Series(data)
3030

31-
with pytest.raises(TypeError):
31+
msg = (
32+
"[Cc]annot perform|Categorical is not ordered for operation|"
33+
"'Categorical' does not implement reduction|"
34+
)
35+
36+
with pytest.raises(TypeError, match=msg):
3237
getattr(s, op_name)(skipna=skipna)
3338

3439
@pytest.mark.parametrize("skipna", [True, False])
3540
def test_reduce_series_boolean(self, data, all_boolean_reductions, skipna):
3641
op_name = all_boolean_reductions
3742
s = pd.Series(data)
3843

39-
with pytest.raises(TypeError):
44+
msg = (
45+
"[Cc]annot perform|Categorical is not ordered for operation|"
46+
"'Categorical' does not implement reduction|"
47+
)
48+
49+
with pytest.raises(TypeError, match=msg):
4050
getattr(s, op_name)(skipna=skipna)
4151

4252

0 commit comments

Comments
 (0)