Today, I would like to talk about my goal to successfully implement a framework to do unit testing while developing Gamebuino games. While it may sounds not fun at all (well, yeah, honestly, it's not as fun as developing the game itself), I found that unit tests are an invaluable tool that helps you write better programs that behave as you wanted to and will save you a lot of time in the long run. I write unit tests every day in my job, but with more high-level languages (ones with lot of machine abstractions, so lot more easy to manipulate than our good old C/C++) like Python or NodeJS.
In C/C++, it takes a bit more of legwork, but fortunately, the Google Test library helps a lot with that: https://github.com/google/googletest
The hard part is that we want to execute the tests on the computer, not on the Gamebuino. It would slow and a pain in the ass.
The solution is to use a mock. A mock is a fake implementation of a class/function/library, that will allow us to replace and observe the behaviour of the code during the tests. In our case, we'll have to mock the
Gamebuino-Meta library to abstract all the hardware-related logic that won't work on our computer.
I've prototyped a working example here : https://github.com/frankie567/conquest/tree/googletest. Unit tests build is setup using a
Makefile, and I have mocked a very small part of the Gamebuino library by hand. My objective now is to use the Google Mock library which helps to do that, plus offers tools to check that a method was called or force the behaviour of a class.
When we get all those tools, we'll be able to write automatic tests that simulates the actions of a player and checks that we get the expected behaviour.
I'll keep you updated regularly about my progresses on this subject!