To read the article online, visit http://www.4GuysFromRolla.com/articles/011905-1.aspx

Test Driven Development Using NUnit in C#

By Muthukumar. V


Introduction


Testing during development is one of the basic tenets of eXtreme Programming (XP). Another proposition of eXtreme Programming is automation of every possible, repetitive task. This is because automation improves productivity to a very large extent. Put these two tenants together and you have the bedrock for an increasingly popular design methodology: Test Driven Development (TDD).

The most enjoyed phase of software development will always be Design and Coding, while the most hated parts of software development will always be bug fixing. Just ask yourself - would you rather be designing a system and/or writing the code, or would you rather be picking through an application and running tests, trying to squash a bug? Regardless of how unenjoyable testing and bug hunting are, they are necessary steps in the creation of an application. Despite how good of a programmer you may be, you will still produce bugs somewhere along the line.

A number of different software development methodologies have been proposed to reduce the amount of bugs in code. To understand the effects of bugs that creep up during the design process, realize that:

  • One bug in code is equal to only one bug - though fixing it might produce more!
  • One bug created in the design phase is equal to more than 10 bugs in the coding phase
  • One bug created in requirement phase is equal to more than 100 bugs in the coding phase
To aid with detecting bugs as early as possible, many companies are moving toward a development technique called Test Driven Development. In this article we'll examine the basics of TDD and examine how to implement TDD using a free tool, NUnit. In addition to showing how to setup and use NUnit, this article also includes a sample that you can download and tinker with.

Test Driven Development, Unit Tests, and NUnit, Oh My!


When sitting down to create an application, many developers start by writing the code. Test Driven Development emphasizes the opposite, stressing the need to prepare test scenarios or test cases before writing the code itself. This seemingly backwards approach has some benefits. First of all, it requires that the programmer be very clear about what tests the program should pass and what test it should fail, bringing such concerns to the forefront of the software design process. Furthermore, by meticulously detailing what tests a system should pass and fail we can use tools to automate most of our tests. An automated job is one that's always very, very easy to do. These automated tests are meant to be run every time there's a code change and are referred to as unit tests.

NUnit is a free, open-source tool for .NET that is developed as a framework which can help automate unit testing. (The same unit test framework was already available for Java and was named jUnit.) NUnit can be downloaded from NUnit site or the SourceForge NUnit page.

Installation and Setup of NUnit


After downloading the NUnit from either of the above download pages, run the installation program, NUnit-XXX.msi. At the time of this writing, the current version of NUnit is 2.2.0 and the setup file name is NUnit-2.2.0.msi. This setup will install the required libraries.

NUnit provides two utilities which can be used for running automated tests:

  1. nunit-gui.exe, a GUI tool which I most commonly use, and
  2. nunit-console.exe, which is a console program
Once you have installed NUnit you can test if the installation has gone through properly using the nunit-gui.exe tool. This can be run from Start Menu --> Programs --> Nunit --> NUnit GUI. After opening the tool, click on the File --> Open and open the nunit.tests.dll from the C:\Program Files\NUnit 2.2\bin folder. (The folder could be different if the installation has happened by choosing a different folder.) When the DLL is chosen, NUnit-GUI shows all the unit test code written for the particular DLL using the NUnit framework. If the button Run is clicked, it will show the results as follows.
  • Passed Test - Green color
  • Failed Test - Red color
  • Ignored Test - Yellow color
The nunit.tests.dll DLL contains over 600 tests and when run all of them should pass and turn the color to Green, as shown in the screenshot below. This means our installation is properly complete.

Using NUnit to Apply Unit Tests in a Visual Studio .NET Project


In order to run test cases we'll need to things:
  1. The test cases themselves, and
  2. The program that is having test cases run against it
Recall that the doctrine of Test Driven Development is to create the test cases prior to writing the actual code, meaning you'll actually be writing code for the tests before you find yourself writing code for the application. However, let's do things a bit backwards here and examine the sample application and its code first. (The code and test cases are available for download at the end of this article.)

The sample application is a command-line application with a single class, Person. The Person class has three properties: Age, FullName, and CashBalance, along with a method, BuyCar(amount), which debits amount from the person's CashBalance. The germane code is shown below:

class Person
{
   //Data Declarations
   string m_FName;
   string m_LName;
   int m_Age;
   float m_cash;

   #region Constructors
   Person()
   {
      m_FName = "";
      m_LName = "";
      m_Age = 0;
      m_cash = 0;
   }   
   
   public Person(string strFName,string strLName, int iAge)
   {
      m_FName = strFName;
      m_LName = strLName;
      m_Age = iAge;
      m_cash = 100000;
   }
   #endregion

   #region Properties
   public int Age
   {
      get{return m_Age;}
   }
   
   public string FullName
   {
      get{return m_FName+ " " + m_LName;}
   }
   
   public float CashBalance
   {
      get {return m_cash;}
   }
   #endregion

   #region Methods
   public void BuyCar(float fCost)
   {
      m_cash = m_cash - fCost;
   }
   #endregion
}

