Using NUnit with Visual Studio 2012 Unit Test Projects

Introduction

In Visual Studio 2012, the Unit Test projects by default use MS-Test, Microsoft’s unit testing framework. A nice feature of Visual Studio 2012 is that it also allows you to use third-party unit testing frameworks such as NUnit, xUnit.Net and MbUnit. This allows you to write unit tests using the unit testing framework of your choice and still use Visual Studio IDE to run the tests. This article shows you how NUnit can be used in the Visual Studio unit test projects.

Sample Class Library – EmployeeLib

To illustrate how to use NUnit in Visual Studio 2012 you will first develop a simple class library project against which the unit tests are to be written. So, begin by creating a new class library project in Visual Studio 2012. The following code shows EmployeeHelper class from this class library.

namespace EmployeeLib
{
    public class EmployeeHelper
    {
        public Employee GetEmployeeByID(int empId)
        {
            NorthwindEntities db=new NorthwindEntities();
            var data = from item in db.Employees
                       where item.EmployeeID == empId
                       select item;
            return data.SingleOrDefault();
        }
    }
}

 

The EmployeeHelper class contains just one method, named GetEmployeeByID(). The GetEmployeeByID() accepts an employee ID and returns an Employee object to the caller. Notice the use of SingleOrDefault() method that returns a valid Employee object if an employee is found, or returns null. The project uses the Employee table from the Northwind database. The Entity Framework data model for the Employees table is shown below:

Entity Framework data model for the Employees

Entity Framework data model for the Employees

Compile the class library so that the corresponding assembly is outputted.

Creating a Unit Test Project in Visual Studio 2012

Now that you havecreated the class library, the next step is to create a Unit Test project in Visual Studio 2012. Add a Unit Test project to the existing solution using the Add New Project dialog.

Add new project

Add new project

Now you need to install NUnit Test Adapter using Visual Studio 2012. The NUnit test adapter essentially acts as a bridge between Visual Studio and NUnit framework so that NUnit framework can be used from within Visual Studio IDE. To install NUnit Test Adapter, click on the Tools menu and then select Extensions and Updates menu option.

Select Extensions and Updates

Select Extensions and Updates

This will open a dialog as shown below. Select “Online” as the source of updates and then search for “NUnit Test Adapter”.

Extensions and Updates

Extensions and Updates

Clicking on the Download button will download and install the necessary components on your machine. Remember that post installation you need to restart Visual Studio so that the update becomes functional. A notification at the bottom of the same dialog after successful installation will tell you this:

Restart Microsoft Visual Studio notification

Restart Microsoft Visual Studio notification

Using NUnit from within the Unit Test Project

Next, you need to install the NUnit framework in your unit test project. To do so, be inside the Unit Test project you just created and select the “Manage NuGet Packages” menu option from the Project menu (see below).

Manage NuGet Packages

Manage NuGet Packages

Once the Manage NuGet Packages dialog opens search for NUnit.

Search for NUnit

Search for NUnit

Now click on the Install button to install the NUnit package in the unit test project. You are now ready to write and run NUnit tests from within Visual Studio IDE. As an example let’s write a simple unit test using NUnit that tests the GetEmployeeByID() method you created earlier.

namespace UnitTestsDemo
{
    [TestFixture]
    public class UnitTest1
    {
        [Test]
        public void TestGetEmployeeByID()
        {
            EmployeeLib.EmployeeHelper helper = new EmployeeLib.EmployeeHelper();
            EmployeeLib.Employee emp = helper.GetEmployeeByID(100);
            Assert.IsNotNull(emp);
        }
    }
}

The unit test shown above calls the GetEmployeeByID() by passing some EmployeeID. An Assert.IsNotNull() will return true if a valid Employee object is returned, otherwise it will return false. Notice that the class UnitTest1 is decorated with the [TestFixture] attribute and the TestGetEmployeeByID() method is decorated with the [Test] attribute. These attributes are provided by the NUnit framework. Make sure you have imported NUnit.Framework as follows:

using NUnit.Framework;

Running the Unit Tests

Now, let’s run the test method TestGetEmployeeByID() using Visual Studio 2012 Test Explorer. The following figure shows the Test Explorer listing tests from a given unit testing project. You can access the Test Explorer from the Test > Windows menu option.

Test Explorer

Test Explorer

Right click on the TestGetEmployeeByID and select the Run selected tests menu option. This will run the test and the Test Explorer will also show you the results of the test. For example, in the above example since there is no employee with ID 100, the assertion fails and the Test Explorer reports it as shown below:

Test Explorer reports

Test Explorer reports

Summary

Visual Studio 2012 allows you to use third-party unit testing frameworks such as NUnit, xUnit.Net and MbUnit. This way you can write unit tests using the unit testing framework of your choice and still use Visual Studio IDE to run the tests. This article illustrated this feature using NUnit as the unit testing framework.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read