Skip to content

Commit b01325c

Browse files
authored
Merge pull request #16940 from tacaswell/doc_compare_figures_equal
DOC/FIX: clarify the docs for check_figures_equal
2 parents 2632ac9 + 0a33534 commit b01325c

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/matplotlib/testing/decorators.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ def check_figures_equal(*, extensions=("png", "pdf", "svg"), tol=0):
359359
"""
360360
Decorator for test cases that generate and compare two figures.
361361
362-
The decorated function must take two arguments, *fig_test* and *fig_ref*,
363-
and draw the test and reference images on them. After the function
364-
returns, the figures are saved and compared.
362+
The decorated function must take two keyword arguments, *fig_test*
363+
and *fig_ref*, and draw the test and reference images on them.
364+
After the function returns, the figures are saved and compared.
365365
366366
This decorator should be preferred over `image_comparison` when possible in
367367
order to keep the size of the test suite from ballooning.
@@ -382,6 +382,7 @@ def check_figures_equal(*, extensions=("png", "pdf", "svg"), tol=0):
382382
def test_plot(fig_test, fig_ref):
383383
fig_test.subplots().plot([1, 3, 5])
384384
fig_ref.subplots().plot([0, 1, 2], [1, 3, 5])
385+
385386
"""
386387
ALLOWED_CHARS = set(string.digits + string.ascii_letters + '_-[]()')
387388
KEYWORD_ONLY = inspect.Parameter.KEYWORD_ONLY
@@ -391,6 +392,11 @@ def decorator(func):
391392
_, result_dir = _image_directories(func)
392393
old_sig = inspect.signature(func)
393394

395+
if not {"fig_test", "fig_ref"}.issubset(old_sig.parameters):
396+
raise ValueError("The decorated function must have at least the "
397+
"parameters 'fig_ref' and 'fig_test', but your "
398+
f"function has the signature {old_sig}")
399+
394400
@pytest.mark.parametrize("ext", extensions)
395401
def wrapper(*args, **kwargs):
396402
ext = kwargs['ext']

lib/matplotlib/tests/test_testing.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ def test_warn_to_fail():
1515
@pytest.mark.parametrize("b", [1])
1616
def test_parametrize_with_check_figure_equal(a, fig_ref, b, fig_test):
1717
assert a == b
18+
19+
20+
def test_wrap_failure():
21+
with pytest.raises(ValueError, match="^The decorated function"):
22+
@check_figures_equal()
23+
def should_fail(test, ref):
24+
pass

0 commit comments

Comments
 (0)