Unit Testing In Video Games

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
dejai
Posts: 522
Joined: Sat Apr 21, 2007 9:00 am

Unit Testing In Video Games

Post by dejai »

Sourced:http://bwright.uberwolf.com/2010/07/07/ ... deo-games/
Unit testing is an “agile” or “extreme” programming methodology which has a lot of beneficial side effects for medium to large scale systems. For example when you modify a file in a 10(0*) thousand line code base what level of certainty do you have that your modifications have not adversely effected some other section of the system? In addition to this what methods do you employ to justify that your code functions correctly across all perceivable cases?

Generally programmers do not have good answers to these questions and as usually we find a way to automate these tasks by creating tests which throw every possible case we can imagine at our program such as jargon, valid input, semi-valid input and we some how measure to what degree the system handled that. Of course we want it to pass all the tests and if it breaks a test we can analyses what is breaking it and repair the system. In theory this makes a very good premise for why we would use unit testing in our applications and for many systems this is extremely achievable for example in an XML parsing library or a video conversion library. But for video games and rendering engines we encounter the issue that graphical components of games cannot be analyses by the computer to see if they meet our standards and it is difficult to describe to a computer what we want in this sense. This also extends to game play.

So how do we solve this issue? I have a few suggestions which you may find useful and they come in at varying degrees of difficulty in terms of implementation. Firstly any game engine worth its salt really should have an error / engine log which logs important engine events such as loading images, maps, data and the like. If you don’t have this in your engine I would suggest this as a first step in the right direction. While this may not actually be unit testing you may find that it accommodates the infrastructure required for testing your system. If you are worrying about the performance hit from this you are worrying too much about micro optimizations and if this is going to be on an embedded system you can simply cover it with a macro statement which you can assign to nothing and then in the preprocessing it will evaluate to nothing. For example #define ERR_LOG(msg) ErrLog( message ); could be changed to just #define ERR_LOG(X) etc.

For ease of use also develop in some hooks into your error logging system for example you could return integer values or a small error object that would hold information on what happened with the error or the process that occured. Was it A:Worked With No Issue. B: Recovered From Failure (such as replacing an image that would not load with a generic image), C: Fail. Now with your hooks in place you can use these to diagnose errors in the system. I am not going to go into specifics as to how you implement this system as it is really up to you to determine (which is all the fun in programming) and to what level you determine it is needed given your game. If you are writing pong I would not even bother (unless you want experience with building the system) however if you are building an RPG you may really want to consider a reasonably sophisticated error logging system.

One you have an error handling system in place you can begin to implement a basic unit testing system. If you have written your error logging system well you may be able to reuse it to handle unit testing “objects” and unit testing “logging” as well. You then write a series of tests loading in good images, good scripts, bad images, bad scripts (and perhaps the unit testing object holds a message as to what you are testing) and you see how your system responds and then develop appropriate coping measures. For example if your game cannot deal with an image not being loaded replace it with an engine generated texture of a pink or blue solid colour (or something along those lines) and then re run it against the unit tests. Then before you add more code, design what you want to write for example you might be writing a section of the physics module for your engine. Write the tests first for that system then write the base code and try and implement a system which passes all the test cases. It may seem like a hassle but you will actually save time writing the code base as you know what test cases you have and you will also be able to accurately determine the performance of your new module to achieve its tasks and sort out any bugs.

The error logging system and the unit testing system should be the way in which you wish to analyses your system from a programmatic standpoint and you should design your system to achieve this goal: To make your life easier and your code better.
What have I missed and how can I improve this article? Please relate to personal experiences with system design and game development. Cheers! (This is not homework, this is just something I have been thinking about).
Programming Blog: http://www.uberwolf.com
Post Reply