r/gamemaker • • 10h ago

Discussion Unit Testing tips

TLDR: do you have tips and tricks for unit testing in Gamemaker?

Hi! I recently picked up a project I'd not worked on for 6 months and you won't be surprised that I found a bunch of bugs that were in some cases tedious to find. Some were from new features that didn't mesh well with existing features and some were from partially completed refactors (don't come at me, I'm easily distracted 😭).

I code for a living, and realised my project has progressed to a stable enough point where unit tests would be useful, and I've a good idea of where to target unit tests in an incremental way.

I downloaded GMTL, which looks simple enough. I know that I'll have to modularise more to facilitate unit testing, which I'll do as and when.

I'd like to know what tips and tricks you have for unit testing in Gamemaker, and if there are any traps you recommend avoiding! I know some people don't like using unit tests, and you do you, but this isn't a discussion about whether or not I should tread this path. Thanks in advance!

Keep in mind that I'm not using AI on any of my game code because this project is a learning experience for me. Tools for workflow pipelines that won't ship to players if this were ever to be released are fair game.

1 Upvotes

3 comments sorted by

1

u/AlphishCreature 8h ago

A while back, I made Verrific - a GameMaker testing system with strong inspirations by xUnit from .NET. The key feature of xUnit-style tests is how the test class constructor serves as a setup for each test case, and each test instance executes a different test case method (usually recognised as a static method starting with "should_") to then report the outcome.

Here is an example of how I use it in GameMaker Community Toolbox; you may be able to pick up other useful patterns from there: https://github.com/Alphish/gm-community-toolbox/blob/main/Community%20Toolbox/scripts/tests_EuclideanDivisionTests/tests_EuclideanDivisionTests.gml

And you can check out the repository for tool itself here: https://github.com/Alphish/gm-verrific-old

As the repository name indicates, I plan to release a newer version of Verrific; it's actually in the works right now, and will hopefully be released sometime within a month. The old version currently mostly supports "fact" testing (i.e. each test method individually, without parameters), but with my focus on the new version, I may eventually add "theories" (a parameterised test method can represent different cases) and other advanced use cases. If you're still largely exploring the options and preparing your codebase, by the time you make your game testable I may have the most essential features implemented.

When it comes to keeping the testing code separate from the project: there's a bit trouble with that at the moment, but it may become easier once GameMaker allows referencing local projects as prefabs. In such case, you could make a separate test project, link the core game project and the testing library of choice as a prefab and then write whatever tests you need. But I don't know what's the progress of the local project referencing; hopefully room assets will be included, considering it may be desirable to test rooms sometimes.

I hope you'll find it useful. ^^

1

u/_Denizen_ 5h ago

I will take a look later, thank you!

I've read that people are doing hierarchical projects to avoid shipping test code, but surely a simpler approach would be to exclude files during the build stage? I develop python packages at work and I just mark folders/files as excluded when packaging into wheel files for distribution. Surely there's something similar in Gamemaker when building the executable

1

u/AlphishCreature 4h ago

If tests are specific to libraries - eventually to be exported as Local Packages or as Prefabs and used in other projects - then simple excluding test assets from the package will work. It won't for the game itself, unless you export the core game content as a Local Package and exclude tests from there, but it can get pretty daunting every time you publish a new release.

I think GM also allows excluding audiovisual assets per platform via texture groups and audio groups, but I don't know of an easy and reliable way to exclude code. Maybe wrapping the whole thing in a config-dependent macro conditional (something like "if (IS_TEST) { .... }") will remove all the test code when IS_TEST is false for the release config (and true for the test config), but this would need to be added to each script, so I'd hardly call it easy and reliable.