WatiN based unit tests using the ASP.NET Development web server
One of the more interesting and less documented features of VSTS is how to handle the ever changing port of your local development server when running unit tests. This really becomes an issue when you want to write WatiN tests that can run out of the box on any developers machine or a server that is performing continuous integration.
As part of this week’s Teched, Mitch Denny has organized a Smith Family community project to develop an application for the smith family. My contribution was to port WatiN to .net 2.0 (again), so it didn’t have my recent enhancements.
The next step was to write a test that used WatiN, but would work with the TFS continuous integration tool Chris Burrows, without a hardcoded test server. Whilst not the exact code, Chris showed me how to utilise the AspNetDevelopmentServer attribute from the Microsoft.VisualStudio.TestTools.UnitTesting.Web namespace to work with the local ASP.NET Development web server. I have written up a short example below that shows how it is done.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using WatiN.Core;[TestClass]
public class UnitTest1
{
private TestContext m_testContext;
public TestContext TestContext
{
get { return m_testContext; }
set { m_testContext = value; }
}public string DevServerURL
{
get
{
Uri webServer = m_testContext.Properties["AspNetDevelopmentServer.localhost"] as Uri;
return webServer.AbsoluteUri;
}
}[TestMethod]
[AspNetDevelopmentServer("localhost", @"C:\Docu ... \WebSite1")]
public void TestMethod1()
{
using (IE ie = new IE(DevServerURL))
{
Assert.IsTrue(ie.MainDocument.ContainsText(”This is a test.”));
}
}
}
To make this work you will need to add a COM reference to Microsoft Internet Controls, and a reference to your Watin.Core.dll. To our complete surprise, once we had defined the initial test as part of the team build, it ran first go on the server, sight unseen and passed.
August 25th, 2006 at 5:48 pm
Dear Mr. McLeod,
How do you run the web project and the test project at the same time under a single solution? Or I misunderstood your post? I am not using VSTS.
Best regards,
Jesús G
August 25th, 2006 at 6:06 pm
Sorry, I just saw the multiple startup projects option.
I guess there is no way to get the dynamic path to the local ASP.NET Development web server from the test project. The option would be to use the server as an external program with a fixed port.
September 8th, 2006 at 2:00 pm
Jesus,
The method I described uses the testing features of VSTS, specifically the test context to start up the web project from the VSTS test.
When the test is run, it starts the cassini web server, then runs the test.
This example will only work with the Visual Studio Edition for Software Testers, or the full Team Suite edition. You don’t need the back end server though.
To get a similar approach with professional edition, the easiest way would be to host in IIS instead of cassini.
Hope that helps
Bruce