引用文:karma、jasmine作angularjs單元測試
karma和jasmine介紹html
karma是Testacular的新名字
karma是用來自動化分析統計單元測試的node
jasmine是作js單元測試的git
npm install -g karma
npm install -g karma-cli (簡化karma調用的插件)angularjs
npm install -g jasminegithub
安裝完成後出現兩個文件 jasmine/jasmine-corechrome
npm install -g karma-coverage (測試代碼覆蓋率的插件) ( 安裝這個插件先需安裝Istanbul,不知道爲何,反正安裝了Istanbul,就能夠安裝了 )npm
function add(a,b){ return a + b; }
describe("my great and huge math lib",function(){ it("should perfectly complete complex addition",function(){ var result = add(3,5); expect(result).toEqual(8); }) })
在karma-my-proc目錄下ide
karma init pro-conf.js單元測試
karma start pro-conf.js測試
根據提示,選擇js目錄、chrome、jasmine等
// list of files / patterns to load in the browser files: [ 'js/**/*.js', 'test/**/*.js' ],
// test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter reporters: ['progress','coverage'], preprocessors : {'js/**/*.js': 'coverage'}, coverageReporter: { type : 'html', dir : 'coverage/' },
引用文Karma: 3 - 測試覆蓋率
karma 的插件 karma-coverage 提供了測試代碼覆蓋率的支持。
karma 的頁面:http://karma-runner.github.io/0.8/config/coverage.html 進行了說明。karma 使用 Istanbul.實現覆蓋率統計。
首先你須要安裝這個 Karma 插件,而後須要在配置文件的三個地方進行配置。
組件
plugins: [ 'karma-coverage', 'karma-jasmine', 'karma-chrome-launcher', 'karma-firefox-launcher' ]
/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:9 throw error('No provider for "' + name + '"!'); ^ Error: No provider for "framework:jasmine"! (Resolving: framework:jasmine) at error (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:22:12) at Object.parent.get (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:9:13) at get (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:54:19) at /usr/local/lib/node_modules/karma/lib/server.js:138:20 at Array.forEach (native) at Server._start (/usr/local/lib/node_modules/karma/lib/server.js:137:21) at invoke (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:75:15) at Server.start (/usr/local/lib/node_modules/karma/lib/server.js:102:18) at Object.exports.run (/usr/local/lib/node_modules/karma/lib/cli.js:243:26) at Object.<anonymous> (/usr/local/lib/node_modules/karma/bin/karma:3:23) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Function.Module.runMain (module.js:441:10) at startup (node.js:134:18)