diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index 61ca2e7f5f19d..9a0788ea068ad 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -1217,14 +1217,14 @@ def test_read_missing_key_opened_store(self, setup_path): df = pd.DataFrame({"a": range(2), "b": range(2)}) df.to_hdf(path, "k1") - store = pd.HDFStore(path, "r") + with pd.HDFStore(path, "r") as store: - with pytest.raises(KeyError, match="'No object named k2 in the file'"): - pd.read_hdf(store, "k2") + with pytest.raises(KeyError, match="'No object named k2 in the file'"): + pd.read_hdf(store, "k2") - # Test that the file is still open after a KeyError and that we can - # still read from it. - pd.read_hdf(store, "k1") + # Test that the file is still open after a KeyError and that we can + # still read from it. + pd.read_hdf(store, "k1") def test_append_frame_column_oriented(self, setup_path): with ensure_clean_store(setup_path) as store: