Skip to content

Commit

Permalink
Use more elegant assertRaisesRegex method for testing exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
peanutfun committed Dec 12, 2023
1 parent dee4a5a commit a87518d
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions climada/hazard/test/test_base_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,8 @@ def test_missing_dims(self):

# Now drop variable altogether, should raise an error
ds = ds.drop_vars("time")
with self.assertRaises(RuntimeError) as cm:
with self.assertRaisesRegex(RuntimeError, "time"):
Hazard.from_xarray_raster(ds, "", "")
self.assertIn("time", str(cm.exception))

# Expand time again
ds = ds.expand_dims(time=[np.datetime64("2022-01-01")])
Expand Down Expand Up @@ -564,15 +563,13 @@ def test_errors(self):
self.assertIn("Unknown coordinates passed: '['bar']'.", str(cm.exception))

# Correctly specified, but the custom dimension does not exist
with self.assertRaises(RuntimeError) as cm:
with self.assertRaisesRegex(RuntimeError, "lalalatitude"):
Hazard.from_xarray_raster_file(
self.netcdf_path,
"",
"",
coordinate_vars=dict(latitude="lalalatitude"),
)
self.assertIn("lalalatitude", str(cm.exception))


# Execute Tests
if __name__ == "__main__":
Expand Down

0 comments on commit a87518d

Please sign in to comment.