Home → Resources → Articles

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

(Microsoft .NET,Microsoft ASP.NET,Microsoft Visual Studio) by Jason Skowronek on 02/03/2011

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).

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 backwards compatible runtime assemblies from the web.config or you will get 500 errors. Best to add them to your web.config transforms if necessary.
<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>

Example Test Class

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

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

 

Recommend

Nero Sale 150x150 

 

System Mechanic 

SkoNet provides comprehensive digital consulting services such as: web development, applications development, database design and architecture, business process management, customer relationship management, and many others that help businesses of every size, industry, and geography meet the complex challenge of managing and sharing information on the web. Our skills and expertise in online systems allow us to help customers build applications ranging from simple, single-page web sites to robust enterprise systems.

Online Backup, Ektron Consulting, Ektron Programmer, Ektron Developer, Ektron Partner Utah, Ektron Partner, Ektron Architect, Ektron Hosting, Salesforce.com Consultant Utah, Salesforce.com Partner Utah, Salesforce.com Partner, Salesforce.com Programmer, Salesforce.com Architect, Salesforce.com APEX Programmer