|
| 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. |
0 commit comments