【node測試系列】幾款前端測試斷言庫(Assertions lib)的選型總結

參考文章 http://blog.lvscar.info/post/...html

在編寫前端測試代碼的過程當中, 幾乎全部的測試框架都提供了根據功能、模塊(module ,spec )來切分測試用例的設計, 因爲測試代碼簡單,直接,直接反應API的特色,咱們編寫測試代碼的體驗很大程度上取決於所選用的斷言庫, 如下是我對目前幾款主流斷言庫的選型分析筆記:前端

better-assert

  • C-style TDD 斷言庫git

  • 由TJ Holowaychuk 發起github

  • 目前Github上的有 : 131 star 14 forkweb

API 樣例

assert('tobi' == user.name)
assert('number' == typeof user.age)

should.js

  • BDD 風格斷言庫框架

  • 一樣由 TJ Holowaychuk 發起post

  • 目前Github上的數據: 384 Star , 200 fork ( TJ放棄維護後由新開發者接手)測試

API樣例

foo.should.be()
bar.should.have()
foo.should.bot.be()

expect.js

  • 追求極簡的 BDD 風格斷言庫prototype

  • 基於 should.js 簡化插件

  • 目前Github上的數據: 926 star 135 fork

API樣例

expect(foo).to.be()
expect(foo).to.eql()
expect(foo).to.be.a()
expect(foo).not.to.be.an()

chai

  • BDD/TDD 雙模 ,同時支持 should / expect / assert 三種風格的斷言庫
    強大插件機制

  • chai-webdriver

  • Github上的數據1559 star 150 fork

API樣例:

should style: foo.should.be.a()
expect style: expect(foo).to.be.a()  // 近Jasmine風格
assert style: assert.equal(foo, 'bar')

其餘

  • Jasmine.js 測試框架中的斷言庫十分相似 Chai expectexpect.js ( Jasmine歷史比前二者悠久).

  • assert 風格最保守 , should 風格須要修改prototype 最爲激進。

  • 不考慮Node環境的話,Jasmine.js是好選擇

  • 我目前傾向於 Mocha + Chai 的測試方案

相關文章
相關標籤/搜索