Javascript单元测试框架比较Qunit VS Jasmine

工欲行其事必先利其器,好的单元测试框架是TDD成功的一半。Javascript优秀的测试框架很多, 包括Jasmine,Qunit,JsTestDriver,JSUnit,Mocha等,当然你也可以写自己的单元测试框架,本文主角是Jasmine和Qunit。我之前一直用Qunit来做单元测试,Qunit在中国占有率是非常高的,我也不例外,而美国同事们已经用到Jasmine了,为了做一个更好的选型,决定对这两个框架做了一个小小的比较。

先看看作者对自己框架的描述:

Jörn Zaefferer( QUnit作者 ) : QUnit是一个JavaScript单元测试框架,主要用于在浏览器中运行单元测试。虽然这个项目从属于jQuery,但却不依赖于jQuery,也不依赖于浏览器DOM。因此你也可以在node.js或Rhino上使用。QUnit很容易学习,你只需在html页面中包含两个文件,不需要安装或者构建任何其他东西。最短的测试集只需要一个11行的html文件。

Davis Frank(Jasmine作者): Jasmine是一个 JavaScript测试框架,目的是将BDD风格引入JavaScript测试之中。至于区别嘛,我们的目标是BDD(相比标准的TDD),因此我们尽 力帮助开发人员编写比一般xUnit框架表达性更强,组织更好的代码。此外我们还力图减少依赖,这样你可以在node.js上使用Jasmine,也可以在浏览器或移动程序中使用。

1、Jasmine和Qunit报表比较

Qunit报表

Jasmine报表

从报表来看都非常精致,结果一目了然。Jasmine有子分组,而且分组很清晰,而Qunit可以在一个测试现在多个断言数。这是他们各自的优点,这块各有千秋。

2、Jasmine和Qunit的断言比较

Jamine,他有12种原生断言比较,同时我们可以很容易的定义自己的断言,这是一个很大的优点。

Qunit自带8种断言,当然你可以自己扩展,但相对比较麻烦,唯一优势是断言可以带自定义描述。

从断言比较这块来讲,Jasmine略带优势。

3、Jasmine和Qunit的分组(分模块)比较

Jasmine用describe()来进行分组和模块,它的优势是可以嵌套,也就是可以很好的区分子模块,非常明了使用的功能。

Qunit用module()进行分组,不能区分子木块。

从这块来函,Jasmine再下一城。

4、Jasmine和Qunit的测试比较

Jasmine只有it()一个用来操作测试的方法。

Qunit包含3个测试用的方法,这个比较多。多了异步测试的方法,而且expect()可以限制断言个数。

这块Qunit略丰富于Jasmine。

5、Jasmine和Qunit的异步控制

先看Jasmine的异步控制方法,很长,很麻烦,需要自己从新封装。

    //其中player.openLibrary含异步调用
it('The music library should be opend', function() {
var flag; runs(function() {
flag = false;
player.openLibrary(function() {
flag = true;
});
});
waitsFor(function() {
return flag;
}, "aaaaaaa", 500);
runs(function() {
expect(player.library.openLibrary).toEqual(true);
});
});

再看Qunit的,很简单明了。

//其中player.openLibrary含异步调用
asyncTest('The music library should be opend', function() {
player.openLibrary(function() {
start();
ok(player.library != null, 'The "player.library" should be not null.');
ok(player.library.openLibrary === true, 'The music library should be opened');
});
});
//或则
test('The music library should be opend', function() {
stop();
player.openLibrary(function() {
start();
ok(player.library != null, 'The "player.library" should be not null.');
ok(player.library.openLibrary === true, 'The music library should be opened');
});
});

异步控制测试来看Qunit更清晰明了。

6、Mock Clock和Spies

这是两个Jasmine独有的东西,非常好。我也非常喜欢这两个功能Mock Clock能让你Mock一个时间间隔(这里我们可以精确的测试我们的代码执行时间),Spies可以用你知道函数的被调用次数和调用的方式,这是Jasmine优越的地方。

7、市场占用额:Jasmine51%,Qunit31%,这是去年12月份的统计数据。

8、 集成和配置:这块也是非常重要的,这里Qunit和Jasmine都是可以集成到Jenskin和VS2012的,也都可以用来测试RequireJs.

9、数据装载和卸载:这块也是Qunit和Jasmine都可以实现的功能非常有用。

