views:

887

answers:

3

I'm using WatiN, NUnit and ReSharper to run my ASP.NET unit tests inside Visual Studio. I'd like (if it's not already running) to start Cassini to run my tests against.

Is this possible? How would I do it?

+1  A: 

The Cassini server is WebDev.WebServer.EXE. There are several blogs that show how to start it manually. Here is one:

http://www.dotnetjunkies.com/WebLog/saravana/archive/2005/06/18/126143.aspx

rp
This link is no longer valid.
Pete Morgan
This one is: http://cassinidev.codeplex.com
Sky Sanders
+3  A: 

Here's some code:

private static void GetDevelopmentServerVPathAndPortFromProjectFile(
    string csprojFileName,
    out string developmentServerVPath,
    out int developmentServerPort)
{
    XPathDocument doc = new XPathDocument(csprojFileName);
    XPathNavigator navigator = doc.CreateNavigator();

    XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
    manager.AddNamespace("msbuild",
        "http://schemas.microsoft.com/developer/msbuild/2003");

    const string xpath = "/msbuild:Project/msbuild:ProjectExtensions/"
                       + "msbuild:VisualStudio/msbuild:FlavorProperties/"
                       + "msbuild:WebProjectProperties";

    XPathNavigator webProjectPropertiesNode =
        navigator.SelectSingleNode(xpath, manager);
    XPathNavigator developmentServerPortNode =
        webProjectPropertiesNode.SelectSingleNode("msbuild:DevelopmentServerPort",
            manager);
    XPathNavigator developmentServerVPathNode =
        webProjectPropertiesNode.SelectSingleNode("msbuild:DevelopmentServerVPath",
            manager);

    developmentServerPort = developmentServerPortNode.ValueAsInt;
    developmentServerVPath = developmentServerVPathNode.Value;
}

private static string GetCommonProgramFilesPath()
{
    string commonProgramFiles =
        Environment.GetEnvironmentVariable("CommonProgramFiles(x86)");
    if (string.IsNullOrEmpty(commonProgramFiles))
    {
        commonProgramFiles =
            Environment.GetEnvironmentVariable("CommonProgramFiles");
    }
    if (string.IsNullOrEmpty(commonProgramFiles))
    {
        commonProgramFiles =
            Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
    }
    return commonProgramFiles;
}

private static Process PrepareCassiniProcess(int developmentServerPort,
                                             string projectPhysicalPath,
                                             string developmentServerVPath)
{
    string commonProgramFiles = GetCommonProgramFilesPath();
    string cassiniPath = Path.Combine(commonProgramFiles,
        @"Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe");
    string cassiniArgs = string.Format(
        CultureInfo.InvariantCulture,
        "/port:{0} /nodirlist /path:\"{1}\" /vpath:\"{2}\"",
        developmentServerPort, projectPhysicalPath, developmentServerVPath);

    Process cassiniProcess = new Process();
    cassiniProcess.StartInfo.FileName = cassiniPath;
    cassiniProcess.StartInfo.Arguments = cassiniArgs;
    return cassiniProcess;
}

To use it, you need to discover the path to the CSPROJ file of the web project under test. I'll leave that as an exercise for the reader (I've currently got it hard-coded).

Roger Lipscombe
+4  A: 

I just released the CassiniDev 3.5.1/4.0.1 beta with a simple test fixture example if you are interested.

Cassini for Developers and Testers: http://cassinidev.codeplex.com

Mo betta, word.

Sky Sanders