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

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


先看看做者对本身框架的描述: node

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


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


一、Jasmine和Qunit报表比较 express

Qunit报表 浏览器

image2013-5-23 17-9-56.png

Jasmine报表 框架

image2013-5-23 17-10-2.png

从报表来看都很是精致,结果一目了然。Jasmine有子分组,并且分组很清晰,而Qunit能够在一个测试如今多个断言数。这是他们各自的优势,这块各有千秋。 less

二、Jasmine和Qunit的断言比较异步

Jamine,他有12种原生断言比较,同时咱们能够很容易的定义本身的断言,这是一个很大的优势。 async

Qunit自带8种断言,固然你能够本身扩展,但相对比较麻烦,惟一优点是断言能够带自定义描述。

从断言比较这块来说,Jasmine略带优点。

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

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

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

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

四、Jasmine和Qunit的测试比较

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

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

这块Qunit略丰富于Jasmine。

五、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更清晰明了。

六、Mock Clock和Spies

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

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

八、 集成和配置:这块也是很是重要的,这里Qunit和Jasmine都是能够集成到Jenskin和VS2012的,也均可以用来测试RequireJs.

九、数据装载和卸载:这块也是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
相关文章
相关标签/搜索