Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Unit Testing Using NUnit

4.50/5 (10 votes)
6 Dec 2012CPOL4 min read 32.7K  
Unit testing using NUnit.

What is NUnit?

NUnit is a unit-testing framework for all .NET languages. Initially ported from JUnit, the current production release, version 2.6, is the seventh major release of this xUnit based unit testing tool for Microsoft .NET. It is written entirely in C# and has been completely redesigned to take advantage of many .NET language features, for example custom attributes and other reflection related capabilities. NUnit brings xUnit to all .NET languages.

How Does NUnit Work? 

NUnit utilizes attributes to designate the different aspects of a unit test class.

TestFixture

This is the attribute that marks a class that contains tests and, optionally, setup or teardown methods.

SetUp

This attribute is used inside a TestFixture to provide a common set of functions that are performed just before each test method is called.

TearDown

This attribute is used inside a TestFixture to provide a common set of functions that are performed after each test method is run.

Test

The Test attribute is one way of marking a method inside a TestFixture class as a test.

ExpectedException

The ExpectedException attribute is an optional attribute that can be added to a unit test method (designated using the Test attribute). As unit testing should in part verify that the method under test throws the appropriate exceptions, this attribute causes the unit test engine to catch the exception and pass the test if the correct exception is thrown.

Ignore

The Ignore attribute is an attribute to not run a test or test fixture for a period of time. The person marks either a Test or a TestFixture with the Ignore attribute. The running program sees the attribute and does not run the test or tests. The progress bar will turn yellow if a test is not run and the test will be mentioned in the reports that it was not run.

Prerequisites

  • Install Visual Studio 2008 or Visual Studio 2010
  • Download and install NUnit

Steps for using NUnit

Step 1

Open Visual Studio and create a new project, paste the following code inside the class.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace TestNUnit
{
    public class MyTest
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
        public int Substract(int a, int b)
        {
            return a - b;
        }
        public int Devide(int a, int b)
        {
            return a / b;
        }
    }
}

Step 2

Create one more project inside the solution and reference nunit.framework.dll.

Image 1

Step 3

Add a class inside the newly created project to write the test methods. Paste the following code inside the class.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NUnit.Framework;
using TestNUnit;     
 
namespace UnitTest
{
    [TestFixture]
    public class NUnit
    {
        MyTest test=null;
        [SetUp]
        public void Initialize()
        {
            test = new MyTest();
        }
        [TearDown]
        public void CleanUp()
        {
            test = null;
        }
        [Test]
       
        public void Addition()
        {
            Assert.AreEqual(5, test.Add(2, 3));
        }
        [Test]
       
        public void Substraction()
        {
            Assert.AreEqual(0, test.Substract(2, 2));
       
        }
        [Test]
       
        public void Divide()
        {
            Assert.AreEqual(1, test.Devide(2, 2));
        }
    }
} 

Some of the frequently used Assert methods are:

Method  Description 

 Assert.AreEqual  Verifies the two objects are equal.  

 Ex.  
Assert.AreEqual(expected, actual)  

Assert.AreEqual(expected, actual, "The expected and actual are not equal.")    

Assert.AreNotEqual  Verifies that two objects are not equal.  

Ex. 

Assert.AreNotEqual(expected, actual) 

Assert.AreNotEqual(expected, actual, "The expected and actual are equal.")

Assert.IsNull  Verifies the passed object is null. 

Ex. 

Assert.IsNull(actual) 

Assert.IsNull(actual, "The actual is not null.")

Assert.IsNotNull  Verifies the passed object is not null. 

Ex. 

Assert.IsNotNull(actual) 

Assert.IsNotNull(actual, "The actual is null.") 

Assert.IsEmpty  Verifies the passed string is empty. 

Ex. 

Assert.IsEmpty(actual) 

Assert.IsEmpty(actual, "The passed string is not empty.") 

Assert.IsTrue  Verifies the passed condition is true or not.

Ex. 

Assert.IsTrue(actual) 

Assert.IsTrue(actual, "The passed condition is not true.") 

Assert.IsFalse  Verifies the passed condition is false or not. 

  Ex.  

Assert.IsFalse(actual) 

Assert.IsFalse(actual, "The passed string is not false.")  

Assert.IsInstanceOf   Verifies the passed object is of the particular type.  

Ex. 

Assert.IsInstanceOf(typeof(Employee), actual) 

Assert.IsInstanceOf(typeof(Employee), actual, "The object is of not type Employee.") 

 

Step 4 

Compile the project and open NUnit.exe from the Start menu.

Image 2

Step 5

From the File menu, click on Open project, locate the .dll file, and load into NUnit.

Image 3

Step 6

The following screen loads all test methods.

Image 4

Step 7

Click on Run, if the tests pass, then the green progress bar will be displayed.

Image 5

If the test fails the following screen will be displayed in the red progress bar.

Image 6

Adding Ignore Attribute

Let’s add the Ignore attribute to the following test method.

C#
[Test]
[Ignore("Ignore a test")]
public void Substraction()
{
    Assert.AreEqual(0, test.Substract(2, 2));
}

The NUnit tool ignores the above test method and shows in the yellow progress bar.

Image 7

Adding Exception Attribute to Test Method

Let’s add the Exception attribute to the following test method.

C#
[Test]
[ExpectedException(typeof(DivideByZeroException))]
public void Divide()
{
    Assert.AreEqual(1, test.Devide(2, 0));
}

Above ExpectedException attribute will catch the appropriate exceptions thrown by the test method and passes the test.

Image 8

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)