3

我正在使用 WatiN、NUnit 和 ReSharper 在 Visual Studio 中运行我的 ASP.NET 单元测试。我想(如果它还没有运行)启动 Cassini 来运行我的测试。

这可能吗?我该怎么做?

4

3 回答 3

8

如果您有兴趣,我刚刚发布了 CassiniDev 3.5.1/4.0.1 测试版,其中包含一个简单的测试夹具示例。

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

Mo betta, word.

于 2010-01-04T00:50:04.467 回答
2

这是一些代码:

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;
}

要使用它,您需要发现被测 Web 项目的 CSPROJ 文件的路径。我将把它作为练习留给读者(我目前已经硬编码)。

于 2008-11-30T07:55:21.763 回答
1

Cassini 服务器是 WebDev.WebServer.EXE。有几个博客展示了如何手动启动它。这是一个:

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

于 2008-11-30T07:04:08.373 回答