Skip to content

Commit 01c223a

Browse files
moinkluckyvs1
authored andcommitted
TST: GH30999 Add match=msg to all but two pytest.raises in tests/io (pandas-dev#38724)
1 parent f7f8904 commit 01c223a

13 files changed

+117
-77
lines changed

pandas/tests/io/excel/test_xlrd.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ def test_read_xlrd_book(read_ext, frame):
4343
# TODO: test for openpyxl as well
4444
def test_excel_table_sheet_by_index(datapath, read_ext):
4545
path = datapath("io", "data", "excel", f"test1{read_ext}")
46+
msg = "No sheet named <'invalid_sheet_name'>"
4647
with ExcelFile(path, engine="xlrd") as excel:
47-
with pytest.raises(xlrd.XLRDError):
48-
pd.read_excel(excel, sheet_name="asdf")
48+
with pytest.raises(xlrd.XLRDError, match=msg):
49+
pd.read_excel(excel, sheet_name="invalid_sheet_name")
4950

5051

5152
def test_excel_file_warning_with_xlsx_file(datapath):

pandas/tests/io/parser/test_network.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,13 @@ def test_parse_public_s3_bucket_nrows_python(self, tips_df, s3so):
200200
tm.assert_frame_equal(tips_df.iloc[:10], df)
201201

202202
def test_read_s3_fails(self, s3so):
203-
with pytest.raises(IOError):
203+
msg = "The specified bucket does not exist"
204+
with pytest.raises(IOError, match=msg):
204205
read_csv("s3://nyqpug/asdf.csv", storage_options=s3so)
205206

206207
# Receive a permission error when trying to read a private bucket.
207208
# It's irrelevant here that this isn't actually a table.
208-
with pytest.raises(IOError):
209+
with pytest.raises(IOError, match=msg):
209210
read_csv("s3://cant_get_it/file.csv")
210211

211212
def test_write_s3_csv_fails(self, tips_df, s3so):

pandas/tests/io/parser/test_python_parser_only.py

+10-19
Original file line numberDiff line numberDiff line change
@@ -249,19 +249,15 @@ def test_multi_char_sep_quotes(python_parser_only, quoting):
249249
parser = python_parser_only
250250

251251
data = 'a,,b\n1,,a\n2,,"2,,b"'
252-
msg = "ignored when a multi-char delimiter is used"
253252

254-
def fail_read():
253+
if quoting == csv.QUOTE_NONE:
254+
msg = "Expected 2 fields in line 3, saw 3"
255255
with pytest.raises(ParserError, match=msg):
256256
parser.read_csv(StringIO(data), quoting=quoting, **kwargs)
257-
258-
if quoting == csv.QUOTE_NONE:
259-
# We expect no match, so there should be an assertion
260-
# error out of the inner context manager.
261-
with pytest.raises(AssertionError):
262-
fail_read()
263257
else:
264-
fail_read()
258+
msg = "ignored when a multi-char delimiter is used"
259+
with pytest.raises(ParserError, match=msg):
260+
parser.read_csv(StringIO(data), quoting=quoting, **kwargs)
265261

266262

267263
def test_none_delimiter(python_parser_only, capsys):
@@ -286,20 +282,15 @@ def test_none_delimiter(python_parser_only, capsys):
286282
@pytest.mark.parametrize("skipfooter", [0, 1])
287283
def test_skipfooter_bad_row(python_parser_only, data, skipfooter):
288284
# see gh-13879 and gh-15910
289-
msg = "parsing errors in the skipped footer rows"
290285
parser = python_parser_only
291-
292-
def fail_read():
286+
if skipfooter:
287+
msg = "parsing errors in the skipped footer rows"
293288
with pytest.raises(ParserError, match=msg):
294289
parser.read_csv(StringIO(data), skipfooter=skipfooter)
295-
296-
if skipfooter:
297-
fail_read()
298290
else:
299-
# We expect no match, so there should be an assertion
300-
# error out of the inner context manager.
301-
with pytest.raises(AssertionError):
302-
fail_read()
291+
msg = "unexpected end of data|expected after"
292+
with pytest.raises(ParserError, match=msg):
293+
parser.read_csv(StringIO(data), skipfooter=skipfooter)
303294

304295

305296
def test_malformed_skipfooter(python_parser_only):

pandas/tests/io/sas/test_sas7bdat.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,16 @@ def test_inconsistent_number_of_rows(datapath):
213213
def test_zero_variables(datapath):
214214
# Check if the SAS file has zero variables (PR #18184)
215215
fname = datapath("io", "sas", "data", "zero_variables.sas7bdat")
216-
with pytest.raises(EmptyDataError):
216+
with pytest.raises(EmptyDataError, match="No columns to parse from file"):
217217
pd.read_sas(fname)
218218

219219

220220
def test_corrupt_read(datapath):
221221
# We don't really care about the exact failure, the important thing is
222222
# that the resource should be cleaned up afterwards (BUG #35566)
223223
fname = datapath("io", "sas", "data", "corrupt.sas7bdat")
224-
with pytest.raises(AttributeError):
224+
msg = "'SAS7BDATReader' object has no attribute 'row_count'"
225+
with pytest.raises(AttributeError, match=msg):
225226
pd.read_sas(fname)
226227

227228

pandas/tests/io/test_common.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ def test_warning_missing_utf_bom(self, encoding, compression_):
406406
df.to_csv(path, compression=compression_, encoding=encoding)
407407

408408
# reading should fail (otherwise we wouldn't need the warning)
409-
with pytest.raises(Exception):
409+
msg = r"UTF-\d+ stream does not start with BOM"
410+
with pytest.raises(UnicodeError, match=msg):
410411
pd.read_csv(path, compression=compression_, encoding=encoding)
411412

412413

pandas/tests/io/test_feather.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@
2121
@filter_sparse
2222
@pytest.mark.single
2323
class TestFeather:
24-
def check_error_on_write(self, df, exc):
24+
def check_error_on_write(self, df, exc, err_msg):
2525
# check that we are raising the exception
2626
# on writing
2727

28-
with pytest.raises(exc):
28+
with pytest.raises(exc, match=err_msg):
29+
with tm.ensure_clean() as path:
30+
to_feather(df, path)
31+
32+
def check_external_error_on_write(self, df):
33+
# check that we are raising the exception
34+
# on writing
35+
36+
with tm.external_error_raised(Exception):
2937
with tm.ensure_clean() as path:
3038
to_feather(df, path)
3139

@@ -42,14 +50,15 @@ def check_round_trip(self, df, expected=None, write_kwargs={}, **read_kwargs):
4250

4351
def test_error(self):
4452

53+
msg = "feather only support IO with DataFrames"
4554
for obj in [
4655
pd.Series([1, 2, 3]),
4756
1,
4857
"foo",
4958
pd.Timestamp("20130101"),
5059
np.array([1, 2, 3]),
5160
]:
52-
self.check_error_on_write(obj, ValueError)
61+
self.check_error_on_write(obj, ValueError, msg)
5362

5463
def test_basic(self):
5564

@@ -95,12 +104,13 @@ def test_duplicate_columns(self):
95104
# https://github.com/wesm/feather/issues/53
96105
# not currently able to handle duplicate columns
97106
df = pd.DataFrame(np.arange(12).reshape(4, 3), columns=list("aaa")).copy()
98-
self.check_error_on_write(df, ValueError)
107+
self.check_external_error_on_write(df)
99108

100109
def test_stringify_columns(self):
101110

102111
df = pd.DataFrame(np.arange(12).reshape(4, 3)).copy()
103-
self.check_error_on_write(df, ValueError)
112+
msg = "feather must have string column names"
113+
self.check_error_on_write(df, ValueError, msg)
104114

105115
def test_read_columns(self):
106116
# GH 24025
@@ -125,8 +135,7 @@ def test_unsupported_other(self):
125135

126136
# mixed python objects
127137
df = pd.DataFrame({"a": ["a", 1, 2.0]})
128-
# Some versions raise ValueError, others raise ArrowInvalid.
129-
self.check_error_on_write(df, Exception)
138+
self.check_external_error_on_write(df)
130139

131140
def test_rw_use_threads(self):
132141
df = pd.DataFrame({"A": np.arange(100000)})
@@ -138,6 +147,10 @@ def test_write_with_index(self):
138147
df = pd.DataFrame({"A": [1, 2, 3]})
139148
self.check_round_trip(df)
140149

150+
msg = (
151+
r"feather does not support serializing .* for the index; "
152+
r"you can \.reset_index\(\) to make the index into column\(s\)"
153+
)
141154
# non-default index
142155
for index in [
143156
[2, 3, 4],
@@ -148,17 +161,19 @@ def test_write_with_index(self):
148161
]:
149162

150163
df.index = index
151-
self.check_error_on_write(df, ValueError)
164+
self.check_error_on_write(df, ValueError, msg)
152165

153166
# index with meta-data
154167
df.index = [0, 1, 2]
155168
df.index.name = "foo"
156-
self.check_error_on_write(df, ValueError)
169+
msg = "feather does not serialize index meta-data on a default index"
170+
self.check_error_on_write(df, ValueError, msg)
157171

158172
# column multi-index
159173
df.index = [0, 1, 2]
160174
df.columns = pd.MultiIndex.from_tuples([("a", 1)])
161-
self.check_error_on_write(df, ValueError)
175+
msg = "feather must have string column names"
176+
self.check_error_on_write(df, ValueError, msg)
162177

163178
def test_path_pathlib(self):
164179
df = tm.makeDataFrame().reset_index()

pandas/tests/io/test_fsspec.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,16 @@ def test_reasonable_error(monkeypatch, cleared_fs):
5151
from fsspec.registry import known_implementations
5252

5353
registry.target.clear()
54-
with pytest.raises(ValueError) as e:
54+
with pytest.raises(ValueError, match="nosuchprotocol"):
5555
read_csv("nosuchprotocol://test/test.csv")
56-
assert "nosuchprotocol" in str(e.value)
57-
err_mgs = "test error messgae"
56+
err_msg = "test error message"
5857
monkeypatch.setitem(
5958
known_implementations,
6059
"couldexist",
61-
{"class": "unimportable.CouldExist", "err": err_mgs},
60+
{"class": "unimportable.CouldExist", "err": err_msg},
6261
)
63-
with pytest.raises(ImportError) as e:
62+
with pytest.raises(ImportError, match=err_msg):
6463
read_csv("couldexist://test/test.csv")
65-
assert err_mgs in str(e.value)
6664

6765

6866
def test_to_csv(cleared_fs):
@@ -225,9 +223,9 @@ def test_s3_parquet(s3_resource, s3so):
225223

226224
@td.skip_if_installed("fsspec")
227225
def test_not_present_exception():
228-
with pytest.raises(ImportError) as e:
226+
msg = "Missing optional dependency 'fsspec'|fsspec library is required"
227+
with pytest.raises(ImportError, match=msg):
229228
read_csv("memory://test/test.csv")
230-
assert "fsspec library is required" in str(e.value)
231229

232230

233231
@td.skip_if_no("pyarrow")

pandas/tests/io/test_gbq.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import pandas as pd
1313
from pandas import DataFrame
14+
import pandas._testing as tm
1415

1516
api_exceptions = pytest.importorskip("google.api_core.exceptions")
1617
bigquery = pytest.importorskip("google.cloud.bigquery")
@@ -195,7 +196,7 @@ def test_roundtrip(self, gbq_dataset):
195196
"if_exists, expected_num_rows, expectation",
196197
[
197198
("append", 300, does_not_raise()),
198-
("fail", 200, pytest.raises(pandas_gbq.gbq.TableCreationError)),
199+
("fail", 200, tm.external_error_raised(pandas_gbq.gbq.TableCreationError)),
199200
("replace", 100, does_not_raise()),
200201
],
201202
)

