Unit testing a gamebuino program with google test - By fvoron

Author :  fvoron

Hi there!

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!



Author :  jicehel

Great project François. It's one thing i don't have done yet but now with modern programmation, the automatic tests have to be coded as you proramm to using Agile methods. Nice to have possibility to learn it with META programs. Thanks for the informations about your progresses.

Author :  Aurélien Rodot

Pretty neat project, I've never done unit testing myself.
You should make a Tutorial Creation instead of just a discussion that will get lost over time ^^

Author :  fvoron

Thanks @jicehel!

@Aurélien, you bet I will! But for now, I felt it was still a little bit too "hacky" to propose it officially to the community. Once I have a clean library and I a (quite) easy install, I'll write a nice tutorial :)

Author :  Aurélien Rodot

You can tag is as "work in progress", that's what it's for, so people can start following your progress ;)

Author :  fvoron

I didn't know about that feature :) I'll check then!

Author :  Sorunome

This looks like a very interesting project, definitely something to follow!

Automated testing can indeed help a lot with accidental code breakings etc. I can see this being really useful!

Author :  fvoron

Thanks Sorunome!

I've not progressed much since my last post, but I think I have a clear idea of how to proceed. In googlemock documentation, they assume that the class we want to mock inherits from an interface with virtual methods ; so we would just have to create a mock class inheriting from this interface and change the implementation we use at runtime through dependency injection.

Well the thing is, it's not the way the Gamebuino library works (and I have to admit it would be quite painful to inject a Gamebuino Meta instance in each class). So, the main work is to create a Google Mock class behaving the same way as the library (as explained here) and use the "include trick" with the TEST compiler constant, as I already did with my current implementation. 

What do you think? :)

Author :  Sorunome

and use the "include trick" with the TEST compiler constant, as I already did with my current implementation

You can use config-gamebuino.h for adding global constants to the gamebuino project. They will be present in every file you did #include <Gamebuino-Meta.h>