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. Android锁定EditText内容和随机生成验证码

    昨天写了个小Demo,实现了随机生成验证码,和锁定EditText两个小功能,先看一下效果图: 锁定EditText在我们不须要用户编辑EditText内容的时候能够用到,实现还是非常easy的,一行 ...

  2. 通讯录C++console application

    #include<iostream> #include<fstream> #include<string> #include<cstring> #inc ...

  3. 在Eclipse下导入vlc-android并编译

    在Ubuntu14.04下载好了VLC的源代码后,VLC的Eclipseproject存放在"vlc-android"文件夹 root@dzt-VirtualBox:/home/d ...

  4. 步步详解近期大火的density_peak超赞聚类

    近期忙着在公司捣腾基于SOA的应急框架,还是前两周才在微博上看见了density_peak,被圈内好些人转载. 由于这个算法的名字起的实在惹眼,都没好意思怎么把这个算法名字翻译成中文,当然更惹眼的是, ...

  5. Redis MSET的极限在哪里

    ·背景 Redis以"快.准.狠"而著称,除了其主-从模式略失光彩(主从模式更多是被以讹传讹,3.0依旧在测试中),大部分的应用可谓尖兵利器.在一些常规写的时候,MSET和HMSE ...

  6. HDU 1501 Zipper(DP,DFS)

    意甲冠军  是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法  DP或者DFS 考虑DP  令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...

  7. JUnit介绍

    8.1.1  JUnit简介 JUnit主要用来帮助开发人员进行Java的单元测试,其设计非常小巧,但功能却非常强 大. 下面是JUnit一些特性的总结: — 提供的API可以让开发人员写出测试结果明 ...

  8. PHP 生成唯一激活码

    <?php /** * 从来没有产生一个唯一的激活码 * @return string */ function create_guid($namespace = null) { static $ ...

  9. VS2015 ASP.NET5 Web项目

    VS2015 ASP.NET5 Web项目结构浅析   前言 本文个人同步博客地址http://aehyok.com/Blog/Detail/76.html 在安装好VS2015之后,一直想看看新版本 ...

  10. CTP API开发期货自动交易平台概论

    题目比较小众,先介绍一下CTP. 综合交易平台CTP(Comprehensive Transaction Platform)是由上海期货信息技术有限公司(上海期货交易所的全资子公司)开发的期货交易平台 ...