|
2 | 2 | KnownFailureDidNotFailTest, ImageComparisonFailure |
3 | 3 | import os, sys |
4 | 4 | import nose |
| 5 | +import matplotlib |
5 | 6 | import matplotlib.tests |
6 | 7 | from matplotlib.testing.compare import compare_images |
7 | 8 |
|
@@ -48,42 +49,54 @@ def image_comparison(baseline_images=None): |
48 | 49 | raise ValueError('baseline_images must be specified') |
49 | 50 | def compare_images_decorator(func): |
50 | 51 | def decorated_compare_images(*args,**kwargs): |
51 | | - result = func(*args,**kwargs) |
52 | | - extension = '.png' # TODO: test more backends |
53 | | - for fname in baseline_images: |
54 | | - # FIXME: place "actual", or current images, images in |
55 | | - # a more reasonable location than the current |
56 | | - # directory. Also, perhaps put them in sub-directory |
57 | | - # according to the name of the test module like the |
58 | | - # baseline images. |
59 | | - actual = fname + extension |
60 | 52 |
|
61 | | - # compute filename for baseline image |
62 | | - module_name = func.__module__ |
63 | | - if module_name=='__main__': |
64 | | - # FIXME: this won't work for nested packages in matplotlib.tests |
65 | | - import warnings |
66 | | - warnings.warn('test module run as script. guessing baseline image locations') |
67 | | - script_name = sys.argv[0] |
68 | | - basedir = os.path.abspath(os.path.dirname(script_name)) |
69 | | - subdir = os.path.splitext(os.path.split(script_name)[1])[0] |
70 | | - else: |
71 | | - mods = module_name.split('.') |
72 | | - assert mods.pop(0)=='matplotlib' |
73 | | - assert mods.pop(0)=='tests' |
74 | | - subdir = os.path.join(*mods) |
75 | | - basedir = os.path.dirname(matplotlib.tests.__file__) |
76 | | - baseline_dir = os.path.join(basedir,'baseline_images',subdir) |
77 | | - expected = os.path.join(baseline_dir,fname) + extension |
| 53 | + # compute baseline image directory |
| 54 | + module_name = func.__module__ |
| 55 | + if module_name=='__main__': |
| 56 | + # FIXME: this won't work for nested packages in matplotlib.tests |
| 57 | + import warnings |
| 58 | + warnings.warn('test module run as script. guessing baseline image locations') |
| 59 | + script_name = sys.argv[0] |
| 60 | + basedir = os.path.abspath(os.path.dirname(script_name)) |
| 61 | + subdir = os.path.splitext(os.path.split(script_name)[1])[0] |
| 62 | + else: |
| 63 | + mods = module_name.split('.') |
| 64 | + assert mods.pop(0)=='matplotlib' |
| 65 | + assert mods.pop(0)=='tests' |
| 66 | + subdir = os.path.join(*mods) |
| 67 | + basedir = os.path.dirname(matplotlib.tests.__file__) |
| 68 | + baseline_dir = os.path.join(basedir,'baseline_images',subdir) |
| 69 | + result_dir = os.path.join(basedir,'current_images',subdir) |
| 70 | + if not os.path.exists(result_dir): |
| 71 | + try: |
| 72 | + # make the current_images directory first |
| 73 | + os.mkdir(os.path.join(basedir,'current_images')) |
| 74 | + except OSError: |
| 75 | + pass # probably exists already |
| 76 | + os.mkdir(result_dir) |
78 | 77 |
|
79 | | - # compare the images |
80 | | - tol=1e-3 # default tolerance |
81 | | - err = compare_images( expected, actual, tol, |
82 | | - in_decorator=True ) |
83 | | - if err: |
84 | | - raise ImageComparisonFailure( |
85 | | - 'images not close: %(actual)s vs. %(expected)s ' |
86 | | - '(RMS %(rms).3f)'%err) |
87 | | - return result |
| 78 | + for extension in ['png', 'pdf']: |
| 79 | + # set the default format of savefig |
| 80 | + matplotlib.rc('savefig', extension=extension) |
| 81 | + # change to the result directory for the duration of the test |
| 82 | + old_dir = os.getcwd() |
| 83 | + os.chdir(result_dir) |
| 84 | + try: |
| 85 | + last_result = func(*args,**kwargs) # actually call the test function |
| 86 | + finally: |
| 87 | + os.chdir(old_dir) |
| 88 | + for fname in baseline_images: |
| 89 | + actual = os.path.join(result_dir, fname) + '.' + extension |
| 90 | + expected = os.path.join(baseline_dir,fname) + '.' + extension |
| 91 | + |
| 92 | + # compare the images |
| 93 | + tol=1e-3 # default tolerance |
| 94 | + err = compare_images( expected, actual, tol, |
| 95 | + in_decorator=True ) |
| 96 | + if err: |
| 97 | + raise ImageComparisonFailure( |
| 98 | + 'images not close: %(actual)s vs. %(expected)s ' |
| 99 | + '(RMS %(rms).3f)'%err) |
| 100 | + return last_result |
88 | 101 | return nose.tools.make_decorator(func)(decorated_compare_images) |
89 | 102 | return compare_images_decorator |
0 commit comments