有了这些比较,我承认我更喜欢Jasmine了,以后改用Jasmine做Javascript测试了。

Jasmine官网:http://pivotal.github.io/jasmine/

Qunit官网:http://qunitjs.com/

Javascript测试框架汇总:http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript

下面附上我之前的比较表

 
Jasmine
Qunit
Result
Assert

expect(x).toEqual(y)
compares objects or primitives x and y and passes if they are equivalent

expect(x).toBe(y)
compares objects or primitives x and y and passes if they are the same object

expect(x).toMatch(pattern)
compares x to string or regular expression pattern and passes if they match

expect(x).toBeDefined()
passes if x is not undefined

expect(x).toBeUndefined()
passes if x is undefined

expect(x).toBeNull()
passes if x is null

expect(x).toBeTruthy()
passes if x evaluates to true

expect(x).toBeFalsy()
passes if x evaluates to false

expect(x).toContain(y)
passes if array or string x contains y

expect(x).toBeLessThan(y)
passes if x is less than y

expect(x).toBeGreaterThan(y)
passes if x is greater than y

expect(function(){fn();}).toThrow(e)
passes if function fn throws exception e when executed

We can write custom matchers when you want to assert a more specific sort of expectation.

 

deepEqual()
A deep recursive comparison assertion, working on primitive types, arrays, objects, regular expressions, dates and functions.

equal()
A non-strict comparison assertion, roughly equivalent to JUnit assertEquals.

notDeepEqual()
An inverted deep recursive comparison assertion, working on primitive types, arrays, objects, regular expressions, dates and functions.

notEqual()
A non-strict comparison assertion, checking for inequality.

notStrictEqual()
A non-strict comparison assertion, checking for inequality.

ok()
A boolean assertion, equivalent to CommonJS’s assert.ok() and JUnit’s assertTrue(). Passes if the first argument is truthy.

strictEqual()
A strict type and value comparison assertion.

throws()
Assertion to test if a callback throws an exception when run.

Jasmine ≈ Qunit
Grouping

describe()
It can be setting sub group.

module()
Group related tests under a single label.
Jasmine > Qunit
Test

it()

It with two parameters: a string and a function.

Qunit can display number of assertions in a test

asyncTest()
Add an asynchronous test to run. The test must include a call to start().

expect()
Specify how many assertions are expected to run within a test.

test()
Add a test to run.

Jasmine ≈ Qunit
Asynchronous Control

runs() 
Blocks by themselves simply run as if they were called directly.

waits()
The function works with runs() to provide a naive timeout before the next block is run

waitsFor() 
Providing a better interface for pausing your spec until some other work has completed.

asyncTest()
Add an asynchronous test to run. The test must include a call to start().

start()
Start running tests again after the testrunner was stopped. See stop().

stop()
Stop the testrunner to wait for async tests to run. Call start() to continue.

Jasmine < Qunit
Mock and Spies

Providing mock and spies are good functions for unit test.

\ Jasmine > Qunit
Market share 45% 31% Jasmine > Qunit
Test Report Jasmine report Qunit report Jasmine ≈ Qunit
Integrate VS Y Y Jasmine ≈ Qunit
Integrate CI Y Y Jasmine ≈ Qunit
Parameterized tests \ plugins qunit-parameterize Jasmine < Qunit
Configuration with RequireJs Y Y Jasmine ≈ Qunit
Setup and Teardown Y Y Jasmine ≈ Qunit
想查看我的更多文章你可以点击这里: 前端学习日志,如果你和我一样喜欢足球和喜欢玩足球类的游戏,你可以点击这里:绿茵吧
 

