Introduction
This is the first article of series of articles related to Test-driven development (TDD) approach in Microsoft.NET. My intention is to illustrate this approach with several real-world examples, and this article is an introduction to the approach and the testing framework NUnit.
Test-driven approach is a new paradigm to develop enterprise applications and it's close to agile methodologies such as SCRUM and Extreme Programming. Although, a lot of developers think that test-driven development is all about testing, I want to say that TTD is a strong approach to specify requirements, implement them and test them in order to support firm concepts related to development process such as Quality Assurance (QA). Therefore, we have a great feedback in the development process and a lot of problems can be discovered and corrected early in the product life cycle.
It's remarkable to say that developers usually don't follow this approach because they don't want to write test code firstly; instead of writing the code for the business logic supporting the features and requirements (it's a waste of time!!!). But one important thing to do is that the test reflects the requirements and they're a way to show you how to implement and check correctness (early fixing bugs) on your modules. Well, every developer has cook book, and they know what approach is best concerning the problems and their contexts. Finally, I want to say that this is another approach that at least some developers should experiment and then see the results.
In this article, I will show how programmers can use the test-driven development approach with real-world example in order to illustrate the principles, methods and techniques using the NUnit which is an open source unit testing tool built for .NET, which follows the same approach to other xUnit testing tools specifically the JUnit for testing Java modules.
NUnit quick start
NUnit is a framework to support the development, management, and execution of automated tests. It was initially ported from JUnit in the Java world and written entirely in C# language in order to take advantages of the features of Microsoft.NET platform.
The first step is to download the framework from www.nunit.org and install the MSI file in your box (for the examples, I've downloaded the NUnit for Microsoft.NET Framework 2.0). It provides a simple way to write tests of the .NET classes by using attributes to define tests and test suites. The main attributes are TestFixture, which applies to the class containing tests, Setup and TearDown which are run before and after each test respectively and Test.
NUnit also provides a graphical user interface that lets you which assembly you want to test and which set of tests you want to run. When the tests run, then the GUI shows a green bar if everything passes the test and a red bar if at least one test failed (see Figure 1).

Figure 1
There is an NUnit add-in for Visual Studio which enable to run the test without leaving the IDE. Just simple right-click on a project or a solution to run the tests. The results are displayed in the Output window in Visual Studio.NET.
And finally, we have another tool in NUnit called NUnitASP which enable testing Web modules.
Now let's see how NUnit is used in .NET applications with a simple example. First of all, open Visual Studio.NET and create a Class Library project (see Figure 2).

Figure 2
Now we're going to implement a CalcTester class whose behavior is check that the sum of add two numbers is correct. In order to integrate this test class with the NUnit framework, we have to add a reference to the nunit.framework.dll assembly (see Figure 3).

Figure 3
Now let's add the code for the tester class (see Listing 1).
using System;
using NUnit.Framework;
namespace TDD_Test1
{
[TestFixture]




Join the conversation! Your thoughts help the community grow.