Ideally we wouldn't yet have written the code for the Person class. Instead, we'd just have documentation that described the behavior of the Person class. This behavior might be spelled out as follows:

  • A person has properties to access their age, full name, and cash balance.
  • When instantiating a person, we should be able to provide the first and last name, along with the age.
  • The person's full name property should return the specified first and last names with a space in-between.
  • When instantiating a person the cash balance should be 100,000.
  • The cash balance should be reduced by the amount spent on car purchase.
Given this behavior, we can formulate a number of test cases. When devising test cases keep in mind that test cases should be simple and easy to implement in code. Some test cases might be:
  • If we create a person with first and last name of "John" and "Abraham," and age 10, the person's full name should not be "John Smith"
  • This person's full name should be "John Abraham"
  • This person's age should be 10
  • Before purchasing a car, this person's cash balance should be 100,000
  • After buying a 20,000 car, the person's cash balance should be 80,000
We can implement these test cases in code using NUnit. To create a test case in your application create a class that is tagged with the [TestFixture] attribute. This class will contain all the relevant tests to test the methods, operations, and values in the Person class. This [TestFixture] class will also contain the capability to initialize, instantiate and operate on our methods and classes to be tested. This test class will look something like:

[TestFixture]
class MyTestClass
{ 
   //declarations ..
   [SetUp] public void Init()
   {
      //All initializations
   }
   
   [Test] public void IsNameEqual(string strNameVal)
   {
      Assert.AreEqual("TestName",strNameVal);
   }
}

The test class can contain a method that sets up the test run. This method should be marked with the [SetUp] attribute, as shown above. Usually class instantiations, initialization of variables, etc., are performed here. Each test case you want to run should be a public method marked with the [Test] attribute. Inside the test cases you'll commonly find Assert statements that test for equality or Boolean conditions. Some of the methods available in the Assert class include: AreEqual(), AreSame(), Fail(), Ignore(), IsNull(), and so on. The class also has some more utility functions like Fail() and Ignore(), which can be used for custom error case generations. For more information see http://www.nunit.org/index.php?p=assertions&r=2.2.

NUnit has more attributes that just [SetUp] and [Test]. Attributes like [Teardown], [Ignore()], [ExpectedException], [Category], [Explicit], and others add some value to the testing process. For more details about these attributes see http://www.nunit.org/index.php?p=attributes&r=2.2.

The following code shows example setup and test case methods for the Person class:

using NUnit.Framework;

[TestFixture]
public class TestClass
{
   Person pTest;
   public TestClass()
   {
      //
      // TODO: Add constructor logic here
      //
   }


   [SetUp]public void Init()
   {
      pTest = new Person("John", "Abraham", 10);
   }

   [Test]public void IsNameJohnSmith()
   {
      Assert.IsFalse("John Smith" == pTest.FullName);
   }

   [Test]public void IsAgeEqual()
   {
      Assert.AreEqual(10, pTest.Age);
   }

   [Test]public void IsNameJohnAbraham()
   {
      Assert.AreEqual("John Abraham", pTest.FullName);
   }

   [Test]public void BalanceBeforeCarPurchase()
   {
      Assert.AreEqual(100000, pTest.CashBalance);
   }

   [Test]public void BalanceAfterCarPurchase()
   {
      pTest.BuyCar(20000);
      Assert.AreEqual(80000, pTest.CashBalance);
   }
}

Note that in the test class we have using NUnit.Framework;. You must also add the C:\Program Files\NUnit 2.2\bin\nunit.framework.dll assembly to the application's References folder. (Right click on the References folder in the Solution Explorer, choose Add, and navigate to the appropriate assembly.)

Running the Test Cases


The final step is to run the test cases we just created. To do this, launch the NUnit-GUI tool, go to the File menu and select Open. Next, browse to the EXE file for the sample application. This will then load the test cases in the NUnit-GUI tool, at which point we can click the Run button to begin the tests. Ideally, all test cases should pass, as shown in the screenshot below:

These test cases should be run after ever major code change. In fact, NUnit can be run from the command-line, meaning that you can include unit test case running as one of the steps in your application's build process.

Conclusion and More Information


In this article we took a look at Test Driven Development (TDD), unit testing, and NUnit, seeing how easy it was to create automated tests through the help of NUnit. At this point, however, you may be wondering how unit testing could help an ASP.NET application. Unit testing with NUnit is useful for testing class libraries that makeup an ASP.NET application, but is not designed to test the UI portions of an ASP.NET application. There is, however, an additional free tool, NUnitAsp, that is designed to provide unit testing for the GUI portion of a Web application. We'll examine this in a future article!

I leave you with some resources one can get more information on Test Driven Development, Extreme Programming and related areas..

Happy Programming!

  • By Muthukumar. V
    http://www.codersource.net


    Attachments


  • Download the complete sample application (in ZIP format)
  • Article Information
    Article Title: ASP.NET.Test Driven Development Using NUnit in C#
    Article Author: Muthukumar. V
    Published Date: January 19, 2005
    Article URL: http://www.4GuysFromRolla.com/articles/011905-1.aspx


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers