1.安装nodejs,进入项目目录
2.安装karma和相关插件
npm install karma --save-dev
npm install karma-jasmine karma-chrome-launcher --save-dev
npm install -g karma-cli
3.执行karma start,可以看到karma会自动打开浏览器
4.进入./node_modules/karma/bin,执行karma init,就成功配置了karma自动化运行脚本
5.修改karma.conf.js配置文件
module.exports = function(config) { config.set({ basePath: '../../../', frameworks: ['jasmine'], files: ['*.js'], exclude: ['karma.conf.js'], reporters: ['progress','coverage'], preprocessors : {'src.js': 'coverage'}, coverageReporter: { type : 'html', dir : 'coverage/' }, port: 9876, colors: true, logLevel: config.LOG_INFO, autoWatch: true, browsers: ['Chrome'], captureTimeout: 60000, singleRun: false, concurrency: Infinity }) }
6.新建test.js
describe("A suite of basic functions", function() {
it("ABCD",function(){
expect("DCBA").toEqual(reverse("ABCD"));
});
it("数组",function(){
expect(["red", "green"]).toEqual(slice(["red", "green", "blue"]));
});
});
7.新建src.js
function reverse(name){
return name.split("").reverse().join("");
}
function slice(name){
return name.slice(0,2);
}
8.启动karma测试,karma start karma.conf.js
9.安装代码覆盖率插件karma-coverage,npm install karma-coverage
10.启动karma start,在工程目录下面找到index.html文件,coverage/chrome/index.html 打开后,我们看到代码测试覆率报告
其他问题:
1.CHROME_BIN的环境变量问题
设置方法:找到系统中chrome的安装位置,找到chrome.exe文件
~ D:workspacejavascriptkarma>set CHROME_BIN="C:Program Files (x86)GoogleChromeApplicationchrome.exe"
http://www.blogjava.net/qileilove/archive/2014/02/18/409973.html