Testing Images

TLDR; I use matplotlib.testing.compare.compare_images instead of matplotlib.testing.decorators.image_comparison

Searching for image testing led to serveral pages using the “image_comparison” decorator in matplotlib.testing.decorators.

Tried to use it, but since a decorator hides the name of the decorated function, nose didn’t see that the name began with “test_” and should be part of the test suite.

Tried to create a decorator that would use “image_comparison” in the background, but wrap the “test_*”” function to keep it’s name correctly visible to nose. I couldn’t figure it out.

After looking at the “image_comparison” decorator code found that the heavy lifting is done by a “compare_images” function. Was able to use that directly with a much easier to understand test.:

from matplotlib.testing.compare import compare_images
from pandas.util.testing import TestCase

from tstoolbox import tstoolbox

class TestPlot(TestCase):
    def setUp(self):
        # Data to plot and temporary filename for plot image
        self.df = tstoolbox.read('tests/data_sine.csv')
        fp, self.fname = tempfile.mkstemp(suffix='.png')

    def test_sine(self):
        # Plot using function to test
        plt = tstoolbox.plot(input_ts=self.df, ofilename=None)

        # Compare against base version...
        # different versions of matplotlib have slightly different fonts
        # set the tolerance pretty high to account for this problem.
        results = compare_images('tests/baseline_images/test_plot/sine.png',

        if results is None:  # the images compare favorably
            return True

        base, ext = os.path.splitext(self.fname)
        os.remove('%s-%s%s' % (base, 'failed-diff', ext))
        assert False


comments powered by Disqus