Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/src/developers_guide/testing_tools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ Custom assertions
=================

:class:`iris.tests.IrisTest` supports a variety of custom unittest-style
assertions, such as :meth:`~iris.tests.IrisTest_nometa.assertStringEqual`,
:meth:`~iris.tests.IrisTest_nometa.assertArrayEqual`,
assertions, such as :meth:`~iris.tests.IrisTest_nometa.assertArrayEqual`,
:meth:`~iris.tests.IrisTest_nometa.assertArrayAlmostEqual`.

.. _create-missing:
Expand Down
2 changes: 2 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ This document explains the changes made to Iris for this release
unlimited time dimension.
(:pull:`4827`)

#. `@rcomer`_ removed some now redundant testing methods. (:pull:`4838`)


.. comment
Whatsnew author names (@github name) in alphabetical order. Note that,
Expand Down
29 changes: 0 additions & 29 deletions lib/iris/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,25 +223,6 @@ def get_result_path(relative_path):
relative_path = os.path.join(*relative_path)
return os.path.abspath(os.path.join(_RESULT_PATH, relative_path))

def assertStringEqual(
self, reference_str, test_str, type_comparison_name="strings"
):
if reference_str != test_str:
diff = "\n".join(
difflib.unified_diff(
reference_str.splitlines(),
test_str.splitlines(),
"Reference",
"Test result",
"",
"",
0,
)
)
self.fail(
"{} do not match:\n{}".format(type_comparison_name, diff)
)

def result_path(self, basename=None, ext=""):
"""
Return the full path to a test result, generated from the \
Expand Down Expand Up @@ -560,16 +541,6 @@ def _recordWarningMatches(self, expected_regexp=""):
expr = re.compile(expected_regexp)
matches.extend(message for message in messages if expr.search(message))

@contextlib.contextmanager
def assertWarnsRegexp(self, expected_regexp=""):
# Check that a warning is raised matching a given expression.
with self._recordWarningMatches(expected_regexp) as matches:
yield

msg = "Warning matching '{}' not raised."
msg = msg.format(expected_regexp)
self.assertTrue(matches, msg)

@contextlib.contextmanager
def assertLogs(self, logger=None, level=None, msg_regex=None):
"""
Expand Down
14 changes: 7 additions & 7 deletions lib/iris/tests/integration/test_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,15 +599,15 @@ def test_load_datum_wkt(self):
cube = iris.load_cube(nc_path)
test_crs = cube.coord("projection_y_coordinate").coord_system
actual = str(test_crs.as_cartopy_crs().datum)
self.assertStringEqual(expected, actual)
self.assertMultiLineEqual(expected, actual)

def test_no_load_datum_wkt(self):
nc_path = tlc.cdl_to_nc(self.datum_wkt_cdl)
with self.assertWarnsRegexp("iris.FUTURE.datum_support"):
with self.assertWarnsRegex(FutureWarning, "iris.FUTURE.datum_support"):
cube = iris.load_cube(nc_path)
test_crs = cube.coord("projection_y_coordinate").coord_system
actual = str(test_crs.as_cartopy_crs().datum)
self.assertStringEqual(actual, "unknown")
self.assertMultiLineEqual(actual, "unknown")

def test_load_datum_cf_var(self):
expected = "OSGB 1936"
Expand All @@ -616,15 +616,15 @@ def test_load_datum_cf_var(self):
cube = iris.load_cube(nc_path)
test_crs = cube.coord("projection_y_coordinate").coord_system
actual = str(test_crs.as_cartopy_crs().datum)
self.assertStringEqual(expected, actual)
self.assertMultiLineEqual(expected, actual)

def test_no_load_datum_cf_var(self):
nc_path = tlc.cdl_to_nc(self.datum_cf_var_cdl)
with self.assertWarnsRegexp("iris.FUTURE.datum_support"):
with self.assertWarnsRegex(FutureWarning, "iris.FUTURE.datum_support"):
cube = iris.load_cube(nc_path)
test_crs = cube.coord("projection_y_coordinate").coord_system
actual = str(test_crs.as_cartopy_crs().datum)
self.assertStringEqual(actual, "unknown")
self.assertMultiLineEqual(actual, "unknown")

def test_save_datum(self):
expected = "OSGB 1936"
Expand Down Expand Up @@ -663,7 +663,7 @@ def test_save_datum(self):

test_crs = cube.coord("projection_y_coordinate").coord_system
actual = str(test_crs.as_cartopy_crs().datum)
self.assertStringEqual(expected, actual)
self.assertMultiLineEqual(expected, actual)


def _get_scale_factor_add_offset(cube, datatype):
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/tests/test_cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def test_destructor(self):
except OSError:
pass
buf.seek(0)
self.assertStringEqual("", buf.read())
self.assertMultiLineEqual("", buf.read())


