Skip to content

Commit 850979b

Browse files
authored
TST: add message matches to pytest.raises in test_duplicate_labels.py GH30999 (#37114)
1 parent 66f3917 commit 850979b

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

pandas/tests/generic/test_duplicate_labels.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ def test_set_flags_with_duplicates(self, cls, axes):
275275
result = cls(**axes)
276276
assert result.flags.allows_duplicate_labels is True
277277

278-
with pytest.raises(pd.errors.DuplicateLabelError):
278+
msg = "Index has duplicates."
279+
with pytest.raises(pd.errors.DuplicateLabelError, match=msg):
279280
cls(**axes).set_flags(allows_duplicate_labels=False)
280281

281282
@pytest.mark.parametrize(
@@ -287,7 +288,8 @@ def test_set_flags_with_duplicates(self, cls, axes):
287288
],
288289
)
289290
def test_setting_allows_duplicate_labels_raises(self, data):
290-
with pytest.raises(pd.errors.DuplicateLabelError):
291+
msg = "Index has duplicates."
292+
with pytest.raises(pd.errors.DuplicateLabelError, match=msg):
291293
data.flags.allows_duplicate_labels = False
292294

293295
assert data.flags.allows_duplicate_labels is True
@@ -297,7 +299,8 @@ def test_setting_allows_duplicate_labels_raises(self, data):
297299
)
298300
def test_series_raises(self, func):
299301
s = pd.Series([0, 1], index=["a", "b"]).set_flags(allows_duplicate_labels=False)
300-
with pytest.raises(pd.errors.DuplicateLabelError):
302+
msg = "Index has duplicates."
303+
with pytest.raises(pd.errors.DuplicateLabelError, match=msg):
301304
func(s)
302305

303306
@pytest.mark.parametrize(
@@ -332,7 +335,8 @@ def test_getitem_raises(self, getter, target):
332335
else:
333336
target = df
334337

335-
with pytest.raises(pd.errors.DuplicateLabelError):
338+
msg = "Index has duplicates."
339+
with pytest.raises(pd.errors.DuplicateLabelError, match=msg):
336340
getter(target)
337341

338342
@pytest.mark.parametrize(
@@ -352,7 +356,8 @@ def test_getitem_raises(self, getter, target):
352356
],
353357
)
354358
def test_concat_raises(self, objs, kwargs):
355-
with pytest.raises(pd.errors.DuplicateLabelError):
359+
msg = "Index has duplicates."
360+
with pytest.raises(pd.errors.DuplicateLabelError, match=msg):
356361
pd.concat(objs, **kwargs)
357362

358363
@not_implemented
@@ -361,7 +366,8 @@ def test_merge_raises(self):
361366
allows_duplicate_labels=False
362367
)
363368
b = pd.DataFrame({"B": [0, 1, 2]}, index=["a", "b", "b"])
364-
with pytest.raises(pd.errors.DuplicateLabelError):
369+
msg = "Index has duplicates."
370+
with pytest.raises(pd.errors.DuplicateLabelError, match=msg):
365371
pd.merge(a, b, left_index=True, right_index=True)
366372

367373

@@ -381,13 +387,14 @@ def test_merge_raises(self):
381387
ids=lambda x: type(x).__name__,
382388
)
383389
def test_raises_basic(idx):
384-
with pytest.raises(pd.errors.DuplicateLabelError):
390+
msg = "Index has duplicates."
391+
with pytest.raises(pd.errors.DuplicateLabelError, match=msg):
385392
pd.Series(1, index=idx).set_flags(allows_duplicate_labels=False)
386393

387-
with pytest.raises(pd.errors.DuplicateLabelError):
394+
with pytest.raises(pd.errors.DuplicateLabelError, match=msg):
388395
pd.DataFrame({"A": [1, 1]}, index=idx).set_flags(allows_duplicate_labels=False)
389396

390-
with pytest.raises(pd.errors.DuplicateLabelError):
397+
with pytest.raises(pd.errors.DuplicateLabelError, match=msg):
391398
pd.DataFrame([[1, 2]], columns=idx).set_flags(allows_duplicate_labels=False)
392399

393400

@@ -412,7 +419,8 @@ def test_format_duplicate_labels_message_multi():
412419

413420
def test_dataframe_insert_raises():
414421
df = pd.DataFrame({"A": [1, 2]}).set_flags(allows_duplicate_labels=False)
415-
with pytest.raises(ValueError, match="Cannot specify"):
422+
msg = "Cannot specify"
423+
with pytest.raises(ValueError, match=msg):
416424
df.insert(0, "A", [3, 4], allow_duplicates=True)
417425

418426

0 commit comments

Comments
 (0)