Skip to content

Commit 6812842

Browse files
authored
TST: bare pytest raises (#32817)
1 parent d308712 commit 6812842

File tree

6 files changed

+48
-22
lines changed

6 files changed

+48
-22
lines changed

pandas/tests/io/excel/test_readers.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ def test_reader_dtype(self, read_ext):
426426
expected["c"] = ["001", "002", "003", "004"]
427427
tm.assert_frame_equal(actual, expected)
428428

429-
with pytest.raises(ValueError):
429+
msg = "Unable to convert column d to type int64"
430+
with pytest.raises(ValueError, match=msg):
430431
pd.read_excel(basename + read_ext, dtype={"d": "int64"})
431432

432433
@pytest.mark.parametrize(
@@ -822,13 +823,15 @@ def test_excel_old_index_format(self, read_ext):
822823

823824
def test_read_excel_bool_header_arg(self, read_ext):
824825
# GH 6114
826+
msg = "Passing a bool to header is invalid"
825827
for arg in [True, False]:
826-
with pytest.raises(TypeError):
828+
with pytest.raises(TypeError, match=msg):
827829
pd.read_excel("test1" + read_ext, header=arg)
828830

829831
def test_read_excel_chunksize(self, read_ext):
830832
# GH 8011
831-
with pytest.raises(NotImplementedError):
833+
msg = "chunksize keyword of read_excel is not implemented"
834+
with pytest.raises(NotImplementedError, match=msg):
832835
pd.read_excel("test1" + read_ext, chunksize=100)
833836

834837
def test_read_excel_skiprows_list(self, read_ext):

pandas/tests/io/excel/test_writers.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ def test_excel_sheet_by_name_raise(self, path):
330330

331331
tm.assert_frame_equal(gt, df)
332332

333-
with pytest.raises(xlrd.XLRDError):
333+
msg = "No sheet named <'0'>"
334+
with pytest.raises(xlrd.XLRDError, match=msg):
334335
pd.read_excel(xl, "0")
335336

336337
def test_excel_writer_context_manager(self, frame, path):
@@ -973,7 +974,11 @@ def roundtrip(data, header=True, parser_hdr=0, index=True):
973974
# This if will be removed once multi-column Excel writing
974975
# is implemented. For now fixing gh-9794.
975976
if c_idx_nlevels > 1:
976-
with pytest.raises(NotImplementedError):
977+
msg = (
978+
"Writing to Excel with MultiIndex columns and no index "
979+
"\\('index'=False\\) is not yet implemented."
980+
)
981+
with pytest.raises(NotImplementedError, match=msg):
977982
roundtrip(df, use_headers, index=False)
978983
else:
979984
res = roundtrip(df, use_headers)

pandas/tests/io/excel/test_xlwt.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ def test_excel_raise_error_on_multiindex_columns_and_no_index(ext):
1818
[("site", ""), ("2014", "height"), ("2014", "weight")]
1919
)
2020
df = DataFrame(np.random.randn(10, 3), columns=cols)
21-
with pytest.raises(NotImplementedError):
21+
22+
msg = (
23+
"Writing to Excel with MultiIndex columns and no index "
24+
"\\('index'=False\\) is not yet implemented."
25+
)
26+
with pytest.raises(NotImplementedError, match=msg):
2227
with tm.ensure_clean(ext) as path:
2328
df.to_excel(path, index=False)
2429

pandas/tests/io/formats/test_format.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,8 @@ def test_to_string_specified_header(self):
15081508

15091509
assert df_s == expected
15101510

1511-
with pytest.raises(ValueError):
1511+
msg = "Writing 2 cols but got 1 aliases"
1512+
with pytest.raises(ValueError, match=msg):
15121513
df.to_string(header=["X"])
15131514

15141515
def test_to_string_no_index(self):

pandas/tests/io/formats/test_style.py

+25-14
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def h(x, foo="bar"):
3737
]
3838

3939
def test_init_non_pandas(self):
40-
with pytest.raises(TypeError):
40+
msg = "``data`` must be a Series or DataFrame"
41+
with pytest.raises(TypeError, match=msg):
4142
Styler([1, 2, 3])
4243

4344
def test_init_series(self):
@@ -1013,7 +1014,8 @@ def test_bar_align_zero_nans(self):
10131014

10141015
def test_bar_bad_align_raises(self):
10151016
df = pd.DataFrame({"A": [-100, -60, -30, -20]})
1016-
with pytest.raises(ValueError):
1017+
msg = "`align` must be one of {'left', 'zero',' mid'}"
1018+
with pytest.raises(ValueError, match=msg):
10171019
df.style.bar(align="poorly", color=["#d65f5f", "#5fba7d"])
10181020

