使用 JUnit 测试图形生成
我正在使用 Java 的 Graphics2D 生成图形的图形表示。我还使用 ImageIO 来编写 PNG 文件。 (ImageIO.write(image, "png", out);
)
我想知道应该如何编写 JUnit 测试来测试生成的图形是否是预期的。我可以预先生成 PNG 文件,但如果不同机器上的字体有点不同怎么办?
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);
)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我来说,这个具体的实现似乎有效:
我使用 ImageIO 从我的 PNG 中检索 BufferedImage(作为 byte[]):
For me, this concrete implementation seems to work:
I retrieve the BufferedImage from my PNG (as byte[]) using ImageIO:
您可以尝试测试输出的特定已知特征,例如:
和/或您可以为某些“聚合属性”编写测试,以允许结果中存在一些模糊性:
You could try testing for specific, known features of the output e.g.:
And/or you could write tests for some "aggregate properties" that allow for some fuzziness in the results:
如果您确实对完整图像很具体,您可以将生成的图像的所有 RGB 值读入一个数组,并将该 2D 数组与表示预生成图像的数组进行比较。
如果您希望忽略字体,则可以根据生成图像的环境,对不包含任何可变数据的图像区域执行相同的操作。为单元测试构建校正和标准化例程将是浪费时间,除非应用程序预计会生成保证的高精度图像。
You could read all the RGB values of the generated images into an array and compare that 2D-array against one representing a pre-generated image, if you are really specific about the complete image.
If you wish to ignore the fonts, you could do the same for the same for regions of the image that do not contain any variable data depending on the environment where the images are generated. Building in correction and normalization routines for unit tests would be a waste of time, unless the application is expected to generate images of such high accuracy as warranted.
我发现这可以有效地使渲染的字体在跨平台上相同,以进行简单事物(例如覆盖在静态图像上的文本)的像素完美图形单元测试。
Font
和FontRenderContext
注入到渲染字体的类中,以便可以在测试下控制它们。Font.createFont()
从该文件生成字体。FontRenderContext
中禁用抗锯齿功能。如果您跳过此步骤,结果似乎确实会因平台而异。我很好奇其他人是否认为这是脆弱的或容易失败的,但到目前为止我已经取得了很好的结果。
I've found this to be effective at making rendered fonts identical cross-platform for pixel perfect graphics unit tests of simple things like text overlaid on a static image.
Font
andFontRenderContext
into the class rendering the fonts, so they can be controlled under test.Font.createFont()
to generate a font from the file.FontRenderContext
used. If you skip this step, the results do vary cross platform it seems.I'm curious if others think this is brittle or failure-prone for any reason, but I've had good results with it so far.