Skip to content

Commit bdca27c

Browse files
committed
show_replaced_by_check_graphic as fixture
1 parent 03d5cb4 commit bdca27c

File tree

3 files changed

+37
-39
lines changed

3 files changed

+37
-39
lines changed

docs/gallery_tests/conftest.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
Pytest fixtures for the gallery tests.
99
1010
"""
11-
11+
import collections
1212
import sys
1313

14+
import matplotlib.pyplot as plt
1415
import pytest
1516

1617
import iris
18+
import iris.plot as iplt
19+
import iris.quickplot as qplt
20+
from iris.tests import check_graphic
1721

18-
from .gallerytest_util import gallery_path
22+
from .gallerytest_util import gallery_examples, gallery_path
1923

2024
GALLERY_DIRECTORY = gallery_path()
2125
GALLERY_DIRECTORIES = [
@@ -52,3 +56,28 @@ def iris_future_defaults():
5256
del default_future_kwargs[dead_option]
5357
with iris.FUTURE.context(**default_future_kwargs):
5458
yield
59+
60+
61+
@pytest.fixture(params=gallery_examples())
62+
def show_replaced_by_check_graphic(request):
63+
"""
64+
Creates a fixture which, for the gallery examples specified by params, is
65+
used to replace the functionality of matplotlib.pyplot.show with a function
66+
which calls the check_graphic function (iris.tests.check_graphic).
67+
68+
Yields the example name so it can be imported in the test.
69+
70+
"""
71+
test_id = f"gallery_tests.test_{request.param}"
72+
assertion_counts = collections.defaultdict(int)
73+
74+
def replacement_show():
75+
# form a closure on test_case and tolerance
76+
unique_id = f"{test_id}.{assertion_counts[test_id]}"
77+
assertion_counts[test_id] += 1
78+
check_graphic(unique_id)
79+
80+
orig_show = plt.show
81+
plt.show = iplt.show = qplt.show = replacement_show
82+
yield request.param
83+
plt.show = iplt.show = qplt.show = orig_show

docs/gallery_tests/gallerytest_util.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,8 @@
1010
1111
"""
1212

13-
import collections
14-
import contextlib
15-
import pathlib
16-
17-
import matplotlib.pyplot as plt
18-
19-
import iris.plot as iplt
20-
import iris.quickplot as qplt
21-
from iris.tests import check_graphic
22-
2313

24-
@contextlib.contextmanager
25-
def show_replaced_by_check_graphic(test_id):
26-
"""
27-
Creates a context manager which can be used to replace the functionality
28-
of matplotlib.pyplot.show with a function which calls the check_graphic
29-
function (iris.tests.check_graphic).
30-
31-
"""
32-
assertion_counts = collections.defaultdict(int)
33-
34-
def replacement_show():
35-
# form a closure on test_case and tolerance
36-
unique_id = f"{test_id}.{assertion_counts[test_id]}"
37-
assertion_counts[test_id] += 1
38-
check_graphic(unique_id)
39-
40-
orig_show = plt.show
41-
plt.show = iplt.show = qplt.show = replacement_show
42-
yield
43-
plt.show = iplt.show = qplt.show = orig_show
14+
import pathlib
4415

4516

4617
def gallery_path():

docs/gallery_tests/test_gallery_examples.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
import pytest
1010

11-
from .gallerytest_util import gallery_examples, show_replaced_by_check_graphic
12-
1311

1412
@pytest.mark.filterwarnings("error::iris.IrisDeprecation")
15-
@pytest.mark.parametrize("example_code", gallery_examples())
16-
def test_plot_example(example_code, add_gallery_to_path, iris_future_defaults):
17-
module = importlib.import_module(example_code)
18-
with show_replaced_by_check_graphic(f"gallery_tests.test_{example_code}"):
19-
module.main()
13+
def test_plot_example(
14+
add_gallery_to_path, iris_future_defaults, show_replaced_by_check_graphic
15+
):
16+
module = importlib.import_module(show_replaced_by_check_graphic)
17+
module.main()

0 commit comments

Comments
 (0)