Skip to content

Commit 0fddf34

Browse files
committed
monkeypatch for sys.path; comments and param ids
1 parent 901c483 commit 0fddf34

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

docs/gallery_tests/conftest.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,12 @@
66

77
"""Pytest fixtures for the gallery tests."""
88

9-
import pathlib
10-
import sys
119

1210
import matplotlib.pyplot as plt
1311
import pytest
1412

1513
import iris
1614

17-
CURRENT_DIR = pathlib.Path(__file__).resolve()
18-
GALLERY_DIR = CURRENT_DIR.parents[1] / "gallery_code"
19-
GALLERY_DIRECTORIES = [
20-
str(path) for path in GALLERY_DIR.iterdir() if path.is_dir()
21-
]
22-
23-
24-
@pytest.fixture
25-
def add_gallery_to_path():
26-
"""
27-
Creates a fixture which can be used to add the iris gallery to the
28-
PYTHONPATH. The gallery entries are only importable throughout the lifetime
29-
of the test.
30-
31-
"""
32-
orig_sys_path = sys.path
33-
sys.path = sys.path[:]
34-
sys.path += GALLERY_DIRECTORIES
35-
yield
36-
sys.path = orig_sys_path
37-
3815

3916
@pytest.fixture
4017
def image_setup_teardown():

docs/gallery_tests/test_gallery_examples.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,53 @@
55
# licensing details.
66

77
import importlib
8+
import pathlib
89

910
import matplotlib.pyplot as plt
1011
import pytest
1112

1213
from iris.tests import check_graphic
1314

14-
from .conftest import GALLERY_DIR
15-
1615

1716
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
2126

2227

2328
@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])
2530
def test_plot_example(
26-
example_code,
27-
add_gallery_to_path,
31+
example,
2832
image_setup_teardown,
2933
iris_future_defaults,
3034
monkeypatch,
3135
):
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.
3242
def no_show():
3343
pass
3444

3545
monkeypatch.setattr(plt, "show", no_show)
3646

47+
# Add example code to sys.path and import it.
48+
monkeypatch.syspath_prepend(example_dir)
3749
module = importlib.import_module(example_code)
3850

51+
# Run example.
3952
module.main()
53+
# Loop through open figures and set each to be the current figure so check_graphic
54+
# will find it.
4055
for fig_num in plt.get_fignums():
4156
plt.figure(fig_num)
4257
image_id = f"gallery_tests.test_{example_code}.{fig_num - 1}"

0 commit comments

Comments
 (0)