JSF 单元测试
我正在尝试为 JSF 找到一个实用的单元测试框架。
我了解 JSFUnit,但这对我来说非常不切实际。 我需要在我的项目中包含大约 10 个 JAR,并跳过许多其他环节才能使其运行。
我意识到,由于需要模拟平台和客户端,对 Web 应用程序进行单元测试是很困难的。 但还有更好的办法吗?
I'm trying to find a practical unit testing framework for JSF.
I know about JSFUnit, but this is very impractical to me. I need to include about 10 JARs to my project, and jump through many other hoops just to get it running.
I realize that -- due to the need to simulate a platform and a client -- unit testing web applications is difficult. But is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您是否考虑过使用 Selenium 或其他工具进行集成测试? Selenium 允许您直接在浏览器中记录和运行测试。 您还可以使用 Selenium Remote Control 在多个浏览器和多个平台上运行测试。
编写单元测试很好,但它可能会提供更多功能来创建一些功能集成测试,而不是对表示层代码进行单元测试。
Have you thought about doing integration testing with Selenium or another tool? Selenium allows you to record and run tests directly in the browser. You can also run tests in multiple browsers and on multiple platforms with Selenium Remote Control.
Writing unit tests is good, but it might provide more to create some functional integration tests rather than unit-testing the presentation layer code.
在我目前正在进行的项目中,我们尝试使用硒。 实际上,我们花了很多时间编写这些 Selenium 测试,但发现它们几乎没有增加任何价值,因为 UI 变化太大,而您最终只会加倍努力,而投资回报却很少。
Selenium 的另一个问题是它需要部署代码,这意味着它不能很好地与单元测试框架(例如 Maven)配合使用。
我想说的是,为托管 bean 编写真正优秀的单元测试是非常宝贵的。
On the project I'm working on at the moment we dabbled with using selenium. We actually spent a lot of time writing these selenium tests, but found that they added little value because the UI changes so much and you just end up doubling your effort for very little return on investment.
Another problem with selenium is that it requires your code to be deployed, which means it doesn't play well with unit test frameworks eg maven.
What I would say is that writing really good unit tests for your managed beans is invaluable.
您是否看过 jsfunitwar Ant 任务或 JSFUnit 提供的 Maven 插件? 两者都大大降低了生成要测试的 .war 文件的复杂性。 我在当前的项目中使用 JSFUnit,发现白盒和黑盒测试功能的组合非常强大。 因为 JSFUnit 在幕后使用 HtmlUnit,所以您可以非常轻松有效地检查生成的 HTML,或者相反,验证内部 JSF 支持 bean 的状态。 我能够将 JSFUnit 测试合并到我的持续集成过程中,并且对结果非常满意。
Have you taken a look at the jsfunitwar Ant task or alternatively the Maven plugin provided by JSFUnit? Both greatly reduce the complexity of generating the .war file to be tested. I'm using JSFUnit on my current project and find the combination of white box and black box testing capabilities to be very powerful. Because JSFUnit uses HtmlUnit under the covers, you can very easily and effectively examine the generated HTML, or conversely, verify the state of your internal JSF backing beans. I was able to incorporate the JSFUnit tests into my Continuous Integration process and have been quite pleased with the outcome.
HttpUnit 也可以作为替代方案。 它提供 API,因此您可以选择自动化测试。
http://httpunit.sourceforge.net/index.html
HttpUnit can also be an alternative. It provides apis so you have a choice to automate the tests.
http://httpunit.sourceforge.net/index.html
Selenium 是表面,jsfunit 是内在。 如果项目不简单,我建议使用jsfunit。 因为团队成员可以更改 jsf ManagedBean 名称等,所以您可以使用 jsfunit 捕获它。
Selenium is superficial, jsfunit is inward. I recommend that use jsfunit if project is not simple. Because team member can change jsf managedbean names or etc, you can catch that with jsfunit.
我同意 Paul 的看法,Selenium 非常容易设置和开始使用。 我在 Firefox 中使用 Selenium IDE,并在该级别进行了一些自定义,然后您可以将它们导出到其他平台,例如 Java JUnit 测试。 下载并启动 selenium-server.jar 非常容易,将 selenium-java-client-driver 添加到我现有的 Eclipse Maven POM 驱动程序项目中; 然后在 Eclipse 中启动相同的导出 JUnit 测试。 我主要想使用 Java 来实现基本 Selenium IDE 不支持的循环。
我也为我的项目配置了 JSF Unit,这确实需要更多时间来配置...更重要的是,尽管使用 JSFUnit 等容器内测试,但对测试的更改需要重建 WAR、在容器中重新部署,然后从 Eclipse 或通过浏览器。 因此,对于快速尝试一个小的改变来说,这是非常耗时的。 当然,使用 JSFUnit,您可以访问 JSFSession 等的所有内部结构,因此我猜这取决于您需要的测试粒度。
如果有人知道一种更快的方法来扭转对 JSFUnit 测试的更改并执行它,我会很感兴趣。 在这方面,Selenium 测试绝对感觉更像 JUnit 测试。
I'm with Paul on Selenium being very easy to setup and start working with. I use Selenium IDE in Firefox with some customization at that level, then you can export these to other platforms such as Java JUnit tests. It was quite easy to download and launch the selenium-server.jar, add the selenium-java-client-driver to my existing Eclipse Maven POM driver project; then launch the same exported JUnit test in Eclipse. I mainly wanted to use Java just for looping which the basic Selenium IDE didn't support.
I have configured JSF Unit for my project too which does require more time to configure... more importantly though with in-container tests like JSFUnit changes to the test require rebuilding the WAR, redeploying in the container and then executing from Eclipse or via a browser. So for quickly trying a small change this is time consuming. Of course with JSFUnit you have access to all the internals of the JSFSession etc so it depends what granularity of testing you need I guess.
I'd be interested if anybody knows a faster way to turnaround changes to a JSFUnit test and execute it. Definitely Selenium tests feel more like JUnit tests in that regard.