1.Karma介绍
Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma。Karma是一个让人感到非常神秘的名字,表示佛教中的缘分,因果报应,比Cassandra这种名字更让人猜不透!
Karma是一个基于Node.js的JavaScript测试执行过程管理工具(Test Runner)。该工具可用于测试所有主流Web浏览器,也可集成到CI(Continuous integration)工具,也可和其他代码编辑器一起使用。这个测试工具的一个强大特性就是,它可以监控(Watch)文件的变化,然后自行执行,通过console.log显示测试结果。
2.Jasmine介绍
Jasmine (茉莉)是一款 JavaScript BDD(行为驱动开发)测试框架,它不依赖于其他任何 JavaScript 组件。它有干净清晰的语法,让您可以很简单的写出测试代码。对基于 JavaScript 的开发来说,它是一款不错的测试框架选择。
3.jasmine基础语法
describe
Your Tests)Suite表示一个测试集,以函数describe(string, function)
封装,它包含2个参数:string
:测试组名称,function
:测试组函数。
一个Suite(describe
)包含多个Specs(it
),一个Specs(it
)包含多个断言(expect
)。
2)Specs : 规范
describe("A suite",function(){
it("contains spec with an expectation",function(){
expect(true).toBe(true);
});
});
3)方法
4)Expectations :期望值
5)Matchers : 匹配器
it("and can have a negative case",function(){
expect(false).not.toBe(true);
});
describe("Included matchers:",function(){
it("The 'toBe' matcher compares with ===",function(){
var a =12;
var b = a;
expect(a).toBe(b);
expect(a).not.toBe(null);
});
}
it("The 'toMatch' matcher is for regular expressions",function(){
var message ='foo bar baz';
expect(message).toMatch(/bar/);
expect(message).toMatch('bar');
expect(message).not.toMatch(/quux/);
});
it("The 'toBeDefined' matcher compares against `undefined`",function(){
var a ={
foo:'foo'
};
expect(a.foo).toBeDefined();
expect(a.bar).not.toBeDefined();
}); it("The `toBeUndefined` matcher compares against `undefined`",function(){
var a ={
foo:'foo'
};
expect(a.foo).not.toBeUndefined();
expect(a.bar).toBeUndefined();
});
it("The 'toBeNull' matcher compares against null",function(){
var a =null;
var foo ='foo';
expect(null).toBeNull();
expect(a).toBeNull();
expect(foo).not.toBeNull();
});
describe("The 'toContain' matcher",function(){
it("works for finding an item in an Array",function(){
var a =["foo","bar","baz"];
expect(a).toContain("bar");
expect(a).not.toContain("quux");
});
it("also works for finding a substring",function(){
var a ="foo bar baz";
expect(a).toContain("bar");
expect(a).not.toContain("quux");
});
});
it("The 'toBeLessThan' matcher is for mathematical comparisons",function(){
var pi =3.1415926, e =2.78;
expect(e).toBeLessThan(pi);
expect(pi).not.toBeLessThan(e);
}); it("The 'toBeGreaterThan' is for mathematical comparisons",function(){
var pi =3.1415926, e =2.78, c =3;
expect(c).toBeGreaterThan(e);
expect(c).not.toBeGreaterThan(pi);
});
it("The 'toBeCloseTo' matcher is for precision math comparison",function(){
var pi =3.1415926, e =2.78;
expect(pi).not.toBeCloseTo(e,2);
expect(pi).toBeCloseTo(e,0);
});
it("The 'toThrow' matcher is for testing if a function throws an exception",function(){
var foo =function(){
return1+2;
};
var bar =function(){
return a +1;
};
expect(foo).not.toThrow();
expect(bar).toThrow();
});
4.基本实现
// Karma configuration
// Generated on Fri Oct 21 2016 12:00:54 GMT+0800 (中国标准时间)
module.exports =function(config){
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
6 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:[
],
// 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
25 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
38 singleRun:false,
// Concurrency level
// how many browser should be started simultaneous
concurrency:Infinity
})
};
这两个参数不需要修改,使用默认值就好。
karma 支持三个命令。
- start [<configFile>] [<options>] 启动 Karma 持续执行,也可以执行单次的测试,然后直接收集测试结果.
- init [<configFile>] 初始化配置文件.
- run [<options>] [ -- <clientArgs>] Trigger a test run.
4)新建测试用例
在项目文件夹中,创建一个名为 test 的子文件夹来保存测试用例。然后在 test 文件夹中创建一个 unit 的文件夹来保存单元测试用例。一般来说,我们会为测试用例的文件名称提供一个特定的模式,以便对测试用例进行统一处理,这里我们约定测试用例的文件名以 .spec.js 为结尾。
(1)自定义测试
describe('hello',function(){
it('test hello',function(){
var a ='hello';
expect(a).toEqual('hello');
});
});
function add(a,b){
return a + b;
}
describe('hello',function(){
it('test add',function(){
var a = add(3,6);
expect(a).toEqual(9);
}); it('test add',function(){
var a = add(3,6);
expect(a).toEqual(10);
});
});
5)修改karma配置文件
确认你的 karma 配置文件中,包含了被测试代码和测试代码。
// list of files / patterns to load in the browser
files: [
'src/**/*.js',
'test/**/*.spec.js'
],
// list of files to exclude
exclude: [
'karma.conf.js'
],
点击 DEBUG 按钮,可以进入实际的测试页面。
这个页面看起来是空白的,但是执行了实际的测试脚本,右击鼠标点击检查,可以看到实际的内容: