Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a style guide for iris pytest #5785

Merged
Merged
Show file tree
Hide file tree
Changes from 10 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
55 changes: 55 additions & 0 deletions docs/src/developers_guide/contributing_pytest_conversions.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
.. include:: ../common_links.inc

.. _converting_from_unittest:

*******************************************
Converting From ``unittest`` to ``pytest``
*******************************************

Conversion Checklist
--------------------
.. note::
Please bear in mind the following checklist is for general use; there may be
some cases which require extra context or thought before implementing these changes.

#. Before making any manual changes, run https://github.com/dannysepler/pytestify
on the file. This does a lot of the brunt work for you!
#. Check for references to :class:`iris.tests.IrisTest`. If a class inherits
from this, remove the inheritance. Inheritance is unnecessary for
pytest tests, so :class:`iris.tests.IrisTest` has been deprecated
and its convenience methods have been moved to the
:mod:`iris.tests._shared_utils` module.
#. Check for references to ``unittest``. Many of the functions within unittest
are also in pytest, so often you can just change where the function is imported
from.
#. Check for references to ``self.assert``. Pytest has a lighter-weight syntax for
assertions, e.g. ``assert x == 2`` instead of ``assertEqual(x, 2)``. In the
case of custom :class:`~iris.tests.IrisTest` assertions, the majority of these
have been replicated in
:mod:`iris.tests._shared_utils`, but with snake_case instead of camelCase.
Some :class:`iris.tests.IrisTest` assertions have not been converted into
:mod:`iris.tests._shared_utils`, as these were deemed easy to achieve via
simple ``assert ...`` statements.
#. Check for references to ``setUp()``. Replace this with ``_setup()`` instead.
Ensure that this is decorated with ``@pytest.fixture(autouse=True)``.
.. codeblock:: python

@pytest.fixture(autouse=True)
def _setup(self):
...
trexfeathers marked this conversation as resolved.
Show resolved Hide resolved

#. Check for references to ``@tests``. These should be changed to ``@_shared_utils``.
#. Check for references to ``with mock.patch("...")``. These should be replaced with
``mocker.patch("...")``. Note, ``mocker.patch("...")`` is NOT a context manager.
#. Check for ``np.testing.assert...``. This can usually be swapped for
``_shared_utils.assert...``.
#. Check for references to ``super()``. Most test classes used to inherit from
:class:`iris.tests.IrisTest`, so references to this should be removed.
#. Check for references to ``self.tmp_dir``. In pytest, ``tmp_path`` is used instead,
and can be passed into functions as a fixture.
#. Check for ``if __name__ == 'main'``. This is no longer needed with pytest.
#. Check for ``mock.patch("warnings.warn")``. This can be replaced with
``pytest.warns(match=message)``.
#. Check the file against https://github.com/astral-sh/ruff , using ``pip install ruff`` ->
``ruff check --select PT <file>``.

2 changes: 1 addition & 1 deletion docs/src/developers_guide/contributing_running_tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ experimental dependency not being present.
SKIPPED [1] lib/iris/tests/unit/util/test_demote_dim_coord_to_aux_coord.py:29: Test(s) require external data.

All Python decorators that skip tests will be defined in
``lib/iris/tests/__init__.py`` with a function name with a prefix of
``lib/iris/tests/_shared_utils.py`` with a function name with a prefix of
``skip_``.

You can also run a specific test module. The example below runs the tests for
Expand Down
147 changes: 0 additions & 147 deletions docs/src/developers_guide/contributing_testing.rst

This file was deleted.

4 changes: 2 additions & 2 deletions docs/src/developers_guide/contributing_testing_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Testing
.. toctree::
:maxdepth: 3

contributing_testing
testing_tools
contributing_tests
contributing_graphics_tests
contributing_running_tests
contributing_ci_tests
contributing_benchmarks
contributing_pytest_conversions
Loading
Loading