Javascript单元测试框架比较Qunit VS Jasmine的更多相关文章

  1. JavaScript单元测试框架-Jasmine

    Jasmine的开发团队来自PivotalLabs,他们一开始开发的JavaScript测试框架是JsUnit,来源于著名的JAVA测试框架JUnit.JsUnit是xUnit的JavaScript实 ...

  2. javascript单元测试框架mochajs详解

    关于单元测试的想法 对于一些比较重要的项目,每次更新代码之后总是要自己测好久,担心一旦上线出了问题影响的服务太多,此时就希望能有一个比较规范的测试流程.在github上看到牛逼的javascript开 ...

  3. javascript单元测试框架mochajs详解(转载)

    章节目录 关于单元测试的想法 mocha单元测试框架简介 安装mocha 一个简单的例子 mocha支持的断言模块 同步代码测试 异步代码测试 promise代码测试 不建议使用箭头函数 钩子函数 钩 ...

  4. JavaScript单元测试框架JsUnit基本介绍和使用

    JavaScript单元测试框架JsUnit基本介绍和使用 XUnit framework XUnit是一套标准化的独立于语言的概念和结构集合,用于编写和运行单元测试(Unit tests). 每一个 ...

  5. JavaScript单元测试框架:Jasmine

    摘抄:http://blog.csdn.net/GuoJiangweigege/article/details/52130589 互联网的快速发展,给web开发人员带来了前所未有的挑战.对于前端开发, ...

  6. JS单元测试框架:QUnit

    QUnit:jQuery的单元测试框架,但不仅限于jQuery(从这个工具不需要引用jquery.js可以看出) index.html <!-- 官网 http://qunitjs.com/ - ...

  7. javascript单元测试(转)

    1.      什么是单元测试 在计算机编程中,单元测试(又称为模块测试)是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小可测试部件.在过程化编程中,一个单元就是单 ...

  8. (译)学习如何构建自动化、跨浏览器的JavaScript单元测试

    作者:Philip Walton 译者:Yeaseon 原文链接:点此查看 译文仅供个人学习,不用于任何形式商业目的,转载请注明原作者.文章来源.翻译作者及链接,版权归原文作者所有. ___ 我们都知 ...

  9. [转]javascript单元测试

    1.      什么是单元测试 在计算机编程中,单元测试(又称为模块测试)是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小可测试部件.在过程化编程中,一个单元就是单 ...

随机推荐

  1. 西门子PLC学习笔记8-(计时器)

    计时器port这包括:信号输入.时间.复位信号.出口.[计时器剩余时间(BI二进制表示法.BCD码表示)其输出被存储MW] 定时器包括::S_PULSE(脉冲定时器).S_PEXT(延时脉冲定时器). ...

  2. likely()与unlikely()

    he gcc C compiler has a built-in directive that optimizes conditional branches as either very likely ...

  3. win7访问xp共享访问不了

    这个问题不是一天两天了,经历几次了,所以记下来. 1. 一些XP对用户权限作了特殊的优化设置.限制了只有guest用户才能用于做局域网共享用户. 2. 大多数时候,需要设置一个密码,才能用于访问. 3 ...

  4. MonkeyRunner源码分析之-谁动了我的截图?

    本文章的目的是通过分析monkeyrunner是如何实现截屏来作为一个例子尝试投石问路为下一篇文章做准备,往下一篇文章本人有意分析下monkeyrunner究竟是如何和目标测试机器通信的,所以最好的办 ...

  5. Linq to Sql : 动态构造Expression进行动态查询

    原文:Linq to Sql : 动态构造Expression进行动态查询 前一篇在介绍动态查询时,提到一个问题:如何根据用户的输入条件,动态构造这个过滤条件表达式呢?Expression<Fu ...

  6. SALT 加密

    大家都知道,MD5加密是不可逆.但事实上,我们通常值的MD5算法.黑客的眼下破解率相对较高.也有非常多站点上干脆就提供批量解密MD5的服务,当然是收费的.http://www.xmd5.org.这里提 ...

  7. 使用AdvancedInstaller打包web工程设置tomcat端口的方法

    原文:使用AdvancedInstaller打包web工程设置tomcat端口的方法 1.首先,要把你要打包的tomcat下的server.xml文件删掉,因为tomcat自带的serv ...

  8. xheditor 进阶

    xhEditor提供两种方式初始化编辑器: 方法1:利用class属性来初始化和传递各种初始化参数,例:  class="xheditor {skin:'default'}" 方法 ...

  9. 【原创】窥视懒人的秘密---android下拉刷新开启手势的新纪元

    小飒的成长史原创作品:窥视懒人的秘密---android下拉刷新开启手势的新纪元转载请注明出处 **************************************************** ...

  10. 让Xcode Lua 语法高亮

    本人不太喜欢用code ide 还是喜欢XCODE的风格 1.让Xcode支援Lua语法高亮(Syntax Highlighting) 1. 下载https://github.com/breinhar ...