ASP.NET应用程序的unit testing配置

这是我对Asp.Net Web应用程序的第一次测试。 我们有一个由几个模块组成的引擎。 我需要在Engine Module中测试类。 虽然这些条款是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,这是设计师生成的TestMethods之一

/

//  ///A test for BlogManagerBPO Constructor /// // 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"); } 

你得到的exception消息听起来并不像是一个unit testing。 您是否尝试运行Visual Studio Web测试套件?

对于unit testing,您应该只需创建业务逻辑类的实例并在不受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等,则可以最好地确保这一点。

将属性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")] 

或者像马克所说的那样,放弃了
[HostType("ASP.NET")] [AspNetDevelopmentServerHost("C:\xx\xx\Documents\Visual Studio 2010\Projects\xx\xx", "/")] [UrlToTest("http://localhost:xxx/examples.aspx")] [DeploymentItem("examples.dll")]试图在MVC项目中使用它并且它没有通过,直到我摆脱它

只需检查是否已将app.config添加到测试项目中,并且它具有来自web.config(来自WebService项目)的必要设置。