8000 TST: Allow baseline_images to be a fixture. · matplotlib/matplotlib@36a1617 · GitHub
[go: up one dir, main page]

Skip to content

Commit 36a1617

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 bdd947c commit 36a1617

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
@@ -343,6 +343,7 @@ def _pytest_image_comparison(baseline_images, extensions, tol,
343343
def decorator(func):
344344
@pytest.mark.usefixtures('mpl_image_comparison_parameters')
345345
@pytest.mark.parametrize('extension', extensions)
346+
@pytest.mark.baseline_images(baseline_images)
346347
@pytest.mark.style(style)
347348
@_checked_on_freetype_version(freetype_version)
348349
@functools.wraps(func)
@@ -358,7 +359,7 @@ def wrapper(*args, **kwargs):
358359
# so that we don't need to modify the function's real signature for
359360
# any parametrization. Modifying the signature is very very tricky
360361
# and likely to confuse pytest.
361-
extension, = func.parameters
362+
baseline_images, extension = func.parameters
362363

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

374375

375-
def image_comparison(baseline_images=None, extensions=None, tol=0,
376+
def image_comparison(baseline_images, extensions=None, tol=0,
376377
freetype_version=None, remove_text=False,
377378
savefig_kwarg=None,
378379
# Default of mpl_test_settings fixture and cleanup too.
@@ -384,10 +385,14 @@ def image_comparison(baseline_images=None, extensions=None, tol=0,
384385
385386
Arguments
386387
---------
387-
baseline_images : list
388+
baseline_images : list or None
388389
A list of strings specifying the names of the images generated by
389390
calls to :meth:`matplotlib.figure.savefig`.
390391
392+
If *None*, the test function must use the ``baseline_images`` fixture,
393+
either as a parameter or with pytest.mark.usefixtures. This value is
394+
only allowed when using pytest.
395+
391396
extensions : [ None | list ]
392397
393398
If None, defaults to all supported extensions.
@@ -414,9 +419,6 @@ def image_comparison(baseline_images=None, extensions=None, tol=0,
414419
'_classic_test' style.
415420
416421
"""
417-
if baseline_images is None:
418-
raise ValueError('baseline_images must be specified')
419-
420422
if extensions is None:
421423
# default extensions to test
422424
extensions = ['png', 'pdf', 'svg']
@@ -431,6 +433,9 @@ def image_comparison(baseline_images=None, extensions=None, tol=0,
431433
freetype_version=freetype_version, remove_text=remove_text,
432434
savefig_kwargs=savefig_kwarg, style=style)
433435
else:
436+
if baseline_images is None:
437+
raise ValueError('baseline_images must be specified')
438+
434439
return ImageComparisonDecorator(
435440
baseline_images=baseline_images, extensions=extensions, tol=tol,
436441
freetype_version=freetype_version, remove_text=remove_text,

0 commit comments

Comments
 (0)
0