测试GWT代码的最佳方法是什么? [英] What is the best way to test GWT code

查看:153
本文介绍了测试GWT代码的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试GWT代码的最佳方法是什么?



GWTTestCase在托管模式下速度太慢,没有一个模拟框架可以工作。 b

目前我们正在关注MVC,正如 http://robvanmaris.jteam.nl/2008/03/09/test-driven-development-for-gwt-ui-code/ 并使用GWTMockUtilities撤防()和恢复()来模拟小部件。我们还没有想出一种在GWT MVC中测试视图的方法。有没有更好的方法来测试GWT代码?

解决方案

如果您想单独测试GWT小部件,许多选择。您可以使用GWTTestCase来实例化您的小部件并通过它的API进行测试,这也是Google为GWT小部件所做的工作: RadioButtonTest的源代码

然而,事件触发机制在GWTTestCases中不起作用,这意味着您不能像编程式地单击按钮那样执行某些操作,并期望在侦听器上调用一些onClick()回调方法。这也很难,如果不是不可能得到底层DOM,所以它可能不是测试低级别HTML发布代码的最佳工具。



听起来像你正在遵循所有正确的步骤; Rob的文章详细介绍了如何使用Model-View-Presenter(MVP)设计模式编写可测试代码。您离开视图层的逻辑越多越好。如果这是不可能的,请使用像Selenium这样的工具来创建动态UI行为的焦点测试。



我遵循类似的策略 - 在小部件中使用最少代码的MVP。在一些情况下,我编写了一些代码来封装Grid类,所以我能够在GWTTestCase中实例化组件,将它传递给Grid,在组件上调用一些方法,并检查Grid的状态。我写了一篇关于测试优先GWT的更好软件的文章,你可以阅读

如果您想要测试使用非UI GWT类(例如URL编码或字典)的代码,则需要使用GWTTestCase,否则请按照类似的包装策略,直到代码太简单而不能打破。然后使用像Selenium这样的工具进行集成测试,或者使用一些有针对性的GWTTestCases,这些测试仅仅测试您正在使用库 - 就像JB Rainsberger所说的:不要测试框架!。

What is the best way to test GWT code?

GWTTestCase in hosted mode is too slow and none of the mocking frameworks work.

Currently we are following MVC as suggested in http://robvanmaris.jteam.nl/2008/03/09/test-driven-development-for-gwt-ui-code/ and using GWTMockUtilities disarm() and restore() to mock widgets. And we havent figured out a way to test View in GWT MVC. Is there a better way to test GWT code?

解决方案

If you're looking to test GWT widgets in isolation, there aren't many options. You can use a GWTTestCase to instantiate your widgets and test it through its API, which is what Google does for the GWT widgets themselves: Source for RadioButtonTest

However, the event-firing mechanism doesn't work in GWTTestCases, which means you can't do things like programmatically click a button and expect some onClick() callback method to be invoked on a listener. It also is hard if not impossible to get at the underlying DOM, so it may not be the best tool for testing low-level HTML-emitting code.

It sounds like you are following all the right steps; Rob's article provides an excellent description of how to write testable code using the Model-View-Presenter (MVP) design pattern. The more logic you keep out of the view layer, the better. When that's not possible, use a tool like Selenium to create focused tests of dynamic UI behavior.

I followed a similar strategy - MVP with minimal code in the widgets. In a few cases I did write some code which would wrap the Grid class, so I was able to instantiate my component in a GWTTestCase, pass it a Grid, invoke some methods on my component, and check the state of the Grid. I wrote an article for Better Software about Test-First GWT, which you can read on my blog.

If you're looking to test code that uses non-UI GWT classes (such as URL encoding or Dictionaries), you'll need to use GWTTestCase, or else follow similar wrapping strategies until the code is too simple to break. Then use an integration test with a tool like Selenium, or a few targeted GWTTestCases which only test that you're using the library correctly -- as J.B. Rainsberger says, "Don't test the framework!"

这篇关于测试GWT代码的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