ASP.Net Website Tests using Web Dev Server


One of the great features that came with VS2005 was the Web Dev Server which allows us to run and debug web applications on our development box without the need to install IIS. As with most cool gadgets used by Visual Studio the web dev server is actually also a standalone component that we can make use of outside of the IDE.

The web dev server is actually a single executable which we pass some parameters into to startup a new web application that we can access locally. On most machines you will find it at ‘C:\Program Files\Common Files\microsoft shared\DevServer\9.0\WebDev.WebServer.EXE’.

There are plenty of posts around on how to call this from the command line so we are going to jump to how you can make use of this tool to create simple and robust tests using the test capabilities of Visual Studio, I’m using VS2008 for the demo.

A Simple Website

So first we need a simple website to test, I’m just going to have a single page (Default.aspx) that will display the contents of the url parameter “ToDisplay” in a label. I.e. if we accessed Default.aspx?ToDisplay=Rowan then we would end up with the text “Rowan” displayed on the page.

Create your web application: ‘File->New Project->Web->ASP.Net Web Application’
I’ve called mine SimpleWebsiteToTest

Open Default.aspx and drag on a label from the toolbox
Label1 seems like as good a name as any.

‘Right click -> View Code’ and add the following line to the Page_Load method;

this.Label1.Text = Request["ToDisplay"];

You can fire up your website now and test it out.

A Simple Test

I’m just going to create a single test that will hit the url “Default.aspx?ToDisplay=Hello World” and checks that the response contains the text “Hello World”.

First we need to add a test project: ‘File -> Add -> New Project -> Test -> Test Project’
I’ve called mine SimpleTests

If you are using VS2008 then it has probably created a ManualTest1.mht which you should delete now.

I’m going to copy the WebDev.WebServer.EXE into my test project so that my tests don’t rely on the dev machine having the executable in a particular directory.
‘Project -> Add Existing Item’ and navigate to the directory where WebDev.WebServer.EXE lives, most likely ‘C:\Program Files\Common Files\microsoft shared\DevServer\9.0\’.

Now lets open up UnitTest1.cs and locate TestMethod1(), I’m using the following code;

public void TestMethod1()
    //Find the base directory of our solution
    string solutionDirectory = AppDomain.CurrentDomain.BaseDirectory;
    solutionDirectory = solutionDirectory.Substring(0, solutionDirectory.LastIndexOf("TestResults"));

    //Parameters for our Web Dev Server
    string websiteSubDirectory = @"SimpleWebsiteToTest\";
    string testSubDirectory = @"SimpleTests\";
    int port = 60000;

    //Launch our Web Dev Server
    System.Diagnostics.ProcessStartInfo webDev = new System.Diagnostics.ProcessStartInfo();
    webDev.FileName = string.Format("{0}{1}WebDev.WebServer.EXE", solutionDirectory, testSubDirectory);
    webDev.Arguments = string.Format("/port:{0} /path:\"{1}{2}", port, solutionDirectory, websiteSubDirectory);
    System.Diagnostics.Process webServ = System.Diagnostics.Process.Start(webDev);

    //Run our request
    string url = string.Format("http://localhost:{0}/Default.aspx?ToDisplay=Hello World", port);
    System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
    System.Net.WebResponse webResponse = webRequest.GetResponse();
    System.IO.StreamReader rd = new System.IO.StreamReader(webResponse.GetResponseStream());
    string responseText = rd.ReadToEnd();

    //Check that the response contains the correct text
    Assert.IsTrue(responseText.Contains("Hello World"), "Page did not contain Hello World");

    //Close off our dev server

Now if you run your test ‘Test -> Run -> All Tests In Solution’ it should succeed, if you want to debug through your test then set a breakpoint and select ‘Test -> Debug -> All Tests In Solution’.


In this post we saw how to use the Web Dev Server, shipped with VS2005 onwards, to create simple, self contained web site tests that do not rely on IIS or any other external components.

This is a very simple example and you should consider re-using the same web dev server instance between tests and adding some error handling to make sure dev servers aren’t left hanging around if you test errors out before finishing.