Skip to content

Commit f736296

Browse files
committed
TST: Allow baseline_images to be a fixture.
The main point is it can be indirectly determined from other parametrizations.
1 parent abc9445 commit f736296

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/matplotlib/testing/conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ def mpl_image_comparison_parameters(request, extension):
6565
# pytest won't get confused.
6666
# We annotate the decorated function with any parameters captured by this
6767
# fixture so that they can be used by the wrapper in image_comparison.
68+
baseline_images = request.keywords['baseline_images'].args[0]
69+
if baseline_images is None:
70+
# Allow baseline image list to be produced on the fly based on current
71+
# parametrization.
72+
baseline_images = request.getfixturevalue('baseline_images')
73+
6874
func = request.function
69-
func.__wrapped__.parameters = (extension, )
75+
func.__wrapped__.parameters = (baseline_images, extension)
7076
yield
7177
delattr(func.__wrapped__, 'parameters')

lib/matplotlib/testing/decorators.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ def _pytest_image_comparison(baseline_images, extensions, tol,
344344
def decorator(func):
345345
@pytest.mark.usefixtures('mpl_image_comparison_parameters')
346346
@pytest.mark.parametrize('extension', extensions)
347+
@pytest.mark.baseline_images(baseline_images)
347348
@pytest.mark.style(style)
348349
@_checked_on_freetype_version(freetype_version)
349350
@functools.wraps(func)
@@ -359,7 +360,7 @@ def wrapper(*args, **kwargs):
359360
# so that we don't need to modify the function's real signature for
360361
# any parametrization. Modifying the signature is very very tricky
361362
# and likely to confuse pytest.
362-
extension, = func.parameters
363+
baseline_images, extension = func.parameters
363364

364365
assert len(plt.get_fignums()) == len(baseline_images), (
365366
"Test generated {} images but there are {} baseline images"
@@ -373,7 +374,7 @@ def wrapper(*args, **kwargs):
373374
return decorator
374375

375376

376-
def image_comparison(baseline_images=None, extensions=None, tol=0,
377+
def image_comparison(baseline_images, extensions=None, tol=0,
377378
freetype_version=None, remove_text=False,
378379
savefig_kwarg=None,
379380
# Default of mpl_test_settings fixture and cleanup too.
@@ -385,10 +386,14 @@ def image_comparison(baseline_images=None, extensions=None, tol=0,
385386
386387
Arguments
387388
---------
388-
baseline_images : list
389+
baseline_images : list or None
389390
A list of strings specifying the names of the images generated by
390391
calls to :meth:`matplotlib.figure.savefig`.
391392
393+
If *None*, the test function must use the ``baseline_images`` fixture,
394+
either as a parameter or with pytest.mark.usefixtures. This value is
395+
only allowed when using pytest.
396+
392397
extensions : [ None | list ]
393398
394399
If None, defaults to all supported extensions.
@@ -415,9 +420,6 @@ def image_comparison(baseline_images=None, extensions=None, tol=0,
415420
'_classic_test' style.
416421
417422
"""
418-
if baseline_images is None:
419-
raise ValueError('baseline_images must be specified')
420-
421423
if extensions is None:
422424
# default extensions to test
423425
extensions = ['png', 'pdf', 'svg']
@@ -432,6 +434,9 @@ def image_comparison(baseline_images=None, extensions=None, tol=0,
432434
freetype_version=freetype_version, remove_text=remove_text,
433435
savefig_kwargs=savefig_kwarg, style=style)
434436
else:
437+
if baseline_images is None:
438+
raise ValueError('baseline_images must be specified')
439+
435440
return ImageComparisonDecorator(
436441
baseline_images=baseline_images, extensions=extensions, tol=tol,
437442
freetype_version=freetype_version, remove_text=remove_text,

0 commit comments

Comments
 (0)