Yesterday, I posted a short entry linking to Scott Hanselmans great article on how he used Cassini to unit test web forms without having Internet Information Server installed on the test server. I made a short comment on exchanging Scott’s simple smoke test with a NUnitASP test case to do more advanced testing. Judging from some of the feedback I got, I decided to give a complete example of how you can use Cassini in combination with NUnitASP to unit test ASP.NET web pages without IIS. I’ve used Scott’s original code with some minor changes. In addition, I’m using my updated version of NUnitASP to make it handle setup and teardown correctly.
Below is the complete source code for my test fixture. The resource handling code and similar has been eluded for clarity.
using System;
using System.Diagnostics;
using System.IO;
using Cassini;
using NUnit.Extensions.Asp.AspTester;
using NUnit.Framework;
namespace Noras.Articles.CassiniWithNUnitASP.Tests {
[TestFixture]
public class Example : NUnit.Extensions.Asp.WebFormTestCase {
private Server webServer;
private int webServerPort=8086;
private string webServerVDir="/";
private string tempPath=AppDomain.CurrentDomain.BaseDirectory;
private string tempBinPath=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"bin");
private string webServerUrl;
private string aspxPath=@"C:\Documents and Settings\anders.noras\My Documents\My Projects\Research\CassiniHostedExample\";
[SetUp]
new public void BaseSetUp() {
base.BaseSetUp();
Directory.CreateDirectory(tempBinPath);
foreach(string file in Directory.GetFiles(tempPath,"*.dll")) {
string newFile =
Path.Combine(tempBinPath,Path.GetFileName(file));
if (File.Exists(newFile)){File.Delete(newFile);}
File.Copy(file,newFile);
}
foreach(string file in Directory.GetFiles(aspxPath,"*.aspx")) {
string newFile =
Path.Combine(tempPath,Path.GetFileName(file));
if (File.Exists(newFile)){File.Delete(newFile);}
File.Copy(file,newFile);
}
//Start the internal Web Server
webServer=new Server(webServerPort,webServerVDir,tempPath);
webServerUrl=String.Format("http://localhost:{0}{1}",
webServerPort,webServerVDir);
webServer.Start();
// Let everyone know
Debug.WriteLine(
String.Format("Web Server started on port {0} with VDir {1} in physical directory {2}",webServerPort,webServerVDir,tempPath));
}
[TearDown]
new public void BaseTearDown() {
base.BaseTearDown();
try {
if (webServer!=null) {
webServer.Stop();
webServer=null;
}
Directory.Delete(tempBinPath,true);
} catch {}
}
[Test]
public void SimpleTest() {
Browser.GetPage(webServerUrl+"Default.aspx");
Debug.WriteLine(Browser.CurrentPageText);
LabelTester label1=new LabelTester("Label1",CurrentWebForm);
AssertVisibility(label1,true);
}
}
}
I’ve added a path to the aspx files to be tested to the class (aspxPath) and added code to copy the aspx files from this location to the physical Cassini web directory. Apart from that I have the SimpleTest test case which is similar to the case in my previous post.
Other files such as the Default.aspx page is similar to the one in Scott’s example.
As this example shows you can combine Scott’s clever Cassini test fixture with the power of NUnitASP. The solution should be perfect for unit testing within continuous integration environments and similar.