Friday, September 23, 2005

Test Driven Development in .NET - NUnit

I heard about Test Driven Developemnt for the first time when i was working on my coursework to develop an instant messenger back in October 2004. One of the requirements for coursework completion was to have unit tests for the piece of code to be written using NUnit. Thus i heard the word NUnit.

There are several articles available on thw web about Test driven developemnt and NUnit.

In this blog post, I am going to summarize my expereince and what i have learned till now regarding Test Driven Development.

The first question is
"What is Test Driven Development?"

According to me, it is a three step process:
- Write test,
- Write code,
- Refactor.
Putting it in another way:
Instead of designing a module, then coding it and then testing it, we turn the process around and do the testing first. The typical programming sequence is something like this:
- Write a test.
- Run the test. It fails to compile because the code you're trying to test doesn't even exist yet! (This is the same thing as failing.)
- Write a bare-bones stub to make the test compile.
- Run the test. It should fail. (If it doesn't, then the test wasn't very good.)
- Implement the code to make the test pass.
- Run the test. It should pass. (If it doesn't, back up one step and try again.)
- Start over with a new test!
(Ref:Test-Driven Development in .NET By Peter Provost)

Reasons for Test Driven Development (TDD)
A unit test provides an example of what a piece of code is supposed to do. It gives a much clearer idea as to how the code is executing, once a particular set of input is provided. Going through the code in the debugger only tells us what it is doing, not what it is supposed to do. Moreover when we fix any bug in the code or if we change even a single line of code, we can never be sure as to how that change would affect the rest of the code. Many times it happens that one fix leads to another bug. In this scenario unit tests can be very useful, because they allow checking to analyze if we broke something else in the code. Also Test Driven Development leads to the overall improvement of the design of the application, because it makes the developer think right form the starting point as to how the application should behave and thus how should it be designed.

What are Unit tests?
“A unit test verifies that a function or set of functions "honors its contract" - in other words, that the function(s) under test meet the requirements. Unit tests inspect both black boxes and white boxes.”

NUnit test Framework
NUnit is an application designed to facilitate unit testing. It consists of both a command line and Window's interface, allowing it to be used both interactively and in automated test batches or integrated with the build process.

No comments:

Post a Comment