Skip to content

Commit 49a510b

Browse files
authored
Developer documentation on available testing conveniences (#4600)
* Developer documentation on available testing conveniences * what's new * Update to be easily usable without code links working * Rebase
1 parent 9a561d6 commit 49a510b

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

docs/src/developers_guide/contributing_testing_index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Testing
77
:maxdepth: 3
88

99
contributing_testing
10+
testing_tools
1011
contributing_graphics_tests
1112
imagehash_index
1213
contributing_running_tests
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
.. include:: ../common_links.inc
2+
3+
.. _testing_tools:
4+
5+
Testing tools
6+
*************
7+
8+
Iris has various internal convenience functions and utilities available to
9+
support writing tests. Using these makes tests quicker and easier to write, and
10+
also consistent with the rest of Iris (which makes it easier to work with the
11+
code). Most of these conveniences are accessed through the
12+
:class:`iris.tests.IrisTest` class, from
13+
which Iris' test classes then inherit.
14+
15+
.. tip::
16+
17+
All functions listed on this page are defined within
18+
:mod:`iris.tests.__init__.py` as methods of
19+
:class:`iris.tests.IrisTest_nometa` (which :class:`iris.tests.IrisTest`
20+
inherits from). They can be accessed within a test using
21+
``self.exampleFunction``.
22+
23+
Custom assertions
24+
=================
25+
26+
:class:`iris.tests.IrisTest` supports a variety of custom unittest-style
27+
assertions, such as :meth:`~iris.tests.IrisTest_nometa.assertStringEqual`,
28+
:meth:`~iris.tests.IrisTest_nometa.assertArrayEqual`,
29+
:meth:`~iris.tests.IrisTest_nometa.assertArrayAlmostEqual`.
30+
31+
Saving results
32+
--------------
33+
34+
Some tests compare the generated output to the expected result contained in a
35+
file. Custom assertions for this include
36+
:meth:`~iris.tests.IrisTest_nometa.assertCMLApproxData`
37+
:meth:`~iris.tests.IrisTest_nometa.assertCDL`
38+
:meth:`~iris.tests.IrisTest_nometa.assertCML` and
39+
:meth:`~iris.tests.IrisTest_nometa.assertTextFile`. See docstrings for more
40+
information.
41+
42+
.. note::
43+
44+
Sometimes code changes alter the results expected from a test containing the
45+
above methods. These can be updated by removing the existing result files
46+
and then running the file containing the test with a ``--create-missing``
47+
command line argument, or setting the ``IRIS_TEST_CREATE_MISSING``
48+
environment variable to anything non-zero. This will create the files rather
49+
than erroring, allowing you to commit the updated results.
50+
51+
Context managers
52+
================
53+
54+
Capturing exceptions and logging
55+
--------------------------------
56+
57+
:class:`iris.tests.IrisTest` includes several context managers that can be used
58+
to make test code tidier and easier to read. These include
59+
:meth:`~iris.tests.IrisTest_nometa.assertWarnsRegexp` and
60+
:meth:`~iris.tests.IrisTest_nometa.assertLogs`.
61+
62+
Temporary files
63+
---------------
64+
65+
It's also possible to generate temporary files in a concise fashion with
66+
:meth:`~iris.tests.IrisTest_nometa.temp_filename`.
67+
68+
Patching
69+
========
70+
71+
:meth:`~iris.tests.IrisTest_nometa.patch` is a wrapper around ``unittest.patch``
72+
that will be automatically cleaned up at the end of the test.
73+
74+
Graphic tests
75+
=============
76+
77+
As a package capable of generating graphical outputs, Iris has utilities for
78+
creating and updating graphical tests - see :ref:`testing.graphics` for more
79+
information.

docs/src/whatsnew/latest.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ This document explains the changes made to Iris for this release
181181
#. `@rcomer`_ updated the "Load a Time Series of Data From the NEMO Model"
182182
gallery example. (:pull:`4741`)
183183

184+
#. `@wjbenfold`_ added developer documentation to highlight some of the
185+
utilities offered by :class:`iris.IrisTest` and how to update CML and other
186+
output files. (:issue:`4544`, :pull:`4600`)
187+
184188

185189
💼 Internal
186190
===========

0 commit comments

Comments
 (0)