|
| 1 | +.. include:: ../common_links.inc |
| 2 | + |
| 3 | +.. _contributing_pytest_conversions: |
| 4 | + |
| 5 | +******************************************* |
| 6 | +Converting From ``unittest`` to ``pytest`` |
| 7 | +******************************************* |
| 8 | + |
| 9 | +Conversion Checklist |
| 10 | +-------------------- |
| 11 | +.. note:: |
| 12 | + Please bear in mind the following checklist is for general use; there may be |
| 13 | + some cases which require extra context or thought before implementing these changes. |
| 14 | + |
| 15 | +#. Before making any manual changes, run https://github.com/dannysepler/pytestify |
| 16 | + on the file. This does a lot of the brunt work for you! |
| 17 | +#. Check for references to :class:`iris.tests.IrisTest`. If a class inherits |
| 18 | + from this, remove the inheritance. Inheritance is unnecessary for |
| 19 | + pytest tests, so :class:`iris.tests.IrisTest` has been deprecated |
| 20 | + and its convenience methods have been moved to the |
| 21 | + :mod:`iris.tests._shared_utils` module. |
| 22 | +#. Check for references to ``unittest``. Many of the functions within unittest |
| 23 | + are also in pytest, so often you can just change where the function is imported |
| 24 | + from. |
| 25 | +#. Check for references to ``self.assert``. Pytest has a lighter-weight syntax for |
| 26 | + assertions, e.g. ``assert x == 2`` instead of ``assertEqual(x, 2)``. In the |
| 27 | + case of custom :class:`~iris.tests.IrisTest` assertions, the majority of these |
| 28 | + have been replicated in |
| 29 | + :mod:`iris.tests._shared_utils`, but with snake_case instead of camelCase. |
| 30 | + Some :class:`iris.tests.IrisTest` assertions have not been converted into |
| 31 | + :mod:`iris.tests._shared_utils`, as these were deemed easy to achieve via |
| 32 | + simple ``assert ...`` statements. |
| 33 | +#. Check for references to ``setUp()``. Replace this with ``_setup()`` instead. |
| 34 | + Ensure that this is decorated with ``@pytest.fixture(autouse=True)``. |
| 35 | + |
| 36 | + .. code-block:: python |
| 37 | +
|
| 38 | + @pytest.fixture(autouse=True) |
| 39 | + def _setup(self): |
| 40 | + ... |
| 41 | +
|
| 42 | +#. Check for references to ``@tests``. These should be changed to ``@_shared_utils``. |
| 43 | +#. Check for references to ``with mock.patch("...")``. These should be replaced with |
| 44 | + ``mocker.patch("...")``. Note, ``mocker.patch("...")`` is NOT a context manager. |
| 45 | +#. Check for ``np.testing.assert...``. This can usually be swapped for |
| 46 | + ``_shared_utils.assert...``. |
| 47 | +#. Check for references to ``super()``. Most test classes used to inherit from |
| 48 | + :class:`iris.tests.IrisTest`, so references to this should be removed. |
| 49 | +#. Check for references to ``self.tmp_dir``. In pytest, ``tmp_path`` is used instead, |
| 50 | + and can be passed into functions as a fixture. |
| 51 | +#. Check for ``if __name__ == 'main'``. This is no longer needed with pytest. |
| 52 | +#. Check for ``mock.patch("warnings.warn")``. This can be replaced with |
| 53 | + ``pytest.warns(match=message)``. |
| 54 | +#. Check the file against https://github.com/astral-sh/ruff , using ``pip install ruff`` -> |
| 55 | + ``ruff check --select PT <file>``. |
| 56 | + |
0 commit comments