Game Development Reference
In-Depth Information
Don't worry if you've noticed a bug; it's there on purpose, I promise! Run the
tests and they should all pass. The code is verified, but the tests don't exhaustively
cover all the code. For instance, there is a TestPlayerIsDead-
WhenHasNoHealth but not a ''test the player is not dead when health is greater
than 0''. I'll leave this one as a reader exercise.
Let's say we keep on developing in this way and get to the stage where we have a
full working game. Then during testing something strange occurs; sometimes,
when the player should die, he doesn't, and after that he's invincible. This isn't
how the game is supposed to work. All the tests are passing, so that means the
bug isn't being tested for. Doing some clever debugging, it's discovered that the
bug occurs when the player has low life, perhaps two or less units of health, and is
then hit a lot of times by a number of enemies.
In unit testing, the first task is to write a test that reproduces the bug. Once this
test is written, the code can be fixed and the test can confirm it. Here's a test that
covers the bug.
class PlayerTests
{
bool TestPlayerShouldBeDead()
{
Player p ¼ new Player();
p.Health ¼ 2; // give him low health
// Now hit him a lot, to reproduce the bug description
p.OnHit();
p.OnHit();
p.OnHit();
p.OnHit();
p.OnHit();
if ( p.IsDead() )
{
return true;
}
return false;
}
}
This test is run, and it fails. Now we've got a unit test that reproduces the bug.
The code must now be changed to pass this test. The death function seems a
good place to fix the bug.
Search WWH ::




Custom Search