r/truegamedev • u/boccy • Jan 14 '14
Unit testing
I was wondering does anyone use unit testing in game dev? It's something that my team has been considering but I'm not sure I see the value in games.
There are so few "units" in our code base I'm not sure it would work out. What do other people think?
Now functional testing is a better fit and validating data is very useful so we may start to implement those.
18
Upvotes
19
u/mooli Jan 15 '14
Some comments based on using unit and integration testing in games:
8
u/gunder_bc Jan 14 '14
Focusing on the semantic hair splitting of "unit" vs. "functional" testing is counter productive. Think of it all as automated testing - what tests can you automate, across all levels of code in your game and pipeline? If you're building out low-level libs with specialized containers, sure, those could be classed as "unit tests", but regardless of the label, more automated tests == more confidence in your code's correctness.
2
u/combatdave Jan 15 '14
While we're on the subject, there's always talk of "rapid prototyping" and "rapid development" being the best you can do for game dev, and that you should be making a game not writing code - and I totally agree with that. Isn't that way of working kind of at odds with automated testing, though? It's for this reason that I dislike unit tests etc - but I'd love to hear opinions about why I'm wrong (or right).
6
u/rtlechow Jan 15 '14 edited Aug 09 '19
From a unit testing perspective, it might be that there are "so few units" because there are no tests. Tests can force the design of things that work in isolation.
5
u/vectorj Jan 15 '14
The biggest hurdle in games regarding this topic is... finding a separation of the external frameworks/libraries from your game specific logic.
The more a class does/depends on... the harder it is to test. If external api's can be abstracted, they become a literal plug-in to your game. You can then pass mocked/stubbed versions of these for testing.
Any game engine you look at online will have tutorials and samples that put game logic right alongside the api calls. It gets the point across quickly and centralizes everything for the purpose of education, but it's not the best approach. Unfortunately this kind of approach is what is considered the norm. It's how most programmers have learned and cemented into their style.
Another important concept is to follow the 'single responsibility principle'. Classes should do 1 thing and do it well. This will leave you with MANY small classes with small methods that is your 'slice' or 'unit' that should then be very trivial to test.
Investigating how to get a game properly under test is a challenging journey. But if you understand the benefits of a test suite and the importance of loose coupling that testable code demands... You'll get a glimpse at what is the future of the industry. Automating QA is the future. It's costs too much time/money to 'fiddle' and visually check your changes yourself.
7
u/kylotan Jan 15 '14
If you have few "units" in your code base, it usually means there's something wrong with that code. The ideal code base would normally have many modules of code which have little dependency on other parts of the code, and therefore each module could be tested individually without too much trouble. Sure, the main loop is tricky to test in isolation. But the input system, sound system, file loading, AI routines like pathfinding, character spawning, world generation, networking, serialisation... all testable if you write them well.
I use unit testing wherever I can, but I don't expect 100% coverage. On server-side code I aim for something like 80-90% and on client-side code I'd aim for at least 50%.
I also don't do "test-driven" development, which I consider to be counterproductive.