10191021
def test_format_with_na_rep(self):
@@ -1082,7 +1084,8 @@ def test_format_non_numeric_na(self):
10821084
def test_format_with_bad_na_rep(self):
10831085
# GH 21527 28358
10841086
df = pd.DataFrame([[None, None], [1.1, 1.2]], columns=["A", "B"])
1085-
with pytest.raises(TypeError):
1087+
msg = "Expected a string, got -1 instead"
1088+
with pytest.raises(TypeError, match=msg):
10861089
df.style.format(None, na_rep=-1)
10871090

10881091
def test_highlight_null(self, null_color="red"):
@@ -1110,10 +1113,11 @@ def test_highlight_null_subset(self):
11101113

11111114
def test_nonunique_raises(self):
11121115
df = pd.DataFrame([[1, 2]], columns=["A", "A"])
1113-
with pytest.raises(ValueError):
1116+
msg = "style is not supported for non-unique indices."
1117+
with pytest.raises(ValueError, match=msg):
11141118
df.style
11151119

1116-
with pytest.raises(ValueError):
1120+
with pytest.raises(ValueError, match=msg):
11171121
Styler(df)
11181122

11191123
def test_caption(self):
@@ -1260,9 +1264,12 @@ def test_display_format(self):
12601264

12611265
def test_display_format_raises(self):
12621266
df = pd.DataFrame(np.random.randn(2, 2))
1263-
with pytest.raises(TypeError):
1267+
msg = "Expected a template string or callable, got 5 instead"
1268+
with pytest.raises(TypeError, match=msg):
12641269
df.style.format(5)
1265-
with pytest.raises(TypeError):
1270+
1271+
msg = "Expected a template string or callable, got True instead"
1272+
with pytest.raises(TypeError, match=msg):
12661273
df.style.format(True)
12671274

12681275
def test_display_set_precision(self):
@@ -1335,35 +1342,39 @@ def test_display_dict(self):
13351342

13361343
def test_bad_apply_shape(self):
13371344
df = pd.DataFrame([[1, 2], [3, 4]])
1338-
with pytest.raises(ValueError):
1345+
msg = "returned the wrong shape"
1346+
with pytest.raises(ValueError, match=msg):
13391347
df.style._apply(lambda x: "x", subset=pd.IndexSlice[[0, 1], :])
13401348

1341-
with pytest.raises(ValueError):
1349+
with pytest.raises(ValueError, match=msg):
13421350
df.style._apply(lambda x: [""], subset=pd.IndexSlice[[0, 1], :])
13431351

1344-
with pytest.raises(ValueError):
1352+
with pytest.raises(ValueError, match=msg):
13451353
df.style._apply(lambda x: ["", "", "", ""])
13461354

1347-
with pytest.raises(ValueError):
1355+
with pytest.raises(ValueError, match=msg):
13481356
df.style._apply(lambda x: ["", "", ""], subset=1)
13491357

1350-
with pytest.raises(ValueError):
1358+
msg = "Length mismatch: Expected axis has 3 elements"
1359+
with pytest.raises(ValueError, match=msg):
13511360
df.style._apply(lambda x: ["", "", ""], axis=1)
13521361

13531362
def test_apply_bad_return(self):
13541363
def f(x):
13551364
return ""
13561365

13571366
df = pd.DataFrame([[1, 2], [3, 4]])
1358-
with pytest.raises(TypeError):
1367+
msg = "must return a DataFrame when passed to `Styler.apply` with axis=None"
1368+
with pytest.raises(TypeError, match=msg):
13591369
df.style._apply(f, axis=None)
13601370

13611371
def test_apply_bad_labels(self):
13621372
def f(x):
13631373
return pd.DataFrame(index=[1, 2], columns=["a", "b"])
13641374

13651375
df = pd.DataFrame([[1, 2], [3, 4]])
1366-
with pytest.raises(ValueError):
1376+
msg = "must have identical index and columns as the input"
1377+
with pytest.raises(ValueError, match=msg):
13671378
df.style._apply(f, axis=None)
13681379

13691380
def test_get_level_lengths(self):

pandas/tests/io/formats/test_to_latex.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,8 @@ def test_to_latex_specified_header(self):
664664

665665
assert withoutescape_result == withoutescape_expected
666666

667-
with pytest.raises(ValueError):
667+
msg = "Writing 2 cols but got 1 aliases"
668+
with pytest.raises(ValueError, match=msg):
668669
df.to_latex(header=["A"])
669670

670671
def test_to_latex_decimal(self, float_frame):

0 commit comments

Comments
 (0)