pandas/tests/io/test_gcs.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,5 @@ def open(self, path, mode="r", *args):
149149

150150
@td.skip_if_installed("gcsfs")
151151
def test_gcs_not_present_exception():
152-
with pytest.raises(ImportError) as e:
152+
with tm.external_error_raised(ImportError):
153153
read_csv("gs://test/test.csv")
154-
assert "gcsfs library is required" in str(e.value)

pandas/tests/io/test_html.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,18 @@ def test_file_like(self):
302302

303303
@tm.network
304304
def test_bad_url_protocol(self):
305-
with pytest.raises(URLError):
305+
with pytest.raises(URLError, match="urlopen error unknown url type: git"):
306306
self.read_html("git://github.com", match=".*Water.*")
307307

308308
@tm.network
309309
@pytest.mark.slow
310310
def test_invalid_url(self):
311-
try:
312-
with pytest.raises(URLError):
313-
self.read_html("http://www.a23950sdfa908sd.com", match=".*Water.*")
314-
except ValueError as e:
315-
assert "No tables found" in str(e)
311+
msg = (
312+
"Name or service not known|Temporary failure in name resolution|"
313+
"No tables found"
314+
)
315+
with pytest.raises((URLError, ValueError), match=msg):
316+
self.read_html("http://www.a23950sdfa908sd.com", match=".*Water.*")
316317

317318
@pytest.mark.slow
318319
def test_file_url(self):
@@ -949,8 +950,13 @@ def test_decimal_rows(self):
949950

950951
def test_bool_header_arg(self):
951952
# GH 6114
953+
msg = re.escape(
954+
"Passing a bool to header is invalid. Use header=None for no header or "
955+
"header=int or list-like of ints to specify the row(s) making up the "
956+
"column names"
957+
)
952958
for arg in [True, False]:
953-
with pytest.raises(TypeError):
959+
with pytest.raises(TypeError, match=msg):
954960
self.read_html(self.spam_data, header=arg)
955961

956962
def test_converters(self):

0 commit comments

Comments
 (0)