@tests.skip_data
Expand Down
6 changes: 3 additions & 3 deletions lib/iris/tests/test_coordsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,18 +548,18 @@ class Test_Datums(tests.IrisTest):
def test_default_none(self):
cs = GeogCS(6543210, 6500000) # Arbitrary radii
cartopy_crs = cs.as_cartopy_crs()
self.assertStringEqual(cartopy_crs.datum.name, "unknown")
self.assertMultiLineEqual(cartopy_crs.datum.name, "unknown")

def test_set_persist(self):
cs = GeogCS.from_datum(datum="WGS84")
cartopy_crs = cs.as_cartopy_crs()
self.assertStringEqual(
self.assertMultiLineEqual(
cartopy_crs.datum.name, "World Geodetic System 1984"
)

cs = GeogCS.from_datum(datum="OSGB36")
cartopy_crs = cs.as_cartopy_crs()
self.assertStringEqual(cartopy_crs.datum.name, "OSGB 1936")
self.assertMultiLineEqual(cartopy_crs.datum.name, "OSGB 1936")


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUp(self):

def test_cube_attributes(self):
self.assertEqual(id(self.cube), self.representer.cube_id)
self.assertStringEqual(str(self.cube), self.representer.cube_str)
self.assertMultiLineEqual(str(self.cube), self.representer.cube_str)

def test__heading_contents(self):
content = set(self.representer.sections_data.values())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def load_cube_from_cdl(self, cdl_string, cdl_path, nc_path):
# Always returns a single cube.
return cube

def run_testcase(self, warning=None, **testcase_kwargs):
def run_testcase(self, warning_regex=None, **testcase_kwargs):
"""
Run a testcase with chosen options, returning a test cube.

Expand All @@ -133,10 +133,10 @@ def run_testcase(self, warning=None, **testcase_kwargs):
print(cdl_string)
print("------\n")

if warning is None:
if warning_regex is None:
context = self.assertNoWarningsRegexp()
else:
context = self.assertWarnsRegexp(warning)
context = self.assertWarnsRegex(UserWarning, warning_regex)
with context:
cube = self.load_cube_from_cdl(cdl_string, cdl_path, nc_path)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,9 @@ def test_latlon_bad_gridmapping_varname(self):
# Notes:
# * behaviours all the same as 'test_bad_gridmapping_nameproperty'
warning = "Missing.*grid mapping variable 'grid'"
result = self.run_testcase(warning=warning, gridmapvar_name="grid_2")
result = self.run_testcase(
warning_regex=warning, gridmapvar_name="grid_2"
)
self.check_result(result, cube_no_cs=True)

def test_latlon_bad_latlon_unit(self):
Expand Down Expand Up @@ -633,7 +635,7 @@ def test_mapping__mismatch__latlon_coords_missing_system(self):
# * coords built : lat + lon, with no coord-system (see above)
warning = "Missing.*grid mapping variable 'grid'"
result = self.run_testcase(
warning=warning,
warning_regex=warning,
gridmapvar_name="moved",
xco_name="longitude",
xco_units="degrees_east",
Expand Down Expand Up @@ -690,7 +692,7 @@ def test_mapping__mismatch__rotated_coords_missing_system(self):
# * coords built : rotated lat + lon, with no coord-system (see above)
warning = "Missing.*grid mapping variable 'grid'"
result = self.run_testcase(
warning=warning,
warning_regex=warning,
gridmapvar_name="moved",
xco_name="grid_longitude",
xco_units="degrees",
Expand Down Expand Up @@ -752,7 +754,7 @@ def test_mapping__mismatch__nonll_coords_missing_system(self):
# * effectively, just like previous 2 cases
warning = "Missing.*grid mapping variable 'grid'"
result = self.run_testcase(
warning=warning,
warning_regex=warning,
gridmapvar_name="moved",
xco_name="projection_x",
xco_units="m",
Expand Down Expand Up @@ -872,7 +874,9 @@ def test_nondim_lats(self):
# * in terms of rule triggering, this is not distinct from the
# "normal" case : but latitude is now created as an aux-coord.
warning = "must be.* monotonic"
result = self.run_testcase(warning=warning, yco_values=[0.0, 0.0])
result = self.run_testcase(
warning_regex=warning, yco_values=[0.0, 0.0]
)
self.check_result(result, yco_is_aux=True)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def test_unrecognised_verticaltype(self):
result = self.run_testcase(
formula_root_name="unknown",
term_names=["a", "b"],
warning="Ignored formula of unrecognised type: 'unknown'.",
warning_regex="Ignored formula of unrecognised type: 'unknown'.",
)
# Check that it picks up the terms, but *not* the factory root coord,
# which is simply discarded.
Expand All @@ -226,7 +226,7 @@ def test_two_formulae(self):

extra_type = "ocean_sigma_coordinate"
result = self.run_testcase(
extra_formula_type=extra_type, warning=warning
extra_formula_type=extra_type, warning_regex=warning
)
# NOTE: FOR NOW, check expected behaviour : only one factory will be
# built, but there are coordinates (terms) for both types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def test_dim_nonmonotonic(self):
# 002 : fc_provides_coordinate_(time[[_period]])
# 003 : fc_build_coordinate_(time[[_period]])
msg = "Failed to create.* dimension coordinate"
result = self.run_testcase(values_all_zero=True, warning=msg)
result = self.run_testcase(values_all_zero=True, warning_regex=msg)
self.check_result(result, "aux")

def test_dim_fails_typeident(self):
Expand Down
15 changes: 9 additions & 6 deletions lib/iris/tests/unit/fileformats/netcdf/test_Saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,9 @@ def test_contains_fill_value_passed(self):
# Test that a warning is raised if the data contains the fill value.
cube = self._make_cube(">f4")
fill_value = 1
with self.assertWarnsRegexp(
"contains unmasked data points equal to the fill-value"
with self.assertWarnsRegex(
UserWarning,
"contains unmasked data points equal to the fill-value",
):
with self._netCDF_var(cube, fill_value=fill_value):
pass
Expand All @@ -560,8 +561,9 @@ def test_contains_fill_value_byte(self):
# when it is of a byte type.
cube = self._make_cube(">i1")
fill_value = 1
with self.assertWarnsRegexp(
"contains unmasked data points equal to the fill-value"
with self.assertWarnsRegex(
UserWarning,
"contains unmasked data points equal to the fill-value",
):
with self._netCDF_var(cube, fill_value=fill_value):
pass
Expand All @@ -571,8 +573,9 @@ def test_contains_default_fill_value(self):
# value if no fill_value argument is supplied.
cube = self._make_cube(">f4")
cube.data[0, 0] = nc.default_fillvals["f4"]
with self.assertWarnsRegexp(
"contains unmasked data points equal to the fill-value"
with self.assertWarnsRegex(
UserWarning,
"contains unmasked data points equal to the fill-value",
):
with self._netCDF_var(cube):
pass
Expand Down
6 changes: 3 additions & 3 deletions lib/iris/tests/unit/fileformats/pp/test_PPField.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def field_checksum(data):
data_64 = np.linspace(0, 1, num=10, endpoint=False).reshape(2, 5)
checksum_32 = field_checksum(data_64.astype(">f4"))
msg = "Downcasting array precision from float64 to float32 for save."
with self.assertWarnsRegexp(msg):
with self.assertWarnsRegex(UserWarning, msg):
checksum_64 = field_checksum(data_64.astype(">f8"))
self.assertEqual(checksum_32, checksum_64)

Expand All @@ -105,7 +105,7 @@ def test_masked_mdi_value_warning(self):
[1.0, field.bmdi, 3.0], dtype=np.float32
)
msg = "PPField data contains unmasked points"
with self.assertWarnsRegexp(msg):
with self.assertWarnsRegex(UserWarning, msg):
with self.temp_filename(".pp") as temp_filename:
with open(temp_filename, "wb") as pp_file:
field.save(pp_file)
Expand All @@ -117,7 +117,7 @@ def test_unmasked_mdi_value_warning(self):
# Make float32 data, as float64 default produces an extra warning.
field.data = np.array([1.0, field.bmdi, 3.0], dtype=np.float32)
msg = "PPField data contains unmasked points"
with self.assertWarnsRegexp(msg):
with self.assertWarnsRegex(UserWarning, msg):
with self.temp_filename(".pp") as temp_filename:
with open(temp_filename, "wb") as pp_file:
field.save(pp_file)
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/tests/unit/io/test_expand_filespecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_files_and_none(self):
.format(self.tmpdir)
)

self.assertStringEqual(str(err.exception), expected)
self.assertMultiLineEqual(str(err.exception), expected)


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion lib/iris/tests/unit/test_Future.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import warnings

from iris import Future
import iris._deprecation


def patched_future(value=False, deprecated=False, error=False):
Expand Down Expand Up @@ -45,7 +46,7 @@ def test_valid_setting(self):
def test_deprecated_warning(self):
future = patched_future(deprecated=True, error=False)
msg = "'Future' property 'example_future_flag' is deprecated"
with self.assertWarnsRegexp(msg):
with self.assertWarnsRegex(iris._deprecation.IrisDeprecation, msg):
future.example_future_flag = False

def test_deprecated_error(self):
Expand Down