使用JUnit测试图形生成 [英] Testing graphics generation with JUnit
问题描述
我正在使用Java的Graphics2D生成图形的图形表示.我还使用ImageIO编写PNG文件. (ImageIO.write(image, "png", out);
)
I'm using Java's Graphics2D to generate a graphical representation of a graph. I'm also using ImageIO to write a PNG file. (ImageIO.write(image, "png", out);
)
我想知道如何编写JUnit测试来测试生成的图形是否符合预期.我可以预生成PNG文件,但是如果另一台计算机上的字体有点不同怎么办?
I'm wondering how should I write JUnit tests to test whether the generated graphics is what is expected. I could pre-generate the PNG files but what if the font is a bit different on a different machine?
推荐答案
您可以尝试测试输出的特定已知功能,例如:
You could try testing for specific, known features of the output e.g.:
- (100,100)处有一个白色像素吗?
- 边界全黑了吗?
- 图片是否为预期尺寸?
和/或您可以为一些聚合属性"编写测试,以使结果模糊不清:
And/or you could write tests for some "aggregate properties" that allow for some fuzziness in the results:
- 图像是否至少有90%与参考图像匹配? (以允许使用不同的字体,消除锯齿的差异等)
- 图像中最普通的颜色等于背景颜色吗?
这篇关于使用JUnit测试图形生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!