1.环境介绍(windows系统+webstorm编辑器)
- 安装nvmw,管理node的版本
- 安装node v0.10.21版本,karma框架暂时支持v0.10版本node
- 安装karma和插件
install karma:
npm install karma --save-dev
install plugins:npm install karma-jasmine karma-chrome-launcher --save-dev
命令接口:npm install -g karma-cli
2.配置文件:
初始化配置文件:
karma init
修改如下:
修改需要测试的文件目录配置项:files
// Karma configuration
// Generated on Fri May 15 2015 09:30:32 GMT+0800 (中国标准时间)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'test/*.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
3.demo测试
源文件src.js
/**
* 字符串翻转
* @param name
* @returns {string}
*/
function reverse(name) {
return name.split("").reverse().join("");
}
/**
* 判断是否是整数
* @param num
* @returns {boolean}
*/
function isInteger(num) {
if (typeof num !== "number") return false;
var pattern = /^[1-9]\d*$/g;
return pattern.test(num);
}
/**
* 判断value的数据类型
* @param value
*/
function getTypeofVal(val) {
if(typeof val == 'object'){
return objType(val);
}
return typeof val;
}
/**
* 获取对象的类型
* @param arr
* @returns {string}
*/
function objType(arr){
return Object.prototype.toString.apply(arr);
}
测试文件test.spec.js
/**
* 测试整数
*/
describe("this is a integer test!", function () {
it("Is integer", function () {
expect(true).toEqual(isInteger(20));
expect(false).toEqual(isInteger("20"));
expect(false).toEqual(isInteger(0));
expect(false).toEqual(isInteger(0.1));
})
});
/**
* 测试翻转
*/
describe("this is a string reverse test!", function () {
it("reverse String", function () {
expect("DCBA").toEqual(reverse("ABCD"))
})
});
describe("Math method test!", function () {
it("Math method", function () {
expect(1.0).toEqual(Math.round(.6));
expect(1.0).toEqual(Math.ceil(.6));
expect(0.0).toEqual(Math.floor(.6));
})
it("number cacl", function () {
var x = .3 - .2;
var y = .2 - .1;
expect(false).toEqual(x == y);
expect(false).toEqual(x == .1);
expect(true).toEqual(y == .1);
})
});
/**
* 测试数据类型
*/
describe("data type method test!", function () {
it("base data type", function () {
expect('number').toEqual(getTypeofVal(80));
expect('string').toEqual(getTypeofVal("this is a string"));
expect('number').toEqual(getTypeofVal(NaN));
expect('boolean').toEqual(getTypeofVal(true));
})
it("object type",function(){
expect('[object Null]').toEqual(objType(null));
expect('[object Array]').toEqual(objType(values));
expect('[object Date]').toEqual(objType(now));
expect('[object Function]').toEqual(objType(printArray));
})
});
启动测试:
karma start karma.conf.js
结果如下:
D:\practise\node\awesome-test>karma start karma.conf.js
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 41.0.2272 (Windows 7)]: Connected on socket j6Z3lnrXa4ZDPCGKrsq_ with id 82349825
Chrome 41.0.2272 (Windows 7) LOG: 1
Chrome 41.0.2272 (Windows 7) LOG: 2
Chrome 41.0.2272 (Windows 7) LOG: 3
Chrome 41.0.2272 (Windows 7) LOG: Object{from: 1, to: 3}
查看浏览器结果: