Automated Unit Testing

Unit testing is a software development and testing approach in which the smallest testable parts of an application, called units, are individually and independently tested to see if they are operating properly. Unit testing can be done manually but is usually automated. Unit testing is a part of the test-driven development (TDD) methodology that requires developers to first write failing unit tests. This article showcase how to perform a thorough unit testing in a c# perspective.

NUnit GUI

Nunit.exe latest version 2.6 can download from here: http://nunit.org/index.php?p=download.

The installation process hardly takes a few minutes to be completed once the executable is downloaded. The NUnit graphical user interface looks like as following after the installation process is complete.

Figure 1.1 NUnit GUI

Figure 1.1 NUnit GUI

After launching the NUnit.exe GUI, it is time to open a project in the form of a DLL or EXE file on which all the test cases will be executed. For that purpose go to the File menu and select Open Project, now choose the Test case DLL or EXE file and Unit Test case process is ready to execute as following;

NOTE: The NUnit software will only open the assembly files (DLL or EXE) that are developed under test-driven project methodology.

Figure 1.2 NUnit Project Loading

Figure 1.2 NUnit Project Loading

NUnit Testing Framework

NUnit is a unit testing framework for .NET applications, where the entire application is isolated into diverse modules and each module is tested independently to ensure whether or not the objective is met. The NUnit Framework caters to a range of attributes that are used during unit tests. They are used to define Test Fixtures, Test methods, ExpectedException and Ignore methods.

TextFixture Attribute

The TestFixture attribute is an indication that a class contains test methods. When you mention this attribute to a class in your project, the Test Runner application will scan it for test methods. The following code illustrates the usage of this attribute:

using System;
usingNUnit.Framework;
usingSystem.Text;

namespaceUNitTesting
{
  [TestFixture]
publicclassProgram
  {
  }
}

Test Attribute

The Test attribute indicates that a method within a test fixture should be run by the Test Runner application. The method must be public, return void, and will not be run when the Test Fixture is running. The following code depicts the use of this attribute:

[TestFixture]
publicclassProgram
  {
      [Test]
publicvoid Test()
      {
          …
      }
  }

Assert Class

Assert class is used to confirm whether or not the test cases are producing the expected result by using auxiliary methods such as AreEqual() orAreNotEqual().

ExpectedException Attribute

You could fortify your code to handle all kinds of exceptions by using try…Catch block. But sometimes you have circumstances where you actually want to ensure that an exception occurs. To overcome such a problem you should use the ExpectedException attribute, as you can see below:

[TestFixture]
publicclassProgram
  {
      [Test]
      [ExpectedException(typeof(DivideByZeroException))]
publicvoid Test()
      {
inti=10,j=0,x;
          x = i / j;
      }
  }

In the aforementioned code, we are intentionally committing a “divide by zero” mistake, which is detected during the NUnit Test execution.

Ignore Attribute

The Ignore attribute is required to indicate that a test should not be run on a particular method. The use the Ignore attribute is as follows:

[TestFixture]
publicclassProgram
  {
      [Test]
      [Ignore("This method is skipping")]
publicvoid Test()
      {
            …
      }
  }

Simple Business Logic Project

In the following scenario, we are implementing Discounts on particular sales made by customers, such as a 5% discount on $1000 to $1999 purchases, 10% on $2000 to $4999 and so on. Such discounts will be deducted automatically in the final payment based on some implicit calculation stated in the business logic.

  1. Create a C#.net Class Library project called UnitTest to produce a DLL
  2. Rename class1 to UtilityLib and put the following business logic:
  3. Now, build the project and notice that the UnitTest.dll file is created in the solution Bin/Debug folder, which will be referenced in the Unit Test project later

Test-Driven Project Development

There are a few steps needed to execute the unit test using NUnit:

  1. Create another C#.net Class Library project and call itUtilityLibtesting
  2. Then, add the reference of the UnitTest.dll file and the NUnit framework DLL files, which are located in the “ \Program Files\NUnit 2.6.2\bin\framework\nunit.framework.dll” directory Figure 1.3 Add references of NUnit framework and UnitTest.dll
  3. Now add the namespace definition to the class library file: using NUnit.Framework;usingUnitTest;
  4. Rename class1.cs to test case.cs with the TextFixture attribute.
  5. It is time to write test case methods to verify whether the business logic stated in the DLL file is producing the expected result or not.
  6. So we write another independent method for each correspond function given in the DLL file with the [test] attribute and we are also using the assert class to theAreEqual() method in order to confirm the desired result. The AreEqual() method takes two arguments; the first is the expected result and second, the particular method with the argument is defined in the business logic class.
  7. We also put the condition for actual test cases by using the assert class AreNotEqual() method, in which we pass any value as the first argument, as follows:
  8. Thereafter, we mention the rest of the code for each condition, as following:
  9. Finally, build the project and you will notice that UtilityLibTesting.dll file is created into the project solution directory, which will be used later during the unit testing in the NUnit application.
Figure 1.4 NUnit Testing.exe depict

Figure 1.4 NUnit Testing.exe depict

Running Test Case

Up until now, we have seen the basics of the code; now it is time to run your tests via the NUnit GUI. To use the GUI application, just simply run the software and target (open project)your test assembly where it is located. The test assembly is the class library (or executable) that contains the Test Fixtures. The application will then show you a graphical view of each class. To run the entire suite of tests, simply click the Run button. If you want to only run one Test Fixture or even just a single test, you can double-click it in the tree. After opening the project file, all the methods stated in the DLL file will show up in the left pane, as shown in the picture below:

Figure 1.5 Test Method Loading

Figure 1.5 Test Method Loading

Now hit the Run button on the right side to start the Unit Automotive testing. It will take some time to verify all the conditions given in the test cases. If all the methods function properly as per the test condition, then the GUI will produce the results in the green progress bar, as shown below:

Figure 1.6 NUnit Test Report with No Error

Figure 1.6 NUnit Test Report with No Error

In the scenario above, if the code has some in-built programming glitches, then they are easily rectified during the NUnit Testing process. The following picture depicts a problem in the two thousand() method with a cross sign. Here, the report suggests that all the methods implementations are working properly, except one method.

Figure 1.7 NUnit Test Fail output

Figure 1.7 NUnit Test Fail output

We can go deeper in the error by using the errors and failure tab shown in the bottom right. This will display the exact error in the source code, as seen below:

Figure 1.6 NUnit Detailed Test Report with Error