Revision: 29873 http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=29873 Author: leifandersen Date: 2010-07-02 18:50:58 +0200 (Fri, 02 Jul 2010)
Log Message: ----------- Reogranized the code to make it much more moduler. Modified Paths: -------------- branches/soc-2010-leifandersen/tests/render/run.py Modified: branches/soc-2010-leifandersen/tests/render/run.py =================================================================== --- branches/soc-2010-leifandersen/tests/render/run.py 2010-07-02 13:47:23 UTC (rev 29872) +++ branches/soc-2010-leifandersen/tests/render/run.py 2010-07-02 16:50:58 UTC (rev 29873) @@ -11,136 +11,118 @@ BLENDER_BIN = "blender" -def render(filename): - command = BLENDER_BIN + " -b " + os.path.join(sys.path[0], filename) + " -o " + os.path.join(sys.path[0], "output", (string.replace(filename, '.blend', '') + "_####")) + " -F PNG -x 1 -f 1" - os.system(command) - return os.path.join(sys.path[0], "output", string.replace(filename, '.blend', '_0001.png')) +class RenderTestCase: + '''A Render Test Case. Steps must be run in order: + 1. Constructer + 2. Render Image (if it doesn't already exist) + 3. Setup Tests + 4. Save Diff''' + name = None + passed = None + good_path = None + blend_path = None + test_path = None + diff_path = None + bad_pixels = None + pixel_count = None + good_image = None + test_image = None + diff_image = None -class TestImage(unittest.TestCase): - - im1 = None - im2 = None - diff = None + def __init__(self, blend_path, good_path, test_path): + self.blend_path = blend_path + self.good_path = good_path + self.name = os.path.split(blend_path)[1] + self.good_image = Image.open(good_path) + self.test_path = test_path - def setUp(self): - image_path1 = os.path.join(sys.path[0], 'render', string.replace(filename,'.blend', '.png')) - image_path2 = os.path.join(sys.path[0], 'output', string.replace(filename, '.blend', '_0001.png')) - self.im1 = Image.open(image_path1) - self.im2 = Image.open(image_path2) - self.diff = ImageChops.difference(self.im1, self.im2) - self.diff.save(os.path.join(sys.path[0], 'output', string.replace(filename, '.blend', '_diff.png')) , "PNG") + def render_image(self): + command = BLENDER_BIN + " -b " + self.blend_path + " -o " + self.test_path + "_#### -F PNG -x 1 -f 1" + os.system(command) + self.test_path += '_0001.png' + self.test_image = Image.open(self.test_path) - def test_diff(self): - global bad_pixels - global pixel_count - bad_pixels = 0 - pixel_count = len(self.diff.histogram()) - for num in self.diff.histogram(): + def setup_test(self): + self.diff_image = ImageChops.difference(self.good_image, self.test_image) + self.bad_pixels = 0 + self.pixel_count = len(self.diff_image.histogram()) + for num in self.diff_image.histogram(): if num > 300: - bad_pixels+=1 - if bad_pixels > pixel_count/10: - self.fail("Images are too different: " + str(bad_pixels)) + self.bad_pixels+=1 + if self.bad_pixels < self.pixel_count/10: + self.passed = True + else: + self.passed = False + return self.passed + def save_diff(self, output_path): + if self.diff_image != None: + self.diff_path = output_path + self.diff_image.save(output_path) -# This class is only run use when being called from by the operator in blender. -class TestImageOperator(unittest.TestCase): - def setUp(self): - pass +def get_cases(folder): + '''Returns all of the blend files in the folder''' + cases = [] + for filename in os.listdir(sys.path[0]): + if filename.find('.blend') != -1: + blend_path = os.path.join(sys.path[0],filename) + good_path = os.path.join(sys.path[0], 'render', string.replace(filename, '.blend', '.png')) + test_path = os.path.join(sys.path[0], 'output', string.replace(filename, '.blend', '')) + test_case = RenderTestCase(blend_path, good_path, test_path) + cases.append(test_case) + return cases - def test_image(self): - if len(sys.argv) < 3: - self.fail("Please don't use this script") - image_path1 = sys.argv[2] - image_path2 = sys.argv[3] - im1 = Image.open(image_path1) - im2 = Image.open(image_path2) - im1_red = im1_green = im1_blue = im2_red = im2_green = im2_blue = 0 +def console_output(cases): + failed = [] + for test_case in cases: + if test_case.passed == False: + failed.append(test_case) - self.assertEquals(im1.size, im2.size) - for i in range(0, im1.size[0]): - for j in range(0, im1.size[1]): - im1_red+=im1.getpixel((i,j))[0] - im1_green+=im1.getpixel((i,j))[1] - im1_blue+=im1.getpixel((i,j))[2] - im2_red+=im2.getpixel((i,j))[0] - im2_green+=im2.getpixel((i,j))[1] - im2_blue+=im2.getpixel((i,j))[2] + print "The following tesets failed:" + for test_case in failed: + print test_case.name - im_red = abs(im1_red-im2_red) - im_green = abs(im1_green-im2_green) - im_blue = abs(im1_blue-im2_blue) +def generate_output(images): + file = open(os.path.join(sys.path[0],"index.html"), "w") + file.write('<html><head><title>Render Test Output</title></head>\n') + file.write('<body>\n') + file.write('<div class="noprint" id="navcontainer"><a href="http://www.blender.org/"><img width="140" height="50" title="blender.org home page" alt="blender.org home page" src="http://www.blender.org/fileadmin/site/_gfx/nav-home.png"></a><a href="http://www.blender.org/features-gallery/"><img width="140" height="50" title="Features & Gallery" alt="Features & Gallery" src="http://www.blender.org/fileadmin/site/_gfx/nav-features.png"></a><a href="http://www.blender.org/download/get-blender/"><img width="140" height="50" title="Download" alt="Download" src="http://www.blender.org/fileadmin/site/_gfx/nav-download.png"></a><a href="http://www.blender.org/education-help/"><img width="140" height="50" title="Tutorials & Help" alt="Tutorials & Help" src="http://www.blender.org/fileadmin/site/_gfx/nav-help.png"></a><a href="http://www.blender.org/community/user-community/"><img width="140" height="50" title="Community" alt="Community" src="http://www.blender.org/f ileadmin/site/_gfx/nav-community.png"></a><a href="http://www.blender.org/development/"><img width="140" height="50" title="Development" alt="Development" src="http://www.blender.org/fileadmin/site/_gfx/nav-development-on.png"></a><a href="http://www.blender.org/e-shop/"><img width="140" height="50" title="e-Shop" alt="e-Shop" src="http://www.blender.org/fileadmin/site/_gfx/nav-eshop.png"></a></div> ') + file.write('<table border="1">\n') + file.write('<tr>\n') + file.write('<th>Filename</th>\n') + file.write('<th>Good Image</th>\n') + file.write('<th>Latest Render</th>\n') + file.write('<th>Diff</th>\n') + file.write('<th>Match?</th>\n') + file.write('</tr>\n') - if im_red > 100 or im_green > 100 or im_blue > 100: - self.fail("Images are too different") + for image in images: + file.write('<tr>\n') + file.write('<td>' + image.name + '</td>\n') + file.write('<td><img src="' + os.path.join('render', string.replace(image.name, '.blend', '.png')) + '" alt="Good Image" width="200" height="150"></td>\n') + file.write('<td><img src="' + os.path.join('output', string.replace(image.name, '.blend', '_0001.png')) + '" alt="Latest Render" width="200" height="150"></td>\n') + file.write('<td><img src="' + os.path.join('output', string.replace(image.name, '.blend', '_diff.png')) + '" alt="Image Diff" width="200" height="150"></td>\n') + if image.passed: + file.write('<td><font color="ooffoo">OK</font>: ' + str(image.bad_pixels) + ' of ' + str(image.pixel_count) + ' pixels different</td>\n') + else: + file.write('<td><font color="ff0000">FAIL</font>: ' + str(image.bad_pixels) + ' of ' + str(image.pixel_count) + ' pixels different</td>\n') + file.write('</tr>\n') + file.write('</table>\n</body>\n</html>') + file.close() -def suite(): - return unittest.TestSuite([ -unittest.TestLoader().loadTestsFromTestCase(TestImage), -]) - -def operator_suite(): - return unittest.TestSuite([ -unittest.TestLoader().loadTestsFromTestCase(TestImageOperator), -]) - if __name__ == "__main__": - passed = [] - failed = [] - if len(sys.argv) > 1 and sys.argv[1] == '--blender-intern': - # If this is run in blender, than the images already exist - # and only comparisons need to take place. - unittest.TextTestRunner(verbosity=2).run(operator_suite()) - else: - # Otherwise render the tests, and generate HTML output + cases = get_cases(sys.path[0]) + cases.sort(key=lambda case: case.name) + for test_case in cases: + print test_case.name + test_case.render_image() + test_case.setup_test() + test_case.save_diff(os.path.join(sys.path[0], 'output', string.replace(test_case.name, '.blend', '_diff.png'))) + print test_case.passed - #Setting up the HTML File - file = open(os.path.join(sys.path[0],"index.html"), "w") - file.write('<html><head><title>Render Test Output</title></head>\n') - file.write('<body>\n') @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org http://lists.blender.org/mailman/listinfo/bf-blender-cvs