工欲行其事必先利其器,好的單元測試框架是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) expect(x).toBe(y) expect(x).toMatch(pattern) expect(x).toBeDefined() expect(x).toBeUndefined() expect(x).toBeNull() expect(x).toBeTruthy() expect(x).toBeFalsy() expect(x).toContain(y) expect(x).toBeLessThan(y) expect(x).toBeGreaterThan(y) expect(function(){fn();}).toThrow(e) We can write custom matchers when you want to assert a more specific sort of expectation. |
deepEqual() equal() notDeepEqual() notEqual() notStrictEqual() ok() strictEqual() throws() |
Jasmine ≈ Qunit |
Grouping | describe() |
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() expect() test() |
Jasmine ≈ Qunit |
Asynchronous Control | runs() waits() waitsFor() |
asyncTest() start() stop() |
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 |