Test-driven development approach in Microsoft.NET using NUnit

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).

1.gif

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).

2.gif

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).

3.gif

Figure 3

Now let's add the code for the tester class (see Listing 1).

using System;

using NUnit.Framework;

 

namespace TDD_Test1

{

    [TestFixture]

    public class CalcTester

    {

        [Test]

        public void TestSum()

        {

            int nNum1 = 10;

            int nNum2 = 20;

            int nSum = nNum1+nNum2;

 

            Assert.AreEqual(30,nSum);

        }

    }

}

Listing 1

To automatically run the GUI NUnit test runner from within Visual Studio.NET, right-click on the project in the Solution Explorer and select Project from the context menu. Go to the Debug tab, and in the Start Action, select the Start external program option and set to the path C:\Program Files\NUnit 2.4.8\bin\nunit.exe (see Figure 4).

4.gif

Figure 4

Now compile and run the test. After that, the GUI NUnit test runner will appear. When you run the GUI NUnit test runner for the first time, it opens with no loaded test. So, go to the File | Open Project and select the TDD_Test1.dll assembly (the assembly which contains all our test classes) as shown in Figure 5.

5.gif

Figure 5

If you run the GUI NUnit test runner again, you will see that the project is already loaded. Now, click on the Run button and see the results (see Figure 6).

6.gif

Figure 6

Now let's take a look to the layout of the GUI NUnit test runner. After you run the test, the progress bar reflects the status of the execution following the colors:

  • Green indicating that all tests executed successfully.
  • Yellow indicating that some tests are ignored but there are no failures.
  • Red indicating that there are failures.

The status bar will give a summary of the current test being run. The Test Cases field displays the amount of test cases found the assembly. Tests Run field displays a running count of tests that have been completed. Failures field displays a running count of tests that have been failed. Time field displays how long it took to run the tests (time is displayed in seconds).

Now we can talk a little about the jargon in the Test-Driven Development approach. A test case is a self-validating programmer test that can be automatically discovered and executed by a test runner.

Test cases are grouped by a test suite.

A test fixture is a group of test cases sharing a common set of runtime resources. It is implemented by annotating fixture classes (classes with test code) with the attribute TestFixture. These classes have to be public in order to be discovered by the test runner. Each test assembly has at least one fixture class.

Tests contain the code to test particular business logic. It's implemented by annotating test methods (implementing test cases) with the attribute Test. These methods must be declared as public, be an instance method (non-static), with return type as void, and take no parameters.

The test runner is the program which automatically discovers, organizes, runs and collects the results of the execution of the test cases.

Assertions are the mechanisms to implement self-verifying test cases. They are a simple statement of truth and they are used by test runners to collect and report failures.

Conclusion

In this article, I've shown how programmers can use the test-driven development approach. I've illustrated the principles, methods and techniques using the NUnit which is an open source unit testing tool built for .NET. Now you can create your own tests for your classes and run them in order to check the correctness.

Up Next
    Ebook Download
    View all
    Learn
    View all