Skip to content

Commit 7dccda5

Browse files
author
Mark Graham
authored
TST: add messages to bare pytest raises (#38414)
1 parent 35cc565 commit 7dccda5

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

pandas/tests/generic/methods/test_pipe.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@ def test_pipe_tuple_error(self, frame_or_series):
3131
obj = obj["A"]
3232

3333
f = lambda x, y: y
34-
with pytest.raises(ValueError):
34+
35+
msg = "y is both the pipe target and a keyword argument"
36+
37+
with pytest.raises(ValueError, match=msg):
3538
obj.pipe((f, "y"), x=1, y=0)

pandas/tests/reshape/merge/test_merge_asof.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -223,27 +223,29 @@ def test_multi_index(self):
223223
# MultiIndex is prohibited
224224
trades = self.trades.set_index(["time", "price"])
225225
quotes = self.quotes.set_index("time")
226-
with pytest.raises(MergeError):
226+
with pytest.raises(MergeError, match="left can only have one index"):
227227
merge_asof(trades, quotes, left_index=True, right_index=True)
228228

229229
trades = self.trades.set_index("time")
230230
quotes = self.quotes.set_index(["time", "bid"])
231-
with pytest.raises(MergeError):
231+
with pytest.raises(MergeError, match="right can only have one index"):
232232
merge_asof(trades, quotes, left_index=True, right_index=True)
233233

234234
def test_on_and_index(self):
235235

236236
# "on" parameter and index together is prohibited
237237
trades = self.trades.set_index("time")
238238
quotes = self.quotes.set_index("time")
239-
with pytest.raises(MergeError):
239+
msg = 'Can only pass argument "left_on" OR "left_index" not both.'
240+
with pytest.raises(MergeError, match=msg):
240241
merge_asof(
241242
trades, quotes, left_on="price", left_index=True, right_index=True
242243
)
243244

244245
trades = self.trades.set_index("time")
245246
quotes = self.quotes.set_index("time")
246-
with pytest.raises(MergeError):
247+
msg = 'Can only pass argument "right_on" OR "right_index" not both.'
248+
with pytest.raises(MergeError, match=msg):
247249
merge_asof(
248250
trades, quotes, right_on="bid", left_index=True, right_index=True
249251
)
@@ -439,7 +441,9 @@ def test_multiby_indexed(self):
439441

440442
tm.assert_frame_equal(expected, result)
441443

442-
with pytest.raises(MergeError):
444+
with pytest.raises(
445+
MergeError, match="left_by and right_by must be same length"
446+
):
443447
pd.merge_asof(
444448
left,
445449
right,
@@ -478,13 +482,15 @@ def test_valid_join_keys(self):
478482
trades = self.trades
479483
quotes = self.quotes
480484

481-
with pytest.raises(MergeError):
485+
msg = r"incompatible merge keys \[1\] .* must be the same type"
486+
487+
with pytest.raises(MergeError, match=msg):
482488
merge_asof(trades, quotes, left_on="time", right_on="bid", by="ticker")
483489

484-
with pytest.raises(MergeError):
490+
with pytest.raises(MergeError, match="can only asof on a key for left"):
485491
merge_asof(trades, quotes, on=["time", "ticker"], by="ticker")
486492

487-
with pytest.raises(MergeError):
493+
with pytest.raises(MergeError, match="can only asof on a key for left"):
488494
merge_asof(trades, quotes, by="ticker")
489495

490496
def test_with_duplicates(self, datapath):
@@ -513,7 +519,9 @@ def test_valid_allow_exact_matches(self):
513519
trades = self.trades
514520
quotes = self.quotes
515521

516-
with pytest.raises(MergeError):
522+
msg = "allow_exact_matches must be boolean, passed foo"
523+
524+
with pytest.raises(MergeError, match=msg):
517525
merge_asof(
518526
trades, quotes, on="time", by="ticker", allow_exact_matches="foo"
519527
)
@@ -535,12 +543,14 @@ def test_valid_tolerance(self):
535543
tolerance=1,
536544
)
537545

546+
msg = r"incompatible tolerance .*, must be compat with type .*"
547+
538548
# incompat
539-
with pytest.raises(MergeError):
549+
with pytest.raises(MergeError, match=msg):
540550
merge_asof(trades, quotes, on="time", by="ticker", tolerance=1)
541551

542552
# invalid
543-
with pytest.raises(MergeError):
553+
with pytest.raises(MergeError, match=msg):
544554
merge_asof(
545555
trades.reset_index(),
546556
quotes.reset_index(),
@@ -549,13 +559,15 @@ def test_valid_tolerance(self):
549559
tolerance=1.0,
550560
)
551561

562+
msg = "tolerance must be positive"
563+
552564
# invalid negative
553-
with pytest.raises(MergeError):
565+
with pytest.raises(MergeError, match=msg):
554566
merge_asof(
555567
trades, quotes, on="time", by="ticker", tolerance=-Timedelta("1s")
556568
)
557569

558-
with pytest.raises(MergeError):
570+
with pytest.raises(MergeError, match=msg):
559571
merge_asof(
560572
trades.reset_index(),
561573
quotes.reset_index(),
@@ -572,13 +584,13 @@ def test_non_sorted(self):
572584
# we require that we are already sorted on time & quotes
573585
assert not trades.time.is_monotonic
574586
assert not quotes.time.is_monotonic
575-
with pytest.raises(ValueError):
587+
with pytest.raises(ValueError, match="left keys must be sorted"):
576588
merge_asof(trades, quotes, on="time", by="ticker")
577589

578590
trades = self.trades.sort_values("time")
579591
assert trades.time.is_monotonic
580592
assert not quotes.time.is_monotonic
581-
with pytest.raises(ValueError):
593+
with pytest.raises(ValueError, match="right keys must be sorted"):
582594
merge_asof(trades, quotes, on="time", by="ticker")
583595

584596
quotes = self.quotes.sort_values("time")

0 commit comments

Comments
 (0)