Skip to content

Commit a87518d

Browse files
committed
Use more elegant assertRaisesRegex method for testing exceptions
1 parent dee4a5a commit a87518d

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

climada/hazard/test/test_base_xarray.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,8 @@ def test_missing_dims(self):
359359

360360
# Now drop variable altogether, should raise an error
361361
ds = ds.drop_vars("time")
362-
with self.assertRaises(RuntimeError) as cm:
362+
with self.assertRaisesRegex(RuntimeError, "time"):
363363
Hazard.from_xarray_raster(ds, "", "")
364-
self.assertIn("time", str(cm.exception))
365364

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

566565
# Correctly specified, but the custom dimension does not exist
567-
with self.assertRaises(RuntimeError) as cm:
566+
with self.assertRaisesRegex(RuntimeError, "lalalatitude"):
568567
Hazard.from_xarray_raster_file(
569568
self.netcdf_path,
570569
"",
571570
"",
572571
coordinate_vars=dict(latitude="lalalatitude"),
573572
)
574-
self.assertIn("lalalatitude", str(cm.exception))
575-
576573

577574
# Execute Tests
578575
if __name__ == "__main__":

0 commit comments

Comments
 (0)