Karma是Testacular的新名字,在2012年google開源了Testacular,2013年Testacular更名爲Karma。Karma是一個讓人感到很是神祕的名字,表示佛教中的緣分,因果報應,比Cassandra這種名字更讓人猜不透!javascript
Karma是一個基於Node.js的JavaScript測試執行過程管理工具(Test Runner)。該工具可用於測試全部主流Web瀏覽器,也可集成到CI(Continuous integration)工具,也可和其餘代碼編輯器一塊兒使用。這個測試工具的一個強大特性就是,它能夠監控(Watch)文件的變化,而後自行執行,經過console.log顯示測試結果。html
Jasmine是單元測試框架,本單將介紹用Karma讓Jasmine測試自動化完成。Jasmine的介紹,請參考文章:jasmine行爲驅動,測試先行java
istanbul是一個單元測試代碼覆蓋率檢查工具,能夠很直觀地告訴咱們,單元測試對代碼的控制程度。node
系統環境:chrome
win7 64bit, node v0.10.5, npm 1.2.19
安裝Karmanpm
~ D:\workspace\javascript>mkdir karma ~ D:\workspace\javascript>cd karma ~ D:\workspace\javascript\karma>npm install -g karma # 測試是否安裝成功 ~ D:\workspace\javascript\karma>karma start INFO [karma]: Karma v0.10.2 server started at http://localhost:9876/ INFO [Chrome 28.0.1500 (Windows 7)]: Connected on socket nIlM1yUy6ELMp5ZTN9Ek
從瀏覽器看到karam界面。瀏覽器
下面咱們要開始配置karam。框架
初始化karma配置文件karma.conf.jssocket
~ D:\workspace\javascript\karma>karma init Which testing framework do you want to use ? Press tab to list possible options. Enter to move to the next question. > jasmine Do you want to use Require.js ? This will add Require.js plugin. Press tab to list possible options. Enter to move to the next question. > no Do you want to capture a browser automatically ? Press tab to list possible options. Enter empty string to move to the next question. > Chrome > What is the location of your source and test files ? You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js". Enter empty string to move to the next question. > Should any of the files included by the previous patterns be excluded ? You can use glob patterns, eg. "**/*.swp". Enter empty string to move to the next question. > Do you want Karma to watch all the files and run the tests on change ? Press tab to list possible options. > yes Config file generated at "D:\workspace\javascript\karma\karma.conf.js".
安裝集成包karma-jasmine編輯器
~ D:\workspace\javascript\karma>npm install karma-jasmine
3步準備工做:
1). 建立源文件:用於實現某種業務邏輯的文件,就是咱們平時寫的js腳本
有一個需求,要實現單詞倒寫的功能。如:」ABCD」 ==> 「DCBA」
~ vi src.js function reverse(name){ return name.split("").reverse().join(""); }
2). 建立測試文件:符合jasmineAPI的測試js腳本
describe("A suite of basic functions", function() { it("reverse word",function(){ expect("DCBA").toEqual(reverse("ABCD")); }); });
3). 修改karma.conf.js配置文件
咱們這裏須要修改:files和exclude變量
module.exports = function (config) { config.set({ basePath: '', frameworks: ['jasmine'], files: ['*.js'], exclude: ['karma.conf.js'], reporters: ['progress'], port: 9876, colors: true, logLevel: config.LOG_INFO, autoWatch: true, browsers: ['Chrome'], captureTimeout: 60000, singleRun: false }); };
啓動karma
單元測試全自動執行
~ D:\workspace\javascript\karma>karma start karma.conf.js INFO [karma]: Karma v0.10.2 server started at http://localhost:9876/ INFO [launcher]: Starting browser Chrome WARN [launcher]: The path should not be quoted. Normalized the path to C:\Program Files (x86)\Google\Chrome\Application\chrome.exe INFO [Chrome 28.0.1500 (Windows 7)]: Connected on socket bVGffDWpc1c7QNdYye_6 INFO [Chrome 28.0.1500 (Windows 7)]: Connected on socket DtTdVbd4ZsgnMQrgye_7 Chrome 28.0.1500 (Windows 7): Executed 1 of 1 SUCCESS (3.473 secs / 0.431 secs) Chrome 28.0.1500 (Windows 7): Executed 1 of 1 SUCCESS (0.068 secs / 0.021 secs) TOTAL: 2 SUCCESS
咱們修改test.js
~ vi test.js describe("A suite of basic functions", function() { it("reverse word",function(){ expect("DCBA").toEqual(reverse("ABCD")); expect("Conan").toEqual(reverse("nano")); }); });
因爲karma.conf.js配置文件中autoWatch: true, 因此test.js文件保存後,會自動執行單元測試。
執行日誌以下:提示咱們單元測試出錯了。
INFO [watcher]: Changed file "D:/workspace/javascript/karma/test.js". Chrome 28.0.1500 (Windows 7) A suite of basic functions reverse word FAILED Expected 'Conan' to equal 'onan'. Error: Expected 'Conan' to equal 'onan'. at null. (D:/workspace/javascript/karma/test.js:4:25) Chrome 28.0.1500 (Windows 7): Executed 1 of 1 (1 FAILED) ERROR (0.3 secs / 0.006 secs)
增長代碼覆蓋率檢查和報告,增長istanbul依賴
~ D:\workspace\javascript\karma>npm install karma-coverage
修改karma.conf.js配置文件
~ vi karma.conf.js reporters: ['progress','coverage'], preprocessors : {'src.js': 'coverage'}, coverageReporter: { type : 'html', dir : 'coverage/' }
啓動karma start,在工程目錄下面找到index.html文件,coverage/chrome/index.html
覆蓋率是100%,說明咱們完整了測試了src.js的功能。
接下來,咱們修改src.js,增長一個if分支
function reverse(name){ if(name=='AAA') return "BBB"; return name.split("").reverse().join(""); }
Statements:75%覆蓋,Branches:50%覆蓋。
爲了產品的質量咱們要儘可能達到更多的覆蓋率,通常對於JAVA項目來講,能達到80%就是至關高的標準了。對於Javascript的代碼測試及覆蓋率研究,我還要作更多的驗證。
CHROME_BIN的環境變量問題
~ D:\workspace\javascript\karma>karma start karma.conf.js INFO [karma]: Karma v0.10.2 server started at http://localhost:9876/ INFO [launcher]: Starting browser Chrome ERROR [launcher]: Cannot start Chrome Can not find the binary C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe Please set env variable CHROME_BIN
設置方法:找到系統中chrome的安裝位置,找到chrome.exe文件
~ D:\workspace\javascript\karma>set CHROME_BIN="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"