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 ...
随机推荐
- 同步异步,阻塞非阻塞 和nginx的IO模型
同步与异步 同步和异步关注的是消息通信机制 (synchronous communication/ asynchronous communication).所谓同步,就是在发出一个*调用*时,在没有得 ...
- Markdown的简单实用
Markdown Markdown是一种标记语言,能让笔者像写代码代码一样写文档,不用再为文档的排版样式而费心.
- Cannot add Controls from 64-bit Assemblies to the Toolbox or Use in Designers Within the Visual Studio IDE
原文地址:https://support.microsoft.com/en-us/kb/963017 Source: Microsoft Support RAPID PUBLISHING RAPID ...
- nginx,apache,tomcat配置https的阿里提供的文档
安装证书 ( 1 ) 打开 Nginx 安装目录下 conf 目录中的 nginx.conf 文件,找到 # HTTPS server # #server { # listen 443; # serv ...
- MXNet设计和实现简介
原文:https://github.com/dmlc/mxnet/issues/797 神经网络本质上是一种语言,我们通过它来表达对应用问题的理解.例如我们用卷积层来表达空间相关性,RNN来表达时间连 ...
- AgileEAS.NET SOA 中间件平台5.2版本下载、配置学习(一):下载平台并基于直连环境运行
一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...
- 【使用Unity开发Windows Phone上的2D游戏】(2)初识工具
下载工具 我们需要下载两个工具:Unity 和 2D Toolkit Unity 在我写这篇文章的时候,最新的Unity版本是4.2.1, 下载地址 Unity公司的开发效率实在是很高,我一个多月前开 ...
- 复制Eclipse工作空间设置
将新建的workspace下的.metadata.plugins内容全部删除: 将原来的workspace下的.metadata.plugins内容除了org.eclipse.core.resourc ...
- [数学]内接多边形求pi
- 配置Tomcat使用https协议
一. 创建tomcat证书 这里使用JDK自带的keytool工具来生成证书: 1. 在jdk的安装目录\bin\keytool.exe下打开keytool.exe 2. 在命令行中输入以下命令: ...