Skip to content

Commit 7bca716

Browse files
ESadek-MObjlittleweb-flowstephenworsleypp-mo
authored
Merge pytesting contributions to main (#6207)
* Add pytest-mock dependency (#5811) * Updated environment lockfiles * added pytest iris class (#5808) * added pytest iris class * actioned majority of review comments * converted remaining functions to camel_case * actioned majority of review comments * make unit/config pytest (#5810) * Convert all tests to pytest. * Don't use staticmethod on fixtures. * Everything in 'TestConcatenate__dask' can use the same sample cubes. * Spurious parentheses in 'pytest.fixture()' * Convert all tests to pytest. * Rewrite `result_path()` for pytest (#5817) * Rewrite result_path. * Review comments. * Configure pytest-mock as pytest required-plugin (#5815) * pytest migration ruff PT compliance for unit.concatenate (#5823) * pytest migration for unit.common.test_Lenient (#5822) * pytest migration for unit.common.lenient.test_Lenient * assert order * review actions * Convert to pytest. * Better implementation of `_shared_utils.results_path()` - using PyTest `request` (#5827) * Rewrite result_path to use the pytest request fixture. * More explicit guidance on the request fixture. * Convert to pytest. * Convert to pytest. * Convert graphics testing conveniences to PyTest (#5832) * PyTest-compatible check_graphic. * Demonstrate new check_graphic in test_plot. * 7c4f700 * Revert "7c4f7003a" This reverts commit 68f81ac. * Revert "Demonstrate new check_graphic in test_plot." This reverts commit 7c4f700. * Tidy up other unittest references in iris.tests.graphics. * Tidy up other unittest references in iris.tests.graphics. * check_graphic_caller docstring example. * Make check_graphic_caller only accessible from conftest. * Simplify data fixtures. * Simplify data fixtures. * Simplify data fixtures. * Simplify Mock usage. * Tidy equality checks. * Convert to pytest, * pytest migration for unit.common.lenient.test__lenient_client (#5842) * pytest migration for unit.common.lenient.test__lenient_client * review actions * pytest migration for unit.common.lenient.test__Lenient (#5828) * pytest migration for unit.common.lenient.test__Lenient * ruff PT rule compliant * fixes * refactor * assert fixes * review actions * Convert to pytest. * Convert `tests/test_plot` to PyTest (#5839) * Convert test_plot to PyTest. * Add explanatory comments. * Make ruff PT compliant. * Convert test_quickplot. * Test aggregate pytest (#5846) * pytestify test_aggregate_by. * pytestify test_lazy_aggregate_by. * pytest migration for unit.common.lenient.test__lenient_service (#5843) * pytest migration for unit.common.mixin.test__get_valid_standard_name (#5847) * pytest migration for unit.common.mixin.test_CFVariableMixin (#5849) * pytest migration for unit.common.lenient.test__qualname (#5845) * pytest migration for unit.common.lenient.test__qualname * use setup_method * use setup fixture * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * pytest migration for unit.common.mixin.test_LimitedAttributeDict (#5851) * pytest migration for unit.common.mixin.test_LimitedAttributeDict * use setup_method * use setup fixture * pytest migration for unit.common.metadata.test_NamedTupleMeta (#5852) * Make test_analysis PyTest (#5859) * Fixes to _shared_utils. * Make test_analysis.py pytest. * Make test_analysis_calculus.py pytest. * Remove np.testing. * Convert to pytest. * Convert unit/fileformats/__init__ code to pytest + move to pp_load_rules where it belongs. * Convert unit/fileformats/pp_load_rules to pytest. * converted cube directory tests to pytest (#5837) * converted asserts to pytest * pytestified and search replaced. Ruffed * resolved old references * refactored preexisting setup_methods into _setup * further changes * fixed mock and CML assert * fixed cdl call in shared_utils * all tests passing * review changes * tests passing * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Convert tests in unit/util to pytest (#5853) * pytestify, and test__coord_regular * search and replace * ruff * fixed most failures * renamed result dir * fixed review comments * whoops, incorrectly called assert_original_metadata with request after removing it * corrected assert * Use mocker.spy in place of mock.patch. * pytest migration for unit.common.metadata.test_AncillaryVariableMetadata (#5854) * pytest migration for unit.common.metadata.test_AncillaryVariableMetadata * use setup fixture * review actions * Convert `unit.plot` and `.quickplot` to PyTest (#5866) * Convert tests.unit.plot to PyTest. * Convert tests.unit.quickplot to PyTest. * Convert setups to autouse fixtures. * Convert to Pytest. * Convert to Pytest. * Convert to pytest. * Convert to pytest, move test_rules into /rules. * Convert to pytest * Convert remaining graphics tests to PyTest (#5867) * Convert test_mapping to PyTest. * Convert integration.plot to PyTest. * Convert unit/fileformats/structured_array_identification to pytest. * Review changes. * pytest migration for unit.common.metadata.test_BaseMetadata (#5868) * pytest migration for unit.common.metadata.test_BaseMetadata * review actions * pytest migration of unit.common.metadata.test_hexdigest (#5874) * pytest migration of unit.common.metadata.test_metadata_filter (#5875) * pytest migration for unit.common.metadata.test_CubeMetadata (#5881) * pytest migration of unit.common.metadata.test_metadata_manager_factory (#5876) * pytest migration of unit.common.metadata.test_metadata_manager_factory * review actions * More root pytest (#5883) * Convert root test_util to PyTest. * Convert root test_pp to PyTest. * Review comments - make tests easier to understand. * pytest migration of unit.common.metadata.test_CellMeasureMetadata (#5878) * pytest migration of unit.common.metadata.test_CellMeasureMetadata * Update lib/iris/tests/unit/common/metadata/test_CellMeasureMetadata.py Co-authored-by: Patrick Peglar <[email protected]> * Update lib/iris/tests/unit/common/metadata/test_CellMeasureMetadata.py Co-authored-by: Patrick Peglar <[email protected]> * Update lib/iris/tests/unit/common/metadata/test_CellMeasureMetadata.py Co-authored-by: Patrick Peglar <[email protected]> * Update lib/iris/tests/unit/common/metadata/test_CellMeasureMetadata.py Co-authored-by: Patrick Peglar <[email protected]> * review actions --------- Co-authored-by: Patrick Peglar <[email protected]> * pytest migration of unit.common.metadata.test_CoordMetadata (#5880) * pytest migration of unit.common.metadata.test_CoordMetadata * review actions * Update lib/iris/tests/unit/common/metadata/test_CoordMetadata.py Co-authored-by: Patrick Peglar <[email protected]> --------- Co-authored-by: Patrick Peglar <[email protected]> * pytest migration of unit.common.resolve.test_Resolve (#5882) * pytest migration of unit.common.resolve.test_Resolve * review actions * regen lockfiles * precommit fixes * fix some test failures * Added a style guide for iris pytest (#5785) * Added a draft style guide for iris pytest * most review comments * refactored documentation * fixed doclinks * reslolved review comments * removed excess pages * conversion checklist * pre-lunch changes * majority review requests, rough reshuffle of Test Categories * further reshuffle of Test Categories * review stuffies * fixed a coup of review comments * fixed a doctest failures * reworded function and class intros --------- Co-authored-by: Martin Yeo <[email protected]> * added whatsnew entries (#6211) * added whatsnew entries * corrected _ to - in githubname * whatsnew corrections --------- Co-authored-by: Martin Yeo <[email protected]> --------- Co-authored-by: Bill Little <[email protected]> Co-authored-by: Lockfile bot <[email protected]> Co-authored-by: stephenworsley <[email protected]> Co-authored-by: Patrick Peglar <[email protected]> Co-authored-by: Martin Yeo <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Henry Wright <[email protected]>
1 parent 836e179 commit 7bca716

File tree

158 files changed

+10234
-9556
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+10234
-9556
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+

docs/src/developers_guide/contributing_running_tests.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ experimental dependency not being present.
8787
SKIPPED [1] lib/iris/tests/unit/util/test_demote_dim_coord_to_aux_coord.py:29: Test(s) require external data.
8888
8989
All Python decorators that skip tests will be defined in
90-
``lib/iris/tests/__init__.py`` with a function name with a prefix of
90+
``lib/iris/tests/_shared_utils.py`` with a function name with a prefix of
9191
``skip_``.
9292

9393
You can also run a specific test module. The example below runs the tests for

docs/src/developers_guide/contributing_testing.rst

Lines changed: 0 additions & 147 deletions
This file was deleted.

docs/src/developers_guide/contributing_testing_index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Testing
66
.. toctree::
77
:maxdepth: 3
88

9-
contributing_testing
10-
testing_tools
9+
contributing_tests
1110
contributing_graphics_tests
1211
contributing_running_tests
1312
contributing_ci_tests
1413
contributing_benchmarks
14+
contributing_pytest_conversions

0 commit comments

Comments
 (0)