Skip to content

Commit 65db05a

Browse files
authored
pytest gallery tests (#4792)
* pytest parametrize gallery tests * two context managers as fixtures * use pathlib instead of os * docstrings * use pytest.mark.filterwarnings * show_replaced_by_check_graphic as fixture * Revert previous (more readable without) This reverts commit bdca27c. * gallery_dir function to variable * add setup/teardown fixture * tweak docstring * remove gallerytests from Makefile * update dev guide * doc tweaks * use monkeypatch to simplify * move stuff about * monkeypatch for sys.path; comments and param ids * revert assert False * revert deliberate fails * import_patches fixture * post rebase check_graphic fix * update test references * whatsnew * test data version
1 parent da85988 commit 65db05a

33 files changed

+163
-851
lines changed

.github/workflows/benchmark.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
env:
1616
IRIS_TEST_DATA_LOC_PATH: benchmarks
1717
IRIS_TEST_DATA_PATH: benchmarks/iris-test-data
18-
IRIS_TEST_DATA_VERSION: "2.14"
18+
IRIS_TEST_DATA_VERSION: "2.15"
1919
# Lets us manually bump the cache to rebuild
2020
ENV_CACHE_BUILD: "0"
2121
TEST_DATA_CACHE_BUILD: "2"

.github/workflows/ci-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
session: "tests"
4747

4848
env:
49-
IRIS_TEST_DATA_VERSION: "2.14"
49+
IRIS_TEST_DATA_VERSION: "2.15"
5050
ENV_NAME: "ci-tests"
5151

5252
steps:

docs/Makefile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,3 @@ linkcheck:
5555
echo "Running linkcheck in $$i..."; \
5656
(cd $$i; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) linkcheck); done
5757

58-
gallerytest:
59-
@echo
60-
@echo "Running \"gallery\" tests..."
61-
@echo
62-
python -m unittest discover -v -t .

docs/gallery_tests/conftest.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright Iris contributors
2+
#
3+
# This file is part of Iris and is released under the LGPL license.
4+
# See COPYING and COPYING.LESSER in the root of the repository for full
5+
# licensing details.
6+
7+
"""Pytest fixtures for the gallery tests."""
8+
9+
import pathlib
10+
11+
import matplotlib.pyplot as plt
12+
import pytest
13+
14+
import iris
15+
16+
CURRENT_DIR = pathlib.Path(__file__).resolve()
17+
GALLERY_DIR = CURRENT_DIR.parents[1] / "gallery_code"
18+
19+
20+
@pytest.fixture
21+
def image_setup_teardown():
22+
"""
23+
Setup and teardown fixture.
24+
25+
Ensures all figures are closed before and after test to prevent one test
26+
polluting another if it fails with a figure unclosed.
27+
28+
"""
29+
plt.close("all")
30+
yield
31+
plt.close("all")
32+
33+
34+
@pytest.fixture
35+
def import_patches(monkeypatch):
36+
"""
37+
Replace plt.show() with a function that does nothing, also add all the
38+
gallery examples to sys.path.
39+
40+
"""
41+
42+
def no_show():
43+
pass
44+
45+
monkeypatch.setattr(plt, "show", no_show)
46+
47+
for example_dir in GALLERY_DIR.iterdir():
48+
if example_dir.is_dir():
49+
monkeypatch.syspath_prepend(example_dir)
50+
51+
yield
52+
53+
54+
@pytest.fixture
55+
def iris_future_defaults():
56+
"""
57+
Create a fixture which resets all the iris.FUTURE settings to the defaults,
58+
as otherwise changes made in one test can affect subsequent ones.
59+
60+
"""
61+
# Run with all default settings in iris.FUTURE.
62+
default_future_kwargs = iris.Future().__dict__.copy()
63+
for dead_option in iris.Future.deprecated_options:
64+
# Avoid a warning when setting these !
65+
del default_future_kwargs[dead_option]
66+
with iris.FUTURE.context(**default_future_kwargs):
67+
yield

docs/gallery_tests/gallerytest_util.py

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright Iris contributors
2+
#
3+
# This file is part of Iris and is released under the LGPL license.
4+
# See COPYING and COPYING.LESSER in the root of the repository for full
5+
# licensing details.
6+
7+
import importlib
8+
9+
import matplotlib.pyplot as plt
10+
import pytest
11+
12+
from iris.tests import _RESULT_PATH
13+
from iris.tests.graphics import check_graphic
14+
15+
from .conftest import GALLERY_DIR
16+
17+
18+
def gallery_examples():
19+
"""Generator to yield all current gallery examples."""
20+
21+
for example_file in GALLERY_DIR.glob("*/plot*.py"):
22+
yield example_file.stem
23+
24+
25+
@pytest.mark.filterwarnings("error::iris.IrisDeprecation")
26+
@pytest.mark.parametrize("example", gallery_examples())
27+
def test_plot_example(
28+
example,
29+
image_setup_teardown,
30+
import_patches,
31+
iris_future_defaults,
32+
):
33+
"""Test that all figures from example code match KGO."""
34+
35+
module = importlib.import_module(example)
36+
37+
# Run example.
38+
module.main()
39+
# Loop through open figures and set each to be the current figure so check_graphic
40+
# will find it.
41+
for fig_num in plt.get_fignums():
42+
plt.figure(fig_num)
43+
image_id = f"gallery_tests.test_{example}.{fig_num - 1}"
44+
check_graphic(image_id, _RESULT_PATH)

docs/gallery_tests/test_plot_COP_1d.py

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

docs/gallery_tests/test_plot_COP_maps.py

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

docs/gallery_tests/test_plot_SOI_filtering.py

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

docs/gallery_tests/test_plot_TEC.py

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

0 commit comments

Comments
 (0)