3

这是我对 Asp.Net Web 应用程序的第一次测试。我们有一个由几个模块组成的引擎。我需要在引擎模块中测试类。尽管这些类是 Asp.Net App 的一部分,但它们仅包含业务逻辑。

作为 WebApp 的一部分,我如何单独测试这些类?因为我收到了这个错误

Web 请求“ http://localhost:8936/ ”成功完成,但未运行测试。这可能发生在配置用于测试的 Web 应用程序失败(处理请求时发生 ASP.NET 服务器错误)或没有执行 ASP.NET 页面时(URL 可能指向 HTML 页面、Web 服务或目录列表)。在 ASP.NET 中运行测试需要将 URL 解析为 ASP.NET 页面,并且页面才能正确执行到 Load 事件。请求的响应与测试结果一起存储在文件“WebRequestResponse_BlogManagerBPOConstr.html”中;通常可以使用 Web 浏览器打开此文件以查看其内容。

谢谢

编辑: @Mark,这是设计师生成的测试方法之一

/

// <summary>
        ///A test for BlogManagerBPO Constructor
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("D:\\WorkingCopies\\MyProject\\Engine", "/")]
        [UrlToTest("http://localhost:8936/")]
        public void BlogManagerBPOConstructorTest()
        {
            BlogManagerBPO target = new BlogManagerBPO();
            Assert.Inconclusive("TODO: Implement code to verify target");
        }
4

4 回答 4

5

您收到的异常消息听起来根本不像是单元测试。您是否尝试运行 Visual Studio Web 测试套件?

对于单元测试,您应该能够简单地创建业务逻辑类的实例并对其进行测试,而不受 ASP.NET 运行时的干扰。

在 MsTest 中,可能如下所示:

[TestMethod]
public void Test5()
{
    var sut = new Thing();
    var expectedResult = new object();
    sut.Bar = expectedResult;
    var actual = sut.Bar;
    Assert.AreEqual(expectedResult, actual);
}

(虽然可能不是最令人兴奋的测试......)

在任何地方都找不到 ASP.NET 细节。

如果您将业务逻辑放在单独的库中并确保它不引用 System.Web 等,则可以最好地确保这一点。

于 2010-01-12T20:40:58.623 回答
2

将属性 UrlToTest 设置为 Url In 测试。像:

[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\xx\\xx\\Documents\\Visual Studio 2010\\Projects\\xx\\xx", "/")]
[UrlToTest("http://localhost:xxx/examples.aspx")]
[DeploymentItem("examples.dll")]
于 2011-12-07T09:43:38.680 回答
1

或者就像 Mark 说的,Get Rid of the
[HostType("ASP.NET")] [AspNetDevelopmentServerHost("C:\xx\xx\Documents\Visual Studio 2010\Projects\xx\xx", "/")] [UrlToTest("http://localhost:xxx/examples.aspx")] [DeploymentItem("examples.dll")] 试图在 MVC 项目中使用它,直到我摆脱它才通过

于 2012-01-24T09:21:47.467 回答
0

Just check if you have added app.config to your test project and it has necessary settings from your web.config (from the WebService Project).

于 2017-07-20T09:34:05.160 回答