Unit Testing and Test Automation in VS2012 Part 1: Running NUnit natively in Visual Studio

One of the most criticized features of Visual Studio ever since it came out has been the ability, or lack and inadequacy thereof, of the IDE to integrate third-party unit testing libraries into it. MSTest was not only too late but was also too lame -- it wanted users to rely on automatically generated tests put up after code has already been written. This runs so counter to the tenets of Test Driven Development that MSTest was simply ignored, and many developers came up with ways to hack third party unit-testing frameworks like NUnit, MBUnit, xUnit and so on for their .NET projects. Inevitably, Microsoft and MSTest ended up being vilified by the believers of TDD.

With Visual Studio 2012 Microsoft provided a means of using frameworks like NUnit and Selenium to actually be run within its IDE, in a tightly integrated way, and in turn, the ability to code test-first.

Let's talk about how to make that happen.

Setting up the NUnit Test Adapter

First in our agenda is to wire Visual Studio 2012's testing framework to NUnit. To be able to do this we need to download and install the NUnit Test Adapter extension, which we will do via Visual Studio's "Extension Managers" feature:

  1. With Visual Studio 2012 started, go to the Tools Menu and click on Extensions and Updates
  2. On the left tab click on "Online" and on the Search field on the upper right type in "NUnit Test Adapter". You will need an internet connection for this to work.
  3. When the NUnit Test Adapter item appears click the "Download" button. Once the download is complete follow the instructions to install the NUnit Test Adapter
  4. At the bottom of the window you may be asked to click "Restart Now" to allow the newly installed components to take effect within Visual Studio 2012.

Once you've finished this Visual Studio 2012 is now ready to run NUnit tests within the IDE.

Creating your first NUnit test project

Let's now create our first NUnit test project.

  1. Go to File -> New -> Project and under Visual C# choose "Class Library" and rename to "NUnitTestDemo". You may also want to rename your "Class1.cs" into something more sensible.
  2. In the Solution Explorer right-click on references then click "Manage NuGet Packages".
  3. On the left tab click on "Online" and then on the Search field type "NUnit" this time. When the result appears click "Install".

All the relevant classes required for NUnit should now be included in the project -- it's now time to write some unit tests!

Our bare code so far looks like this (I've renamed the project "NunitTestDemo" and the default class "FirstUnitTest"):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NunitTestDemo
{
    public class FirstUnitTest
    {
    }
}

To check if NUnit works on Visual Studio let's try adding bits of code and one dummy test:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace NunitTestDemo
{
    [TestFixture]
    public class FirstUnitTest
    {
        [Test]
        public void TrueTest()
        {
            Assert.IsTrue(true);
        }
    }
}

To run this test, click on the Test menu, Run, then All Tests. You could also use the shortcut Ctrl-R, then A.

The test result under the new "Test Explorer" tab will look like this:

Details like the name of the tests and execution times of both individual and all tests appear in this tab.

Let's add a failing test just to see what it looks like:

        [Test]
        public void MathTest()
        {
            Assert.AreEqual(0, 1 + 1);
        }

Running the tests again, the result would look like this:

Additional details come up -- highlighting the failed test reveals the expectations of the failing test, the actual result, and even the stack trace, crucial for figuring out why the test is failing.

You're now ready to use NUnit to code test-first for your classes.

But you could also use it for your front-end code, particularly web applications. On the next installment of this 2-part series I'll show you how to use Selenium for front-end testing in Visual Studio.

References

Peter Provosts's presentation on Visual Studio 2012 unit testing features in TechEd Europe was a crucial reference for this post.

About Jon Limjap

Jon Limjap has been programming since he was 12 and hasn't stopped yet. He was gone for a while in iOS and Java land, but is now back in .NET searching for unicorns and hunting down dragons.
This entry was posted in Technical Articles and tagged , , , , , . Bookmark the permalink.

5 Responses to Unit Testing and Test Automation in VS2012 Part 1: Running NUnit natively in Visual Studio

  1. Jarda says:

    Thanks! Great stuff.

  2. Preeti Valand says:

    Thanks, this got me up and running on VS12. Looking to move all the Selenium IDE tests to VS12 now.

  3. Phong Ly says:

    Thanks for the instruction, I followed all the step but the NUnit test doesn’t run. I then installed ReSharper and I could run the FirstUnitTest and SecondUnitTest.cs.

    But then, when I uninstalled ReSharper, same problem, I’ve tried several ways to run the test but there is no result in Test Explorer windows, only exclamation mark in front of each test case.

    I am not sure what’s wrong, could you shed some light on this? Thx

  4. Jon Limjap says:

    Hi Phong,

    I’m not sure it was a good idea to uninstall Resharper, technically you can start unit tests from it, and I occasionally use that instead of the built-in test runner.

    I actually don’t know what went wrong, but you can try uninstalling and reinstalling the NUnit Test Adapter. It’s been a year since this posting, and the Beta version might not be appropriate anymore, so make sure you get the latest one. Also don’t miss the step where you actually reference the NUnit Nuget package from inside the project.

    Hope this helps,
    Jon

  5. lalitha says:

    i followed the procedure but i am unable to run any test, can you please help me how to run these tests only from IDE.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>