def _mark_xfail_if_format_is_uncomparable(extension): if isinstance(extension, str): will_fail = extension not in comparable_formats() else: # Extension might be a pytest marker instead of a plain string. will_fail = extension.args[0] not in comparable_formats() if will_fail: fail_msg = 'Cannot compare %s files on this system' % extension import pytest return pytest.mark.xfail(extension, reason=fail_msg, strict=False, raises=ImageComparisonFailure) else: return extension
def mark_extension(extension): ''' Mark whether extension is supported. ''' __tracebackhide__ = True # pylint: disable=unused-variable if extension not in mplcmp.comparable_formats(): raise unittest.SkipTest('Cannot compare {} files in this ' 'system'.format(extension))
def compare_images_generator(): for extension in extensions: @knownfailureif( extension not in comparable_formats(), "Cannot compare %s files on this system" % extension ) def decorated_compare_images(): matplotlib.rc("savefig", extension=extension) old_dir = os.getcwd() os.chdir(result_dir) try: result = func() # actually call the test function finally: os.chdir(old_dir) for fname in baseline_images: actual = os.path.join(result_dir, fname) + "." + extension expected = os.path.join(baseline_dir, fname) + "." + extension tol = 1e-3 # default tolerance err = compare_images(expected, actual, tol, in_decorator=True) if err: raise ImageComparisonFailure( "images not close: %(actual)s vs. %(expected)s " "(RMS %(rms).3f)" % err ) return result yield (decorated_compare_images,)
def _xfail_if_format_is_uncomparable(extension): import pytest return pytest.mark.xfail( extension not in comparable_formats(), reason='Cannot compare {} files on this system'.format(extension), raises=ImageComparisonFailure, strict=True)
def test(self): baseline_dir, result_dir = _image_directories(self._func) for fignum, baseline in zip(plt.get_fignums(), self._baseline_images): for extension in self._extensions: will_fail = not extension in comparable_formats() if will_fail: fail_msg = 'Cannot compare %s files on this system' % extension else: fail_msg = 'No failure expected' orig_expected_fname = os.path.join(baseline_dir, baseline) + '.' + extension if extension == 'eps' and not os.path.exists(orig_expected_fname): orig_expected_fname = os.path.join(baseline_dir, baseline) + '.pdf' expected_fname = make_test_filename(os.path.join( result_dir, os.path.basename(orig_expected_fname)), 'expected') actual_fname = os.path.join(result_dir, baseline) + '.' + extension if os.path.exists(orig_expected_fname): shutil.copyfile(orig_expected_fname, expected_fname) else: will_fail = True fail_msg = ( "Do not have baseline image {0} because this " "file does not exist: {1}".format( expected_fname, orig_expected_fname ) ) @knownfailureif( will_fail, fail_msg, known_exception_class=ImageComparisonFailure) def do_test(): figure = plt.figure(fignum) if self._remove_text: self.remove_text(figure) figure.savefig(actual_fname, **self._savefig_kwarg) err = compare_images(expected_fname, actual_fname, self._tol, in_decorator=True) try: if not os.path.exists(expected_fname): raise ImageComparisonFailure( 'image does not exist: %s' % expected_fname) if err: raise ImageComparisonFailure( 'images not close: %(actual)s vs. %(expected)s ' '(RMS %(rms).3f)'%err) except ImageComparisonFailure: if not check_freetype_version(self._freetype_version): raise KnownFailureTest( "Mismatched version of freetype. Test requires '%s', you have '%s'" % (self._freetype_version, ft2font.__freetype_version__)) raise yield (do_test,)
def _mark_xfail_if_format_is_uncomparable(extension): if isinstance(extension, six.string_types): name = extension marks = [] elif isinstance(extension, tuple): # Extension might be a pytest ParameterSet instead of a plain string. # Unfortunately, this type is not exposed, so since it's a namedtuple, # check for a tuple instead. name = extension.values[0] marks = list(extension.marks) else: # Extension might be a pytest marker instead of a plain string. name = extension.args[0] marks = [extension.mark] if name not in comparable_formats(): fail_msg = 'Cannot compare %s files on this system' % (name, ) import pytest marks += [ pytest.mark.xfail(reason=fail_msg, strict=False, raises=ImageComparisonFailure) ] return pytest.param(name, marks=marks) else: return extension
def compare_images_generator(): for extension in extensions: orig_expected_fnames = [ os.path.join(baseline_dir, fname) + '.' + extension for fname in baseline_images ] expected_fnames = [ os.path.join(result_dir, 'expected-' + fname) + '.' + extension for fname in baseline_images ] for src, dst in zip(orig_expected_fnames, expected_fnames): shutil.copyfile(src, dst) actual_fnames = [ os.path.join(result_dir, fname) + '.' + extension for fname in baseline_images ] have_baseline_images = [ os.path.exists(expected) for expected in expected_fnames ] have_baseline_image = np.all(have_baseline_images) is_comparable = extension in comparable_formats() if not is_comparable: fail_msg = 'Cannot compare %s files on this system' % extension elif not have_baseline_image: fail_msg = 'Do not have baseline images %s' % expected_fnames else: fail_msg = 'No failure expected' will_fail = not (is_comparable and have_baseline_image) @knownfailureif(will_fail, fail_msg, known_exception_class=ImageComparisonFailure) def decorated_compare_images(): # set the default format of savefig matplotlib.rc('savefig', extension=extension) # change to the result directory for the duration of the test old_dir = os.getcwd() os.chdir(result_dir) try: result = func() # actually call the test function finally: os.chdir(old_dir) for actual, expected in zip(actual_fnames, expected_fnames): if not os.path.exists(expected): raise ImageComparisonFailure( 'image does not exist: %s' % expected) # compare the images err = compare_images(expected, actual, tol, in_decorator=True) if err: raise ImageComparisonFailure( 'images not close: %(actual)s vs. %(expected)s ' '(RMS %(rms).3f)' % err) return result yield (decorated_compare_images, )
def test(self): baseline_dir, result_dir = _image_directories(self._func) for fignum, baseline in zip(plt.get_fignums(), self._baseline_images): figure = plt.figure(fignum) for extension in self._extensions: will_fail = not extension in comparable_formats() if will_fail: fail_msg = 'Cannot compare %s files on this system' % extension else: fail_msg = 'No failure expected' orig_expected_fname = os.path.join(baseline_dir, baseline) + '.' + extension if extension == 'eps' and not os.path.exists( orig_expected_fname): orig_expected_fname = os.path.join(baseline_dir, baseline) + '.pdf' expected_fname = os.path.join( result_dir, 'expected-' + os.path.basename(orig_expected_fname)) actual_fname = os.path.join(result_dir, baseline) + '.' + extension if os.path.exists(orig_expected_fname): shutil.copyfile(orig_expected_fname, expected_fname) else: will_fail = True fail_msg = 'Do not have baseline image %s' % expected_fname @knownfailureif(will_fail, fail_msg, known_exception_class=ImageComparisonFailure) def do_test(): figure.savefig(actual_fname) err = compare_images(expected_fname, actual_fname, self._tol, in_decorator=True) try: if not os.path.exists(expected_fname): raise ImageComparisonFailure( 'image does not exist: %s' % expected_fname) if err: raise ImageComparisonFailure( 'images not close: %(actual)s vs. %(expected)s ' '(RMS %(rms).3f)' % err) except ImageComparisonFailure: if not check_freetype_version(self._freetype_version): raise KnownFailureTest( "Mismatched version of freetype. Test requires '%s', you have '%s'" % (self._freetype_version, ft2font.__freetype_version__)) raise yield (do_test, )
def _mark_xfail_if_format_is_uncomparable(extension): will_fail = extension not in comparable_formats() if will_fail: fail_msg = 'Cannot compare %s files on this system' % extension import pytest return pytest.mark.xfail(extension, reason=fail_msg, strict=False, raises=ImageComparisonFailure) else: return extension
def mark_xfail_if_format_is_uncomparable(extension): will_fail = extension not in comparable_formats() if will_fail: fail_msg = 'Cannot compare %s files on this system' % extension import pytest return pytest.mark.xfail(extension, reason=fail_msg, strict=False, raises=ImageComparisonFailure) else: return extension
def _xfail_if_format_is_uncomparable(extension): will_fail = extension not in comparable_formats() if will_fail: fail_msg = 'Cannot compare %s files on this system' % extension else: fail_msg = 'No failure expected' return _knownfailureif(will_fail, fail_msg, known_exception_class=ImageComparisonFailure)
def assert_fig(expected, extensions=['png']): j = os.path.join for extension in extensions: if not extension in comparable_formats(): raise ImageComparisonFailure('Cannot compare %s files ' 'on this system' % extension) for fignum in plt.get_fignums(): figure = plt.figure(fignum) path = os.path.dirname(__file__) actual_fname = j(path, 'plots', expected) + '_actual_.' + extension expected_fname = j(path, 'plots', expected) + '.' + extension figure.savefig(actual_fname) err = compare_images(expected_fname, actual_fname, 13) if err: raise ImageComparisonFailure( 'images not close: %(actual)s vs. %(expected)s ' '(RMS %(rms).3f)' % err)
def test(self): baseline_dir, result_dir = _image_directories(self._func) for fignum, baseline in zip(plt.get_fignums(), self._baseline_images): figure = plt.figure(fignum) for extension in self._extensions: will_fail = not extension in comparable_formats() if will_fail: fail_msg = 'Cannot compare %s files on this system' % extension else: fail_msg = 'No failure expected' orig_expected_fname = os.path.join(baseline_dir, baseline) + '.' + extension expected_fname = os.path.join( result_dir, 'expected-' + baseline) + '.' + extension actual_fname = os.path.join(result_dir, baseline) + '.' + extension if os.path.exists(orig_expected_fname): shutil.copyfile(orig_expected_fname, expected_fname) else: will_fail = True fail_msg = 'Do not have baseline image %s' % expected_fname @knownfailureif(will_fail, fail_msg, known_exception_class=ImageComparisonFailure) def do_test(): figure.savefig(actual_fname) if not os.path.exists(expected_fname): raise ImageComparisonFailure( 'image does not exist: %s' % expected_fname) err = compare_images(expected_fname, actual_fname, self._tol, in_decorator=True) if err: raise ImageComparisonFailure( 'images not close: %(actual)s vs. %(expected)s ' '(RMS %(rms).3f)' % err) yield (do_test, )
def compare_images_generator(): for extension in extensions: orig_expected_fnames = [os.path.join(baseline_dir,fname) + '.' + extension for fname in baseline_images] expected_fnames = [os.path.join(result_dir,'expected-'+fname) + '.' + extension for fname in baseline_images] for src,dst in zip( orig_expected_fnames, expected_fnames ): if os.path.exists(src) and not os.path.exists(dst): shutil.copyfile(src,dst) actual_fnames = [os.path.join(result_dir, fname) + '.' + extension for fname in baseline_images] have_baseline_images = [os.path.exists(expected) for expected in expected_fnames] have_baseline_image = np.all(have_baseline_images) is_comparable = extension in comparable_formats() if not is_comparable: fail_msg = 'Cannot compare %s files on this system' % extension elif not have_baseline_image: fail_msg = 'Do not have baseline images %s' % expected_fnames else: fail_msg = 'No failure expected' will_fail = not (is_comparable and have_baseline_image) @knownfailureif(will_fail, fail_msg, known_exception_class=ImageComparisonFailure ) def decorated_compare_images(): # set the default format of savefig matplotlib.rc('savefig', extension=extension) # change to the result directory for the duration of the test old_dir = os.getcwd() os.chdir(result_dir) try: result = func() # actually call the test function finally: os.chdir(old_dir) for actual,expected in zip(actual_fnames,expected_fnames): if not os.path.exists(expected): raise ImageComparisonFailure( 'image does not exist: %s'%expected) # compare the images tol=1e-3 # default tolerance err = compare_images( expected, actual, tol, in_decorator=True ) if err: raise ImageComparisonFailure( 'images not close: %(actual)s vs. %(expected)s ' '(RMS %(rms).3f)'%err) return result yield (decorated_compare_images,)
def test(self): baseline_dir, result_dir = _image_directories(self._func) for fignum, baseline in zip(plt.get_fignums(), self._baseline_images): figure = plt.figure(fignum) for extension in self._extensions: will_fail = not extension in comparable_formats() if will_fail: fail_msg = 'Cannot compare %s files on this system' % extension else: fail_msg = 'No failure expected' orig_expected_fname = os.path.join(baseline_dir, baseline) + '.' + extension if extension == 'eps' and not os.path.exists(orig_expected_fname): orig_expected_fname = os.path.join(baseline_dir, baseline) + '.pdf' expected_fname = os.path.join(result_dir, 'expected-' + os.path.basename(orig_expected_fname)) actual_fname = os.path.join(result_dir, baseline) + '.' + extension if os.path.exists(orig_expected_fname): shutil.copyfile(orig_expected_fname, expected_fname) else: will_fail = True fail_msg = 'Do not have baseline image %s' % expected_fname @knownfailureif( will_fail, fail_msg, known_exception_class=ImageComparisonFailure) def do_test(): figure.savefig(actual_fname) err = compare_images(expected_fname, actual_fname, self._tol, in_decorator=True) if not os.path.exists(expected_fname): raise ImageComparisonFailure( 'image does not exist: %s' % expected_fname) if err: raise ImageComparisonFailure( 'images not close: %(actual)s vs. %(expected)s ' '(RMS %(rms).3f)'%err) yield (do_test,)
def test(self): baseline_dir, result_dir = _image_directories(self._func) if self._style != 'classic': skip('temporarily disabled until 2.0 tag') for fignum, baseline in zip(plt.get_fignums(), self._baseline_images): for extension in self._extensions: will_fail = not extension in comparable_formats() if will_fail: fail_msg = 'Cannot compare %s files on this system' % extension else: fail_msg = 'No failure expected' orig_expected_fname = os.path.join(baseline_dir, baseline) + '.' + extension if extension == 'eps' and not os.path.exists(orig_expected_fname): orig_expected_fname = os.path.join(baseline_dir, baseline) + '.pdf' expected_fname = make_test_filename(os.path.join( result_dir, os.path.basename(orig_expected_fname)), 'expected') actual_fname = os.path.join(result_dir, baseline) + '.' + extension if os.path.exists(orig_expected_fname): shutil.copyfile(orig_expected_fname, expected_fname) else: will_fail = True fail_msg = ( "Do not have baseline image {0} because this " "file does not exist: {1}".format( expected_fname, orig_expected_fname ) ) @knownfailureif( will_fail, fail_msg, known_exception_class=ImageComparisonFailure) def do_test(fignum, actual_fname, expected_fname): figure = plt.figure(fignum) if self._remove_text: self.remove_text(figure) figure.savefig(actual_fname, **self._savefig_kwarg) err = compare_images(expected_fname, actual_fname, self._tol, in_decorator=True) try: if not os.path.exists(expected_fname): raise ImageComparisonFailure( 'image does not exist: %s' % expected_fname) if err: raise ImageComparisonFailure( 'images not close: %(actual)s vs. %(expected)s ' '(RMS %(rms).3f)'%err) except ImageComparisonFailure: if not check_freetype_version(self._freetype_version): xfail( "Mismatched version of freetype. Test requires '%s', you have '%s'" % (self._freetype_version, ft2font.__freetype_version__)) raise yield do_test, fignum, actual_fname, expected_fname