forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 4
/
visual_tests.py
92 lines (87 loc) · 3.38 KB
/
visual_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
#
# This builds a html page of all images from the image comparison tests
# and opens that page in the browser.
#
# $ python visual_tests.py
#
import os
import time
import six
from collections import defaultdict
def run():
# Build a website for visual comparison
image_dir = "result_images"
# build the website
_html = ""
_html += """<html><head><style media="screen" type="text/css">
img{
width:100%;
max-width:800px;
}
</style>
</head><body>\n"""
_subdirs = [name for name in os.listdir(image_dir) if os.path.isdir(os.path.join(image_dir, name))]
# loop over all pictures
_row = '<tr><td>{0} {1}</td><td>{2}</td><td><a href="{3}"><img src="{3}"></a></td><td>{4}</td>\n'
_failed = ""
_failed += "<h2>Only Failed</h2>"
_failed += "<table>\n<thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>\n"
_has_failure = False
_body = ""
for subdir in _subdirs:
if subdir == "test_compare_images":
# these are the image which test the image comparison functions...
continue
pictures = defaultdict(dict)
for file in os.listdir(os.path.join(image_dir, subdir)):
if os.path.isdir(os.path.join(image_dir, subdir, file)):
continue
fn, fext = os.path.splitext(file)
if fext != ".png":
continue
# Always use / for URLs.
if "-failed-diff" in fn:
pictures[fn[:-12]]["f"] = "/".join((subdir, file))
elif "-expected" in fn:
pictures[fn[:-9]]["e"] = "/".join((subdir, file))
else:
pictures[fn]["c"] = "/".join((subdir, file))
_body += "<h2>{0}</h2>".format(subdir)
_body += "<table>\n<thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>\n"
for name, test in six.iteritems(pictures):
if test.get("f", None):
# a real failure in the image generation, resulting in different images
_has_failure = True
s = "(failed)"
failed = '<a href="{0}">diff</a>'.format(test.get("f", ""))
current = '<a href="{0}"><img src="{0}"></a>'.format(test.get("c", ""))
_failed += _row.format(name, "", current, test.get("e", ""), failed)
elif test.get("c", None) is None:
# A failure in the test, resulting in no current image
_has_failure = True
s = "(failed)"
failed = '--'
current = '(Failure in test, no image produced)'
_failed += _row.format(name, "", current, test.get("e", ""), failed)
else:
s = "(passed)"
failed = '--'
current = '<a href="{0}"><img src="{0}"></a>'.format(test.get("c", ""))
_body += _row.format(name, "", current, test.get("e", ""), failed)
_body += "</table>\n"
_failed += "</table>\n"
if _has_failure:
_html += _failed
_html += _body
_html += "\n</body></html>"
index = os.path.join(image_dir, "index.html")
with open(index, "w") as f:
f.write(_html)
try:
import webbrowser
webbrowser.open(index)
except:
print("Open {0} in a browser for a visual comparison.".format(str(index)))
if __name__ == '__main__':
run()