Gunit Test Framework

Gunit is a unit testing framework for games.

While the framework does include some classic unit testing facilities similar to what you can find most test frameworks like XUnit or JUnit, the main feature is asynchronous unit tests that can run in the game loop and drive the game the same way that a player does. A few years ago I would’ve emphatically exclaimed: “NO! That’s not a unit test. That sounds like an integration test or an end-to-end test…”.

However, I have since learnt that the “unit” does not necessarily have to be the smallest bit of code that can be tested in isolation, like a class or a function. In fact, I now believe that testing like that is a bad pattern: it couples your tests very directly to your implementation, which means the moment you want to refactor that implementation the tests become wrong and you end up either having to delete them or spend hours trying to adjust them. By using a coarser unit test scope you focus your test more on the requirements you are trying to implement and less on the current implementation, making it easier to refactor in future.

I’ve spent most of the last few weeks getting this into a basic working state, now it is time to release and see if this works in some real game development projects. Hope you find it useful.

Happy testing!


Overview

Gunit GitLab: https://gitlab.com/mattpwest/gunit-defold
Gunit Docs: https://gitlab.com/mattpwest/gunit-defold/-/tree/master/docs

The best way to see some tests in action is to clone the repository, open it in Defold and run it, you’ll see the tests run before the example game becomes playable.

Example of use

Here is a quick demo of what running the tests looks like:

For details on how to integrate this into your game please read the README and the docs.

Example CICD Pipeline

You can look at Gunit’s .gitlab-ci.yml for a simple example of running the tests in pipeline before building the game for Windows and Linux.

7 Likes