6666class TestHDFStore :
6767 def test_format_kwarg_in_constructor (self , setup_path ):
6868 # GH 13291
69+
70+ msg = "format is not a defined argument for HDFStore"
71+
6972 with ensure_clean_path (setup_path ) as path :
70- with pytest .raises (ValueError ):
73+ with pytest .raises (ValueError , match = msg ):
7174 HDFStore (path , format = "table" )
7275
7376 def test_context (self , setup_path ):
@@ -203,21 +206,27 @@ def test_api(self, setup_path):
203206 # Invalid.
204207 df = tm .makeDataFrame ()
205208
206- with pytest .raises (ValueError ):
209+ msg = "Can only append to Tables"
210+
211+ with pytest .raises (ValueError , match = msg ):
207212 df .to_hdf (path , "df" , append = True , format = "f" )
208213
209- with pytest .raises (ValueError ):
214+ with pytest .raises (ValueError , match = msg ):
210215 df .to_hdf (path , "df" , append = True , format = "fixed" )
211216
212- with pytest .raises (TypeError ):
217+ msg = r"invalid HDFStore format specified \[foo\]"
218+
219+ with pytest .raises (TypeError , match = msg ):
213220 df .to_hdf (path , "df" , append = True , format = "foo" )
214221
215- with pytest .raises (TypeError ):
216- df .to_hdf (path , "df" , append = False , format = "bar " )
222+ with pytest .raises (TypeError , match = msg ):
223+ df .to_hdf (path , "df" , append = False , format = "foo " )
217224
218225 # File path doesn't exist
219226 path = ""
220- with pytest .raises (FileNotFoundError ):
227+ msg = f"File { path } does not exist"
228+
229+ with pytest .raises (FileNotFoundError , match = msg ):
221230 read_hdf (path , "df" )
222231
223232 def test_api_default_format (self , setup_path ):
@@ -230,7 +239,10 @@ def test_api_default_format(self, setup_path):
230239 _maybe_remove (store , "df" )
231240 store .put ("df" , df )
232241 assert not store .get_storer ("df" ).is_table
233- with pytest .raises (ValueError ):
242+
243+ msg = "Can only append to Tables"
244+
245+ with pytest .raises (ValueError , match = msg ):
234246 store .append ("df2" , df )
235247
236248 pd .set_option ("io.hdf.default_format" , "table" )
@@ -251,7 +263,7 @@ def test_api_default_format(self, setup_path):
251263 df .to_hdf (path , "df" )
252264 with HDFStore (path ) as store :
253265 assert not store .get_storer ("df" ).is_table
254- with pytest .raises (ValueError ):
266+ with pytest .raises (ValueError , match = msg ):
255267 df .to_hdf (path , "df2" , append = True )
256268
257269 pd .set_option ("io.hdf.default_format" , "table" )
@@ -384,7 +396,10 @@ def test_versioning(self, setup_path):
384396 # this is an error because its table_type is appendable, but no
385397 # version info
386398 store .get_node ("df2" )._v_attrs .pandas_version = None
387- with pytest .raises (Exception ):
399+
400+ msg = "'NoneType' object has no attribute 'startswith'"
401+
402+ with pytest .raises (Exception , match = msg ):
388403 store .select ("df2" )
389404
390405 def test_mode (self , setup_path ):
@@ -428,7 +443,11 @@ def check(mode):
428443
429444 # conv read
430445 if mode in ["w" ]:
431- with pytest .raises (ValueError ):
446+ msg = (
447+ "mode w is not allowed while performing a read. "
448+ r"Allowed modes are r, r\+ and a."
449+ )
450+ with pytest .raises (ValueError , match = msg ):
432451 read_hdf (path , "df" , mode = mode )
433452 else :
434453 result = read_hdf (path , "df" , mode = mode )
0 commit comments