Click here to Skip to main content
Licence CPOL
First Posted 3 Oct 2011
Views 26,919
Bookmarked 12 times

How to Configure ASP.NET Unit Tests in Visual Studio 2010

By | 3 Oct 2011 | Article
Unit Test in ASP.NET

Introduction

This article demonstrates how to setup a basic unit test project to take care of code coverage of a standard ASP.NET website.

This article assumes you have created an ASP.NET Web Application project (not ASP.NET Website).

Using the Code

Setup a Unit Test Project

  1. Open your solution in Visual Studio 2010
  2. Click Test | New Test
  3. Select Basic Unit Test (or any of the others)
  4. Name your test class
  5. Add to or create a new test project (preferably ProjectName.Tests)

Couple of caveats

  1. Only works for ASP.NET 4.0 Web Applications
  2. You will need to remove any backward compatible runtime assemblies from the web.config or you will get 500 errors. Best to add them to your web.config transforms if necessary.
// Language:c#
// web.config file
//
<runtime xdt:Transform="Replace">
    <assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Extensions" 
		publicKeyToken="31bf3856ad364e35"/>
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Extensions.Design" 
	publicKeyToken="31bf3856ad364e35"/>
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
        </dependentAssembly>
    </assemblyBinding>
</runtime>

Points of Interest

Example Test Class

This is a very basic test class and unit test method for example.

Language:c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
 
namespace Project.Tests
{
    [TestClass]
    public class BusinessTests
    {
        [TestMethod]
        [UrlToTest("http://localhost:8080/Default.aspx")]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("$(SolutionDir)\\Project.Web")]
        public void TestMethod1()
        {
            string url = HttpContext.Current.Request.Url.ToString();
        }
    }
}

A couple of things to note:

  1. UrlToTest: References the Visual Studio Development Server. IMHO, best to use a specific port (i.e. 8080) when using tests.
  2. AspNetDevelopmentServerHost: Microsoft recommends using the environment variable: %PathToWebRoot%. However, this can prove to be problematic when working in team environments. So in this example, we take advantage of the Visual Studio macro variable, $(SolutionDir) to fill the path to the solution regardless of the build environment.

Debugging ASP.NET Unit Tests

To debug your tests, you will need to attach to the worker process for your web application (either WebDev.WebServer... or w3svc), and NOT to the test project.

Once attached to the web debugger, you can then select the test method and click CTRL + R + T to run the selected test, or CTRL + R + A to run all tests.

Code, out!

References

License

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

About the Author

Sunitha Sudheesh

Software Developer (Senior)
KeralaInfotech
India India

Member

I love to remain updated in technology. In spare times I explore with google, find new projects and solutions. My favourites is Asp.net, SQL server , javascript, WPF and Silverlight. I started my software job as a VB6 programmer.
 
My family includes husband(my great support),our baby son.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThis is a Stolen Article Pinmemberhipreetham19:42 6 Mar '12  
GeneralMy vote of 2 Pinmembersujit07611:59 27 Jan '12  
Questionvery basic PinmemberSunitha Sudheesh22:32 11 Oct '11  
Question[My vote of 1] poor explanation PinmemberMohammad_Denmark21:31 11 Oct '11  
Questionnot an article Pinmemberkiran dangar19:28 3 Oct '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 3 Oct 2011
Article Copyright 2011 by Sunitha Sudheesh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid