For security policies, in my job I don't have installed IIS, so I have to develop in ASP.NET by using Visual Studio 2008's (great) web server. But the problem is that whenever I want to make unit tests, I have to first launch the web service via Visual Studio... which is tedious and not very automated.
So... digging a bit around the net I found that the integrated web server can be launched stand-alone too, like this:
C:\Program Files\Common Files\microsoft shared\DevServer\9.0\Webdev.WebServer.exe /port:3491 /path:D:\WebSites\MyExampleWebService
That would launch my WS and keep it running until I stop the server.
Now, we take .NET's windows process handling features, a NUnit test battery template, and we get something like this:
using System;
using System.Diagnostics;
using NUnit.Framework;
namespace TestsComponent001
{
[TestFixture]
public class TestBattery001
{
Process webServerProcess = null;
[TestFixtureSetUp]
public void TestBatterySetup()
{
webServerProcess = new Process();
webServerProcess.StartInfo.FileName =
@"C:\Program Files\Common Files\microsoft shared\DevServer\9.0\Webdev.WebServer.exe";
webServerProcess.StartInfo.Arguments =
@"/port:3491 /path:D:\WebSites\MyExampleWebService";
webServerProcess.Start();
}
[TestFixtureTearDown]
public void TestBatteryTeardown()
{
if (webServerProcess != null)
webServerProcess.Kill();
}
[SetUp]
public void TestSetup()
{
}
[TearDown]
public void TestTeardown()
{
}
[Test]
public void Test_001()
{
}
}
}
When this test battery is launched, NUnit will start the web server with our desired web service, run all the tests and then kill the process to stop it.
Easy, functional and quite handy!
Note: I use the Kill() method because the Close() one was not always closing the process quickly enough to allow multiple test batteries to be launched in sucession using the same web server and port (and the server crashes if it's already running in a specific port).
Tags: Development