The journal of Paul M. Watson.

Thursday, September 15, 2005

It failed again?

Unit Test by Paul Watson

That is the NUnit interface showing a failed unit test of mine. The unit test was meant to fail as I had not yet implemented the class I was testing. First you fail your tests, then you write your code until the test passes. I am following the NUnit Get Started guide.

The grand thing is that I was unintentionally shown the value of this unit test even as I was following the guide. The first step in the guide makes you write a unit test that fails against the account class. The unit test runs various methods to withdraw and deposit funds with the final test being a balance check. At that stage your account class has only stub methods so withdrawing and depositing did nothing which meant balance checking returned erroneous values. The next step is to add just enough code to the methods so that the unit test will pass. So I followed the code they provided, compiled and ran the unit test. Only it failed, again. The guide clearly said it should pass at this stage.

So I went back to my account class and looked over the code. I spotted my mistake; I had balance += amount; in the Withdraw method instead of balance -= amount;.

Now if I had been doing this the old way with no unit tests I would not have spotted my mistake (because it all compiled fine and there were no runtime errors) until my client had come back and said "Buddy, when I withdraw, my balance goes up! What the hell is going on?".

So wow, what a great illumination in my first 30 minutes of trying out test driven development.

2 Comments:

Anonymous Anonymous said...

This comment has been removed by a blog administrator.

2:26 AM

 
Anonymous Anonymous said...

Well done you for getting in to unit testing and TDD. FlexWiki is done the same way and I do keep trying to do the same with my own projects. (Shame on you for leaving it so late to start though :P)

10:49 AM

 

Post a Comment

Links to this post:

Create a Link

<< Home