# How can i write automated tests for some SDL2 objects?

For eg having a class that is rendering something on the screen, could be even a point or a line if not an image or whatever.

How is possible to write some tests for that class only?

or just a small program rendering a line, would it be possible to write an automated test for it?

There are a few approaches you could take for testing rendering output.

First, you could create a test mock version of your renderer, that instead of plotting things on the screen, would instead perhaps store some kind of representation of the rendering output, such as lists of renderer elements that you could assert against. This sort of approach is often used in webdev testing, where the output of components is tested using DOM elements instead of directly checking the output.

Your tests could then look something like this:

MyMockRenderer renderer;
MyObject object;
object.render(&renderer);
TEST_ASSERT(renderer.contains(Line(expectedStart, expectedEnd)));
TEST_ASSERT(renderer.contains(Image(playerImage, expectedPosition)));


Alternatively, you could first manually visually verify a rendered output that looks good, and implement code for storing screenshots of the output. Then, your automated testing procedure could launch the game, perform the rendering, capture the framebuffer contents, and compare those to the previously verified output images. This could be beneficial to find regression bugs for an example. I believe the term for this is "snapshot testing".

• i was considering the screenshot approach, thought was too much "invasive", but could work. The mock one as well something that i thought but need to refactor a lot, probably no other solution than also using the DIP for renderer and abstract from SDL, i guess. I was hoping SDL might have some utility to help test.. thanks – Raffaello Mar 21 at 18:48