views:

38

answers:

2

What is the best way to unit test code that generates images? Say, for example, a class that generates a plot or a chart?

+2  A: 

A method that I've used is to generate a "known-good" file, store it in your source tree, and then do a binary compare against it as part of the test. If the file contents match the output hasn't changed.

This doesn't allow you to test all possible combinations of input that would generate the image, but is useful for basic regression tests.

devstuff
+2  A: 

If this class uses a third party library to generate plots/charts (say matplotlib) then you can write tests for the methods that generate input for the the library. This will be fairly easy.

If the output is an image and you are interested in verifying its properties then you will have to dig deeper. External image attributes (size, height, format etc.) can be easily verified but others such as the actual contents of the image would be quite hard. IMHO that wouldn't be worth the trouble.

If the output is non-binary (say SVG) then you can easily write tests to ensure that the output XML contains what you are looking for.

Manoj Govindan
Indeed, and matplotlib gives a good image comparison decorator for this.
epoch