Title: Auto-Launch Visual Studio Web Server when running tests
Slug: auto-launch-visual-studio-web-server-when-running-tests
Date: 2008-09-19 11:05:00
Author: Kartones
Lang: en
Tags: Development, Visual Studio, .NET, ASP.NET, Windows, Testing
Description: A guide on auto-launching Visual Studio web server for unit tests using .NET's process handling features and NUnit.

 <p>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.</p> <p>So... digging a bit around the net I found that the integrated web server can be launched stand-alone too, like this:</p> <p><font face="Courier New">C:\Program Files\Common Files\microsoft shared\DevServer\9.0\Webdev.WebServer.exe</font> <font face="Courier New">/port:3491 /path:D:\WebSites\MyExampleWebService</font></p> <p>That would launch my WS and keep it running until I stop the server.</p> <p>Now, we take .NET's windows process handling features, a NUnit test battery template, and we get something like this:</p> <p><font face="Courier New"><font color="blue">using </font><font color="black">System</font></font><font face="Courier New"><font color="blue">;<br>using </font><font color="black">System.Diagnostics</font></font><font face="Courier New"><font color="blue">;<br>using </font><font color="black">NUnit.Framework</font></font><font face="Courier New" color="blue">;<br><br>namespace </font><font face="Courier New"><font color="black">TestsComponent001<br>{<br>    [TestFixture]<br>    </font><font color="blue">public class </font></font><font face="Courier New" color="black">TestBattery001<br>    {<br>        Process webServerProcess </font><font face="Courier New" color="blue">= null;<br><br>        </font><font face="Courier New"><font color="black">[TestFixtureSetUp]<br>        </font><font color="blue">public void </font></font><font face="Courier New"><font color="black">TestBatterySetup()<br>        {<br>            webServerProcess </font><font color="blue">= new </font><font color="black">Process()</font></font><font face="Courier New"><font color="blue">;<br><br>            </font><font color="black">webServerProcess.StartInfo.FileName </font></font><font face="Courier New"><font color="blue">= <br>                </font><font color="#808080">@"C:\Program Files\Common Files\microsoft shared\DevServer\9.0\Webdev.WebServer.exe"</font></font><font face="Courier New"><font color="blue">;<br>            </font><font color="black">webServerProcess.StartInfo.Arguments </font><font color="blue">= <br>                </font><font color="#808080">@"/port:3491 /path:D:\WebSites\MyExampleWebService"</font></font><font face="Courier New"><font color="blue">;<br><br>            </font><font color="black">webServerProcess.Start()</font></font><font face="Courier New" color="blue">;<br>        </font><font face="Courier New"><font color="black">}<br><br>        [TestFixtureTearDown]<br>        </font><font color="blue">public void </font></font><font face="Courier New"><font color="black">TestBatteryTeardown()<br>        {<br>            </font><font color="blue">if </font><font color="black">(webServerProcess !</font><font color="blue">= null</font></font><font face="Courier New" color="black">)<br>                webServerProcess.Kill()</font><font face="Courier New" color="blue">;<br>        </font><font face="Courier New"><font color="black">}<br><br>        [SetUp]<br>        </font><font color="blue">public void </font></font><font face="Courier New"><font color="black">TestSetup()<br>        {<br>        }<br><br>        [TearDown]<br>        </font><font color="blue">public void </font></font><font face="Courier New"><font color="black">TestTeardown()<br>        {<br>        }<br><br>        [Test]<br>        </font><font color="blue">public void </font></font><font face="Courier New" color="black">Test_001()<br>        {<br>        }<br>    }<br>}</font></p> <p>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.</p> <p>Easy, functional and quite handy!</p> <p> </p> <p><b>Note</b>: I use the <font face="Courier New">Kill()</font> method because the <font face="Courier New">Close()</font> 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). </p>
