DotNetCurry Logo

ASP.NET MVC Unit Testing using NUnit in Visual Studio

Posted by: Suprotim Agarwal , on 2/12/2014, in Category ASP.NET MVC
Views: 77878
Abstract: Set up NUnit in ASP.NET MVC and use the integration points in Visual Studio to integrate NUnit Test Cases

Visual Studio 2012 comes with a perfectly capable Unit Testing system in MS Test. However if your team’s skills require you to use alternate Testing frameworks like NUnit, Visual Studio is game to play along. In this article, we’ll see the how we can setup NUnit to work with Visual Studio’s Test Explorer and run Unit tests in our project.

Setting up NUnit in Visual Studio 2012

There are two parts to NUnit setup for use in Visual Studio. First part is to install the framework, we can do this in two ways.

1. Download the 2.6.2 (the latest is 2.6.3 at the time of writing) msi installer from http://www.nunit.org. Ensure Visual Studio is not running while the installer is running. This installs NUnit globally along with the NUnit test runner. However the test runner needs .NET 3.5 to run.

2. Another way to include NUnit in your project is to download it using the following Nuget Package Manager command

PM> install-package NUnit

Second part is to setup the NUnit Test Adapter so that Visual Studio recognizes the Test Cases in our project and allows us to Run them from the Test Explorer.

 

 

Setting up the NUnit Test Adapter

1. To Setup the adapter, go to Visual Studio Tools->Extensions and Updates.

2. Select, Online in the Left hand pane Search for NUnit and Install the NUnit Test Adapter.

3. It needs a Visual Studio Restart

With that we are ready to write Test Cases using NUnit. So let’s write a sample Test Case.

Writing Test Cases for ASP.NET MVC Apps

Let’s create a new MVC4 project. The NUnit Test Adapter doesn’t register itself to be used with the ‘New Project Template’ dialog hence while creating the project we can’t select NUnit. So we’ll not create a Test Project here. As shown below, if we select ‘Create a unit Test project’ NUnit is not registered as a Test Framework so we’ll uncheck the ‘Create a unit Test Project’ and continue.

nunit-not-registered

Once the MVC Project has been created we add a simple Class Library project to the Solution and append the word ‘Test’ at the end to indicate it’s a Test project.

Next we add references to the Project using the Nuget Package Manager.

PM> install-package NUnit

Make sure you select the Class library in the Package Manager console.

Adding your first Test case

1. Rename the default Class1.cs to HomeControllerTest.

2. Next add reference to System.Web.Mvc in the Test Project

3. Add a new Test case to test if view return has the name “Home”.

[TestFixture]
public class HomeControllerTest
{
    [Test]
    public void IndexActionReturnsIndexView()
    {
        string expected = "Index";
        HomeController controller = new HomeController();

        var result = controller.Index() as ViewResult;

        Assert.AreEqual(expected, result.ViewName);
    }
}

4. Build the project

5. Open Test Explorer from the ‘Test’ Menu.

6. The Test Explorer will scan all the projects for Test cases, you’ll see a green progress bar spinning on top. Once it completes it will show the Test Case you just created.

test-explorer

7. Run the Test case. It will fail. This is because the View name is not set and thus empty.

test-case-fail

8. Go to the HomeController’s Index method and change the return statement to explicitly specify the ViewName.

public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View("Index");
}

9. Build and Run the Test again. This time the test will succeed.

test-case-pass

Conclusion

We saw how to setup NUnit and use the integration points in Visual Studio to integrate the Test case execution of an ASP.NET MVC app with Visual Studio’s tooling.
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+
Further Reading - Articles You May Like!
Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Dave Hanna on Wednesday, February 12, 2014 11:05 AM
Can you tell us how to register NUnit so that it WILL show up in the choices for "Create a Unit Test Project"?  Also, how to do it in VS 2013, which doesn't seem to have the Test Framework option on "Create a Test Project"?
Comment posted by Suprotim Agarwal on Saturday, February 15, 2014 10:48 PM
@Dave: I do not have VS 2013 on this machine, so I can't test it out at the moment. But just wanted to be sure you aren't using VS 2013 'Express' edition as they don't support extensions/plugins
Comment posted by SUDHEER on Tuesday, September 23, 2014 12:43 PM
thanks for pointing out NUnit Test Adapter
Comment posted by ngo dac kien on Tuesday, February 3, 2015 8:03 PM
thank you.i had knew very about code test.