Skip to content

Commit

Permalink
Added options to set default tolerance and style
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed Sep 8, 2022
1 parent c10bd98 commit b83ea27
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions pytest_mpl/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ def pytest_addoption(parser):
parser.addini('mpl-use-full-test-name', help="use fully qualified test name as the filename.",
type='bool')

style_help = "default style to use for tests, unless specified in the mpl_image_compare decorator"
group.addoption('--mpl-default-style', help=style_help, action='store')
parser.addini('mpl-default-style', help=style_help)

tolerance_help = "default tolerance to use for tests, unless specified in the mpl_image_compare decorator"
group.addoption('--mpl-default-tolerance', help=tolerance_help, action='store')
parser.addini('mpl-default-tolerance', help=tolerance_help)


def pytest_configure(config):

Expand Down Expand Up @@ -196,6 +204,12 @@ def pytest_configure(config):
if results_dir is not None:
results_dir = os.path.abspath(results_dir)

default_style = (config.getoption("--mpl-default-style") or
config.getini("mpl-default-style"))

default_tolerance = (config.getoption("--mpl-default-tolerance") or
config.getini("mpl-default-tolerance"))

config.pluginmanager.register(ImageComparison(config,
baseline_dir=baseline_dir,
baseline_relative_dir=baseline_relative_dir,
Expand All @@ -204,7 +218,9 @@ def pytest_configure(config):
hash_library=hash_library,
generate_hash_library=generate_hash_lib,
generate_summary=generate_summary,
results_always=results_always))
results_always=results_always,
default_style=default_style,
default_tolerance=default_tolerance))

else:

Expand Down Expand Up @@ -261,6 +277,8 @@ def __init__(self,
generate_hash_library=None,
generate_summary=None,
results_always=False
default_style='classic',
default_tolerance=2,
):
self.config = config
self.baseline_dir = baseline_dir
Expand All @@ -281,6 +299,9 @@ def __init__(self,
self.generate_summary = generate_summary
self.results_always = results_always

self.default_style = default_style
self.default_tolerance = default_tolerance

# Generate the containing dir for all test results
if not self.results_dir:
self.results_dir = Path(tempfile.mkdtemp(dir=self.results_dir))
Expand Down Expand Up @@ -451,7 +472,7 @@ def compare_image_to_baseline(self, item, fig, result_dir, summary=None):
summary = {}

compare = get_compare(item)
tolerance = compare.kwargs.get('tolerance', 2)
tolerance = compare.kwargs.get('tolerance', self.default_tolerance)
savefig_kwargs = compare.kwargs.get('savefig_kwargs', {})

baseline_image_ref = self.obtain_baseline_image(item, result_dir)
Expand Down Expand Up @@ -612,7 +633,7 @@ def pytest_runtest_call(self, item): # noqa
from matplotlib.testing.decorators import ImageComparisonTest as MplImageComparisonTest
remove_ticks_and_titles = MplImageComparisonTest.remove_text

style = compare.kwargs.get('style', 'classic')
style = compare.kwargs.get('style', self.default_style)
remove_text = compare.kwargs.get('remove_text', False)
backend = compare.kwargs.get('backend', 'agg')

Expand Down

0 comments on commit b83ea27

Please sign in to comment.