C# 对 ASP.NET 2.0 网页进行单元测试的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to do unit testing for ASP.NET 2.0 web pages?
提问by Bryan Denny
Any suggestions? Using visual studio in C#.
有什么建议?在 C# 中使用 Visual Studio。
Are there any specific tools to use or methods to approach this?
是否有任何特定的工具可以使用或方法来解决这个问题?
Update:
更新:
Sorry, I should have been a little more specific. I am using ASP.Net 2.0 and was looking more for a tool like jUnit for Java. I took a look at NUnit and NUnitAsp and that looks very promising. And I didn't even know that Visual Studio Pro has a testing suite, so I'll look at all of these options (I've just started using Visual Studio/Asp.net/C# this summer).
对不起,我应该更具体一点。我正在使用 ASP.Net 2.0,并且正在寻找更多类似 jUnit for Java 的工具。我看了一下 NUnit 和 NUnitAsp,这看起来很有希望。而且我什至不知道 Visual Studio Pro 有测试套件,所以我将查看所有这些选项(今年夏天我刚刚开始使用 Visual Studio/Asp.net/C#)。
采纳答案by Brad Tutterow
Boy, that's a pretty general question. I'll do my best, but be prepared to see me miss by a mile.
男孩,这是一个很笼统的问题。我会尽力而为,但要准备好看到我错过一英里。
Assumptions
假设
- You are using ASP.NET, not plain ASP
- You don't really want to test your web pages, but the logicbehind them. Unit testing the actual .ASPX pages is rather painful, but there are frameworks out there to do it. NUnitAspis one.
- 您使用的是 ASP.NET,而不是普通的 ASP
- 您并不是真的想测试您的网页,而是测试它们背后的逻辑。对实际的 .ASPX 页面进行单元测试是相当痛苦的,但有一些框架可以做到这一点。 NUnitAsp就是其中之一。
The first thing to do is to organize (or plan) your code so that it can be tested. The two most popular design patterns for this at the time seem to be MVP and MVC. Both separate the logicof the application away from the viewso that you can test the logic without the view (web pages) getting in your way.
首先要做的是组织(或计划)您的代码,以便可以对其进行测试。当时最流行的两种设计模式似乎是 MVP 和 MVC。两者都将应用程序的逻辑与视图分开,以便您可以在没有视图(网页)妨碍您的情况下测试逻辑。
Either MVP or MVC will be effective. MVC has the advantage of having a Microsoft framework almost ready to go.
MVP 或 MVC 都会有效。MVC 的优势在于几乎可以使用 Microsoft 框架。
Once you've selected a framework pattern that encourages testability, you need to use a unit testing tool. NUnitis a good starting point. Visual Studio Professional has a testing suite built it, but NUnit + TestDrive.NETalso works in the IDE.
一旦您选择了鼓励可测试性的框架模式,您就需要使用单元测试工具。 NUnit是一个很好的起点。Visual Studio Professional 有一个内置的测试套件,但 NUnit + TestDrive.NET也可以在 IDE 中运行。
That's sort of a shotgun blast of information. I hope some if it hits. The Pragmatic Bookshelf has a good book covering the topic.
这有点像散弹枪般的信息。我希望一些如果它击中。实用书架有一本很好的书,涵盖了这个主题。
回答by Chris Farmer
There was a screencast series a year or so ago on Polymorphic Podcast that did a pretty good intro walkthrough of an MVP implementation in ASP.NET. Implemented this way, unit tests fall into place much more naturally.
大约一年前,Polymorphic Podcast 上有一个截屏系列,它对 ASP.NET 中的 MVP 实现做了很好的介绍演练。以这种方式实现,单元测试会更自然地就位。
回答by Frog
NUnitis the way to go for you. You could find some links here :
NUnit是您的最佳选择。你可以在这里找到一些链接:
回答by Joshua Turner
Take a look at http://selenium.openqa.org/it offers a good automated way to build unit tests hooking into the browser. there is a nice firefox plugin for recording tests and can utilize almost any unit testing framework. We had a presentation/demo at our local user group meeting last month and it looked awesome.
看看http://selenium.openqa.org/它提供了一种很好的自动化方式来构建连接到浏览器的单元测试。有一个很好的 firefox 插件用于记录测试,几乎可以使用任何单元测试框架。上个月我们在本地用户组会议上进行了演示/演示,看起来很棒。
回答by Glenn Slaven
WatiNis the best that I've found. It integrates into Visual Studio unit testing or nunit & you can do pretty much anything you need in the browser (click links, submit forms, look for text/images, etc.), plus it's written in .net so you don't need to have ruby installed (as you do for watir, which is an awesome tool none the less)
WatiN是我发现的最好的。它集成到 Visual Studio 单元测试或 nunit 中,您几乎可以在浏览器中执行任何您需要的操作(单击链接、提交表单、查找文本/图像等),而且它是用 .net 编写的,因此您不需要安装 ruby(就像你为 watir 所做的那样,它仍然是一个很棒的工具)
回答by Glenn Slaven
These frameworks are useful for integrationtesting, but they can't provide unit testing, that is, testing the View isolated from persistence, business logic, whatever.
这些框架对于集成测试很有用,但是它们不能提供单元测试,即测试与持久性、业务逻辑等隔离的View。
For unittesting Asp.Net Webforms, as well as MVC, you can use Ivonna. For example, you can mock your database access and verify that the mocked records are displayed in the datagrid. Or you can mock the membership provider and test the logged in scenario without having to navigate to the login page and entering your credentials, as with integration testing.
对于Asp.Net Webforms以及 MVC 的单元测试,您可以使用Ivonna。例如,您可以模拟数据库访问并验证模拟记录是否显示在数据网格中。或者,您可以模拟成员资格提供程序并测试登录场景,而无需导航到登录页面并输入您的凭据,就像集成测试一样。
回答by RodH257
Your best bet is separating the model logic from presentation and thoroughly unit testing the model with NUnit or similar. Testing the users interaction with the web page can be fiddly.
最好的办法是将模型逻辑与表示分离,并使用 NUnit 或类似方法对模型进行彻底的单元测试。测试用户与网页的交互可能很繁琐。
If you actually do want to unit test the users interaction with the web page some of the afformentioned tools such as waitn seem good, an addition to that which I've heard of is Selenium
如果您确实想对用户与网页的交互进行单元测试,那么上面提到的一些工具(例如 waitn)似乎不错,除了我听说过的工具之外,还有Selenium