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
相關文章
相關標籤/搜索