使用 JavaScript 进行自动化单元测试 [英] Automated unit testing with JavaScript

查看:54
本文介绍了使用 JavaScript 进行自动化单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些 JavaScript 单元测试合并到我的自动构建过程中.目前 JSUnit 可以很好地与 JUnit 配合使用,但它似乎是废弃软件,缺乏对 Ajax、调试和超时的良好支持.

有没有人有幸自动化(使用 Ant)单元测试库,例如 YUI 测试,jQuery 的 QUnit 还是 jQUnit?

注意:我使用自定义构建的 Ajax 库,所以 Dojo 的 DOH 的问题在于它要求您使用他们自己的 Ajax 函数调用和事件处理程序来处理任何 Ajax 单元测试.

解决方案

有许多 JavaScript 单元测试框架(JSUnit、scriptaculous 等),但我知道 JSUnit 是唯一一个可以与自动构建.

如果您正在进行真正的"单元测试,您应该不需要 AJAX 支持.例如,如果您使用 RPC Ajax 框架(例如 DWR),您可以轻松编写一个模拟功能:

<前>函数模拟函数(someArg,回调){var 结果 = ...;//一些处理设置超时(
函数(){ 回调(结果);},300//一些假延迟);}

是的,JSUnit 确实处理超时:在 JSUnit 测试中模拟时间

I'm trying to incorporate some JavaScript unit testing into my automated build process. Currently JSUnit works well with JUnit, but it seems to be abandonware and lacks good support for Ajax, debugging, and timeouts.

Has anyone had any luck automating (with Ant) a unit testing library such as YUI test, jQuery's QUnit, or jQUnit?

Note: I use a custom built Ajax library, so the problem with Dojo's DOH is that it requires you to use their own Ajax function calls and event handlers to work with any Ajax unit testing.

解决方案

There are many JavaScript unit test framework out there (JSUnit, scriptaculous, ...), but JSUnit is the only one I know that may be used with an automated build.

If you are doing 'true' unit test you should not need AJAX support. For example, if you are using an RPC Ajax framework such as DWR, you can easily write a mock function:

   function mockFunction(someArg, callback) {
      var result = ...; // Some treatments
      setTimeout(
function() { callback(result); }, 300 // Some fake latency ); }

And yes, JSUnit does handle timeouts: Simulating Time in JSUnit Tests

这篇关于使用 JavaScript 进行自动化单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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