FireUnit:基于Firebug的JavaScript单元测试扩展

John Resig最近在他的博客中发表文章John Resig – FireUnit: JavaScript Unit Testing Extension.发布了他与Jan Odvarko合作开发的一款基于Firebug的扩展FireUnit。(Firebug真是越来越强大了……)
简单说来,FireUnit给Firebug增加了一个标签面板,并提供了一些简单的JavaScript API来记录和查看测试。
举例来说:

  1. // Simple true-like/false-like testing
  2. fireunit.ok( true, "I'm going to pass!" );
  3. fireunit.ok( false, "I'm going to fail!" );
  4.  
  5. // Compare two strings - shows a diff of the
  6. // results if they're different
  7. fireunit.compare(
  8.   "The lazy fox jumped over the log.",
  9.   "The lazy brown fox jumped the log.",
  10.   "Are these two strings the same?"
  11. );
  12.  
  13. // Compare a string using a regular expression
  14. fireunit.reCompare(
  15.   /The .* fox jumped the log./,
  16.   "The lazy brown fox jumped the log.",
  17.   "Compare a string using a RegExp."
  18. );
  19.  
  20. // Display the total results
  21. fireunit.testDone();

就可以在Firebug中的Test标签面板中看到下图:

fireunit测试

FireUnit还可以模拟触发原生浏览器事件:

  1. // You can also simulate browser events
  2. var input = document.getElementsByTagName("input")[0];
  3. fireunit.mouseDown( input );
  4. fireunit.click( input );
  5. fireunit.focus( input );
  6. fireunit.key( input, "a" );

此外,可以批量测试文件(每个文件都含有一些独立测试)

  1. // Or run multiple pages of tests:
  2. fireunit.runTests("test2.html", "test3.html");
  3.  
  4. // Place at the end of every test file in order to continue
  5. fireunit.testDone();

fireunit批量测试

推荐把测试代码包含在下面的if语句内

  1. if ( typeof fireunit === "object" ) {
  2.         //Your code for test
  3. }

如果感兴趣,可以访问Fireunit.org下载并测试(这个网站内就包含了测试代码,安装后即可看到第一张图的内容)
可以在Github上获取源码,得用git而不是svn
Jan也写了一篇文章,可以看看。

This entry was posted in JavaScript and tagged , , . Bookmark the permalink.

6 Responses to FireUnit:基于Firebug的JavaScript单元测试扩展

  1. 学习笔记 says:

    jQuery的作者要和firebug联系起来,呵呵,这消息太让人高兴了,firebug是我测试脚本不可缺少的工具,jQuery更是我最喜爱的JS框架,呵呵,

  2. Daniel Hu says:

    对fireunit简单试以后感觉这个框架功能还不丰富
    http://hi.baidu.com/dearhwj/blog/item/1d1e0dbfc380f80219d81f35.html

  3. 断桥残雪 says:

    firebug不错,可是在IE下怎么就没有这样的优秀插件呢?!哎~

  4. Pingback: javascript单元测试工具QUnit介绍 « i在云端

  5. Pingback: JQuery团队打造的javascript单元测试工具QUnit介绍 « My Coding World

  6. Pingback: javascript unit test tools « My Coding World

發表迴響

您的電子郵件位址並不會被公開。 必要欄位標記為 *

*

您可以使用這些 HTML 標籤與屬性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>