|
5 | 5 | # licensing details. |
6 | 6 |
|
7 | 7 | import importlib |
| 8 | +import pathlib |
8 | 9 |
|
9 | 10 | import matplotlib.pyplot as plt |
10 | 11 | import pytest |
11 | 12 |
|
12 | 13 | from iris.tests import check_graphic |
13 | 14 |
|
14 | | -from .conftest import GALLERY_DIR |
15 | | - |
16 | 15 |
|
17 | 16 | def gallery_examples(): |
18 | | - """Generator to yield all current gallery examples.""" |
19 | | - for example_file in GALLERY_DIR.glob("*/plot*.py"): |
20 | | - yield example_file.stem |
| 17 | + """ |
| 18 | + Generator to yield all current gallery examples and their containing |
| 19 | + directories. |
| 20 | +
|
| 21 | + """ |
| 22 | + current_dir = pathlib.Path(__file__).resolve() |
| 23 | + gallery_dir = current_dir.parents[1] / "gallery_code" |
| 24 | + for example_file in gallery_dir.glob("*/plot*.py"): |
| 25 | + yield example_file.parent, example_file.stem |
21 | 26 |
|
22 | 27 |
|
23 | 28 | @pytest.mark.filterwarnings("error::iris.IrisDeprecation") |
24 | | -@pytest.mark.parametrize("example_code", gallery_examples()) |
| 29 | +@pytest.mark.parametrize("example", gallery_examples(), ids=lambda arg: arg[1]) |
25 | 30 | def test_plot_example( |
26 | | - example_code, |
27 | | - add_gallery_to_path, |
| 31 | + example, |
28 | 32 | image_setup_teardown, |
29 | 33 | iris_future_defaults, |
30 | 34 | monkeypatch, |
31 | 35 | ): |
| 36 | + """Test that all figures from example code match KGO.""" |
| 37 | + |
| 38 | + example_dir, example_code = example |
| 39 | + |
| 40 | + # Replace pyplot.show with a function that does nothing, so all figures from the |
| 41 | + # example are still open after it runs. |
32 | 42 | def no_show(): |
33 | 43 | pass |
34 | 44 |
|
35 | 45 | monkeypatch.setattr(plt, "show", no_show) |
36 | 46 |
|
| 47 | + # Add example code to sys.path and import it. |
| 48 | + monkeypatch.syspath_prepend(example_dir) |
37 | 49 | module = importlib.import_module(example_code) |
38 | 50 |
|
| 51 | + # Run example. |
39 | 52 | module.main() |
| 53 | + # Loop through open figures and set each to be the current figure so check_graphic |
| 54 | + # will find it. |
40 | 55 | for fig_num in plt.get_fignums(): |
41 | 56 | plt.figure(fig_num) |
42 | 57 | image_id = f"gallery_tests.test_{example_code}.{fig_num - 1}" |
|
0 commit comments