Skip to content

Backport PR #16940 on branch v3.2.x (DOC/FIX: clarify the docs for check_figures_equal) #16974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/matplotlib/testing/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ def check_figures_equal(*, extensions=("png", "pdf", "svg"), tol=0):
"""
Decorator for test cases that generate and compare two figures.

The decorated function must take two arguments, *fig_test* and *fig_ref*,
and draw the test and reference images on them. After the function
returns, the figures are saved and compared.
The decorated function must take two keyword arguments, *fig_test*
and *fig_ref*, and draw the test and reference images on them.
After the function returns, the figures are saved and compared.

This decorator should be preferred over `image_comparison` when possible in
order to keep the size of the test suite from ballooning.
Expand All @@ -381,6 +381,7 @@ def check_figures_equal(*, extensions=("png", "pdf", "svg"), tol=0):
def test_plot(fig_test, fig_ref):
fig_test.subplots().plot([1, 3, 5])
fig_ref.subplots().plot([0, 1, 2], [1, 3, 5])

"""
ALLOWED_CHARS = set(string.digits + string.ascii_letters + '_-[]()')
KEYWORD_ONLY = inspect.Parameter.KEYWORD_ONLY
Expand All @@ -390,6 +391,11 @@ def decorator(func):
_, result_dir = _image_directories(func)
old_sig = inspect.signature(func)

if not {"fig_test", "fig_ref"}.issubset(old_sig.parameters):
raise ValueError("The decorated function must have at least the "
"parameters 'fig_ref' and 'fig_test', but your "
f"function has the signature {old_sig}")

@pytest.mark.parametrize("ext", extensions)
def wrapper(*args, **kwargs):
ext = kwargs['ext']
Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ def test_warn_to_fail():
@pytest.mark.parametrize("b", [1])
def test_parametrize_with_check_figure_equal(a, fig_ref, b, fig_test):
assert a == b


def test_wrap_failure():
with pytest.raises(ValueError, match="^The decorated function"):
@check_figures_equal()
def should_fail(test, ref):
pass