Skip to content

Commit

Permalink
import_patches fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
rcomer committed Jun 20, 2022
1 parent 51847c2 commit f782d73
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
24 changes: 24 additions & 0 deletions docs/gallery_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

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

import pathlib

import matplotlib.pyplot as plt
import pytest

import iris

CURRENT_DIR = pathlib.Path(__file__).resolve()
GALLERY_DIR = CURRENT_DIR.parents[1] / "gallery_code"


@pytest.fixture
def image_setup_teardown():
Expand All @@ -27,6 +31,26 @@ def image_setup_teardown():
plt.close("all")


@pytest.fixture
def import_patches(monkeypatch):
"""
Replace plt.show() with a function that does nothing, also add all the
gallery examples to sys.path.
"""

def no_show():
pass

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

for example_dir in GALLERY_DIR.iterdir():
if example_dir.is_dir():
monkeypatch.syspath_prepend(example_dir)

yield


@pytest.fixture
def iris_future_defaults():
"""
Expand Down
33 changes: 9 additions & 24 deletions docs/gallery_tests/test_gallery_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,39 @@
# licensing details.

import importlib
import pathlib

import matplotlib.pyplot as plt
import pytest

from iris.tests import check_graphic

from .conftest import GALLERY_DIR


def gallery_examples():
"""
Generator to yield all current gallery examples and their containing
directories.
"""Generator to yield all current gallery examples."""

"""
current_dir = pathlib.Path(__file__).resolve()
gallery_dir = current_dir.parents[1] / "gallery_code"
for example_file in gallery_dir.glob("*/plot*.py"):
yield example_file.parent, example_file.stem
for example_file in GALLERY_DIR.glob("*/plot*.py"):
yield example_file.stem


@pytest.mark.filterwarnings("error::iris.IrisDeprecation")
@pytest.mark.parametrize("example", gallery_examples(), ids=lambda arg: arg[1])
@pytest.mark.parametrize("example", gallery_examples())
def test_plot_example(
example,
image_setup_teardown,
import_patches,
iris_future_defaults,
monkeypatch,
):
"""Test that all figures from example code match KGO."""

example_dir, example_code = example

# Replace pyplot.show with a function that does nothing, so all figures from the
# example are still open after it runs.
def no_show():
pass

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

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

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

0 comments on commit f782d73

Please sign in to comment.