Game Development Reference
In-Depth Information
class Player
{
bool IsDead()
{
return Health <= 0;
}
...
}
If the player has zero health or less, then he is dead. This causes all the unit tests to pass,
and we're now sure that this bug won't crop back up without us knowing about it.
Test Driven Development
The method of writing tests, then skeleton code, and then making that code pass
the test is called Test Driven Development. Developing code this way reduces bugs
and you can have faith in what your code actually does. This is very important if
you're developing a big project. It also encourages classes to be quite modular
and self-contained. This is because unit tests need to be small; therefore, you
want to avoid having to make 50 classes and setting them all up just to run one
simple test. For this reason, lots of needless coupling between classes is avoided.
The other big win on unit testing is refactoring. For instance, if the inventory
system in your game needs to be massively redesigned, then with unit testing you
have all the tests required to confirm you've replaced the system correctly.
With unit testing, you can confidently say your code is bulletproof. Un-
fortunately, not everything can be tested; graphics are very hard to test and you
just have to assume the libraries such as OpenGL will work correctly.
Unit Testing in C#
The best unit-testing software for C# is called NUnit (Figure 3.3). It's free and
easy to use.
In the following chapters, we'll download NUnit and walk through the setup. All
the code in the topic is unit tested, but for the sake of brevity, most unit-test code
is left out of the code examples.
Summary
Writing high quality software can be difficult; it's hard to write bug-free software
and it's hard to finish projects on time or even finish them at all. Modern
 
 
Search WWH ::




Custom Search