From 75308d674d0e5bc79709a17ebe19b66a62b794cc Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Tue, 2 Feb 2016 12:50:19 +0000 Subject: [PATCH 1/2] Emit warning instead of exception if both the generate and baseline path are provided. --- pytest_mpl/plugin.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pytest_mpl/plugin.py b/pytest_mpl/plugin.py index ffe61cd9..0f6f5120 100644 --- a/pytest_mpl/plugin.py +++ b/pytest_mpl/plugin.py @@ -33,6 +33,7 @@ import os import shutil import tempfile +import warnings import pytest @@ -51,15 +52,15 @@ def pytest_addoption(parser): def pytest_configure(config): - if config.getoption("--mpl-generate-path") is not None: - if config.getoption("--mpl-baseline-path") is not None: - raise ValueError("Can't set --mpl-baseline-path when generating reference images with --mpl-generate-path") - if config.getoption("--mpl") or config.getoption("--mpl-generate-path") is not None: baseline_dir = config.getoption("--mpl-baseline-path") generate_dir = config.getoption("--mpl-generate-path") + if baseline_dir is not None and generate_dir is not None: + warnings.warn("Ignoring --mpl-baseline-path since --mpl-generate-path is set") + baseline_dir = None + if baseline_dir is not None: baseline_dir = os.path.abspath(baseline_dir) if generate_dir is not None: From 9ed529841445a257bdc9f0ab5626c6c9c03a4c4b Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Tue, 2 Feb 2016 12:52:47 +0000 Subject: [PATCH 2/2] The baseline_dir option can now take a list of baseline directories, and pytest-mpl will search these in order for baseline images. --- pytest_mpl/plugin.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/pytest_mpl/plugin.py b/pytest_mpl/plugin.py index 0f6f5120..e292697c 100644 --- a/pytest_mpl/plugin.py +++ b/pytest_mpl/plugin.py @@ -39,6 +39,12 @@ from matplotlib.testing.compare import compare_images +import sys +if sys.version_info[0] == 2: + string_types = basestring, +else: + string_types = str, + def pytest_addoption(parser): group = parser.getgroup("general") @@ -93,14 +99,21 @@ def pytest_runtest_setup(self, item): @wraps(item.function) def item_function_wrapper(*args, **kwargs): + # Note that below we make baseline_dir into a list (if it isn't + # already one) and we'll then search for a baseline image starting + # from the first directory in baseline_dir and stopping once we find + # one. This then allows support for having different reference + # images for different versions of Matplotlib for example. baseline_dir = compare.kwargs.get('baseline_dir', None) if baseline_dir is None: if self.baseline_dir is None: - baseline_dir = os.path.join(os.path.dirname(item.fspath.strpath), 'baseline') + baseline_dir = [os.path.join(os.path.dirname(item.fspath.strpath), 'baseline')] else: - baseline_dir = self.baseline_dir + baseline_dir = [self.baseline_dir] else: - baseline_dir = os.path.join(os.path.dirname(item.fspath.strpath), baseline_dir) + if isinstance(baseline_dir, string_types): + baseline_dir = [baseline_dir] + baseline_dir = [os.path.join(os.path.dirname(item.fspath.strpath), x) for x in baseline_dir] # Run test and get figure object import inspect @@ -125,14 +138,15 @@ def item_function_wrapper(*args, **kwargs): fig.savefig(test_image, **savefig_kwargs) # Find path to baseline image - baseline_image_ref = os.path.abspath(os.path.join(os.path.dirname(item.fspath.strpath), baseline_dir, filename)) - - if not os.path.exists(baseline_image_ref): + for directory in baseline_dir: + baseline_image_ref = os.path.abspath(os.path.join(os.path.dirname(item.fspath.strpath), directory, filename)) + if os.path.exists(baseline_image_ref): + break + else: raise Exception("""Image file not found for comparison test Generated Image: \t{test} - This is expected for new tests.""".format( - test=test_image)) + This is expected for new tests.""".format(test=test_image)) # distutils may put the baseline images in non-accessible places, # copy to our tmpdir to be sure to keep them in case of failure