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.



Related Articles

Downloads

Comments

  • Cool

    Posted by Mojster on 07/28/2014 05:48am

    Simple and very useful, thank you!

    Reply
  • Good article

    Posted by Prabodha on 01/07/2014 12:59am

    Very useful article. simple and straightforward. Thanks a lot...

    Reply
  • Mr

    Posted by Jonny on 06/30/2013 12:46am

    This was very helpful, thanks very much!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds