Karma+Jasmine实现自动化单元测试
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 按钮,可以进入实际的测试页面。
这个页面看起来是空白的,但是执行了实际的测试脚本,右击鼠标点击检查,可以看到实际的内容:

Karma+Jasmine实现自动化单元测试的更多相关文章
- 搭建Karma+Jasmine的自动化单元测试
最近在打算将以前的代码进行重构,过程中发现自己不写自动化测试代码,而是手动的写,这样并不好,所以就学了Karma+Jasmine的自动化单元测试,以后写代码尽量要写自动化单元测试,也要测一下istan ...
- 在WebStorm中集成Karma+jasmine进行前端单元测试
在WebStorm中集成Karma+jasmine进行前端单元测试 前言 好久没有写博了,主要还是太懒=.=,有点时间都去带娃.看书了,今天给大家分享一个原创的小东西,如果大家对TDD或者BDD有兴趣 ...
- Karma和Jasmine自动化单元测试
从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...
- Karma和Jasmine 自动化单元测试环境搭建
最近初学AngularJS ,看到的一些教程中经常有人推荐使用Karma+Jasmine来进行单元测试.自己之前也对Jasmine有些了解,jasmine也是一个不错的测试框架. 1. karma介绍 ...
- 学习Karma+Jasmine+istanbul+webpack自动化单元测试
学习Karma+Jasmine+istanbul+webpack自动化单元测试 1-1. 什么是karma? Karma 是一个基于Node.js的Javascript测试执行过程管理工具.该工具可 ...
- Karma和Jasmine自动化单元测试——本质上还是在要开一个浏览器来做测试
1. Karma的介绍 Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma.Karma是一个让人感到非常神秘的 ...
- karma、jasmine做angularjs单元测试
引用文:karma.jasmine做angularjs单元测试 karma和jasmine介绍 <1>技术介绍 karma karma是Testacular的新名字 karma是用来自动化 ...
- 使用karma+jasmine做单元测试
目的 使用karma和jasmine来配置自动化的js单元测试. Karma和Jasmine Karma是由Angular团队所开发的一种自动化测试工具.链接:http://karma-runner. ...
- 利用Angularjs测试引擎Karma进行自动化单元测试
Karma是Google用于angularjs框架单元测试的js引擎(javascript test runner ), angular1 和angular2项目源码的单元测试都是基于karma和ja ...
随机推荐
- Emmet,让你爱上敲代码
原文链接:http://m.blog.csdn.net/article/details?id=53484535 不错 —— 由 都不要欺负我 分享 Emmet 是一个可用在许多流行文本编辑器上的极大简 ...
- 学习android推荐网站
1. Android Developers 作为一个Android开发者,官网的资料当然不可错过,从设计,培训,指南,文档,都不应该错过,在以后的学习过程中慢慢理解体会. 2. Android Gui ...
- tp框架之空方法与空控制器
对于空方法的两种方法: 1.建立一个empty模板(不建议,除非打算指定静态页面) 2.在控制器里面加一个empty方法 public function _empty() { echo "控 ...
- OpenStack 云计算基础知识
OpenStack Docs: Currenthttp://docs.openstack.org/ OpenStack云计算快速入门教程 - OpenStack及其构成简介_服务器应用_Linux公社 ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Channel
提起Channel,JDK的NIO类库的重要组成部分,就是提供了java.nio.SocketChannel和java.nio.ServerSocketChannel,用于非阻塞的I/O操作. 类似于 ...
- PHPExcel——读取excel
在网上找了excel读取的一些资料,个人觉得PHPExcel这还是挺好用的,相对比较全的工具. 主要功能是读取上传的excel文件,插入或更新到数据库. iconv("gbk",& ...
- 【转】前端工程筹建NodeJs+gulp+bower
转自:http://www.myexception.cn/javascript/1781968.html npm nodejs 安装过程中会自动安装npm,nodejs安装程序会在环境变量中添加两个变 ...
- Python for Infomatics 第13章 网页服务一(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 一旦利用程序通过HTTP协议获得 ...
- Web前端学习过程
推荐学习网站www.freecodecamp.cn http://www.w3school.com.cn/ 步骤: 作者:张帅 知乎链接:https://www.zhihu.com/question/ ...