前端单元测试环境搭建 Karma Jasmine
Karma
官网
On the AngularJS team, we rely on testing and we always seek better tools to make our life easier. That's why we created Karma - a test runner that fits all our needs.
Karma是一个测试工具。
The main goal for Karma is to bring a productive testing environment to developers.
Karma的主要目标是给开发者带来一个非常有效,有效率的测试环境。
Commandline Interface(命令行接口)
你可以在项目目录下运行 ./node_modules/karma/bin/karma start 命令,这个很繁琐。也可以全局安装命令 npm install -g karma-cli
配置karma:
运行 karma init karma.conf.js 命令,名字自己命名
启动 karma start karma.conf.js
karma配置项:
frameworks:
Type: Array
Default: []
Description: List of test frameworks you want to use(需要的测试框架). Typically, you will set this to ['jasmine']
, ['mocha']
or ['qunit']
...
Please note just about all frameworks in Karma require an additional plugin/framework library to be installed (via NPM)(需要npm安装).
plugins:
Type: Array
List of plugins to load. A plugin can be a string (in which case it will be required by Karma) or an inlined plugin - Object. By default, Karma loads all sibling NPM modules which have a name starting with karma-*.
Note:
Just about all plugins in Karma require an additional library to be installed (via NPM).
类型是数组,karma使用的插件,默认会加载以karma-开头的npm包。
karma中所有的插件都需要npm安装
preprocessors:
Type: Object
Default: {'**/*.coffee': 'coffee'}
Description: A map of preprocessors to use.
Preprocessors can be loaded through plugins.
Note: Just about all preprocessors in Karma (other than CoffeeScript and some other defaults) require an additional library to be installed (via NPM).
karma中的preprocessors都需要npm安装,除了CoffeeScript和一些默认
Be aware that preprocessors may be transforming the files and file types that are available at run time. For instance, if you are using the "coverage" preprocessor on your source files, if you then attempt to interactively debug your tests, you'll discover that your expected source code is completely changed from what you expected. Because of that, you'll want to engineer this so that your automated builds use the coverage entry in the "reporters" list, but your interactive debugging does not.
browsers:
Type: Array
Capturing browsers on your own can be a tedious and time-consuming task. However, Karma can automate this for you. Simply add the browsers you would like to capture into the configuration file.
Then, Karma will take care of auto-capturing these browsers, as well as killing them after the job is over.
Note: Most of the browser launchers(需要安装浏览器启动器) need to be loaded as plugins.
默认是空数组,用npm下载后,加入空数组
Jasmine
Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.
Jasmine是一个行为驱动开发测试框架,不依赖任何其它JavaScipt框架,不需要DOM。
常用名词或API介绍:(version2.7)
describe
A test suite begins with a call to the global Jasmine function describe
with two parameters: a string and a function. The string is a name or title for a spec suite - usually what is being tested. The function is a block of code that implements the suite.
通过调用Jasmine全局函数,定义了测试套装(suites)。每个套装有一个或是多个测试规格组成
specs it expectaion
Specs are defined by calling the global Jasmine function it
, which, like describe
takes a string and a function. The string is the title of the spec and the function is the spec, or test. A spec contains one or more expectations that test the state of the code. An expectation in Jasmine is an assertion that is either true or false. A spec with all true expectations is a passing spec. A spec with one or more false expectations is a failing spec.
通过调用Jasmine全局函数it,定义了spec。it有两个参数,第一个是字符串,第二个是函数,即spect或是test(规格或是测试,下文统一翻译为规格测试)。
一个规格测试包含一个或多个期待(expectation),Jasmine中的期待是断言(assertion)。如果所有期待都为true,则称规格测试为成功规格测试,否则是失败规格测试。
Macher
Expectations are built with the function expect
which takes a value, called the actual. It is chained with a Matcher function, which takes the expected value.
通过expect函数,建立期待(expectation),参数是真正的值。期待可以链式调用一个匹配器函数,匹配器函数接受一个被期望的值作为参数。
expect().tobe()
expect().not.tobe()
expect().toEqual()
expect().not.toEqual()
expect().toMatch()
expect().not.toMatch()
> defined undefined null
expect().toDefined()
expect().not.toDefined()
expect().toBeUndefined()
expect().not.toBeUndefined()
expect().toBeNull()
expect().not.toBeNull()
> 正确,不正确 和 错误,不错误
expect().toBeTruthy()
expect().not.toBeTruthy()
expect().toBeFalsy()
expect().not.toBeFalsy()
> 小于,不小于 和 大于,不大于
expect().toBeLessThan()
expect().not.toBeLessThan()
expect().toBeGreaterThan()
expect().not.toBeGreaterThan()
> 异常抛出
expect().toThrow()
expect().not.toThrow()
describe('matcher', function() {
// jasmine 内置了许多匹配器
// tobe 相当于 ===
it("and has a positive case", function() {
expect(true).toBe(true);
});
it("and can have a negative case", function() {
expect(false).not.toBe(true);
});
// “相等”
it("should work for objects", function() {
var foo = {
a: 12,
b: 34
};
var bar = {
a: 12,
b: 34
};
expect(foo).toEqual(bar);
expect(foo).not.toEqual({a:1});
});
// 匹配正则表达式
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/);
});
// undefined
it("The `toBeUndefined` matcher compares against `undefined`", function() {
var a = {
foo: "foo"
}; expect(a.foo).not.toBeUndefined();
expect(a.bar).toBeUndefined();
});
// defined
it("The 'toBeDefined' matcher compares against `undefined`", function() {
var a = {
foo: "foo"
};
expect(a.foo).toBeDefined();
expect(a.bar).not.toBeDefined();
});
// null
it("The 'toBeNull' matcher compares against null", function() {
var a = null;
var foo = "foo";
expect(null).toBeNull();
expect(a).toBeNull();
expect(foo).not.toBeNull();
});
// true
it("The 'toBeTruthy' matcher is for boolean casting testing", function() {
var a, foo = "foo"; expect(foo).toBeTruthy();
expect(a).not.toBeTruthy();
});
// false
it("The 'toBeFalsy' matcher is for boolean casting testing", function() {
var a, foo = "foo"; expect(a).toBeFalsy();
expect(foo).not.toBeFalsy();
});
// contains 包含
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("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' matcher is for mathematical comparisons", function() {
var pi = 3.1415926,
e = 2.78;
expect(pi).toBeGreaterThan(e);
expect(e).not.toBeGreaterThan(pi);
});
// 异常抛出
it("The 'toThrow' matcher is for testing if a function throws an exception", function() {
var foo = function() {
return 1 + 2;
};
var bar = function() {
return a + 1;
};
var baz = function() {
throw 'what';
};
expect(foo).not.toThrow();
expect(bar).toThrow();
expect(baz).toThrow('what');
});
// 精确度匹配
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);
});
前端单元测试环境搭建 Karma Jasmine的更多相关文章
- 搭建Karma+Jasmine的自动化单元测试
最近在打算将以前的代码进行重构,过程中发现自己不写自动化测试代码,而是手动的写,这样并不好,所以就学了Karma+Jasmine的自动化单元测试,以后写代码尽量要写自动化单元测试,也要测一下istan ...
- Karma和Jasmine 自动化单元测试环境搭建
最近初学AngularJS ,看到的一些教程中经常有人推荐使用Karma+Jasmine来进行单元测试.自己之前也对Jasmine有些了解,jasmine也是一个不错的测试框架. 1. karma介绍 ...
- windows下vue+webpack前端开发环境搭建及nginx部署
一.开发环境搭建 1.前端框架一般都依赖nodejs,我们首先要安装node.js.请参考http://www.cnblogs.com/wuac/p/6381819.html. 2.由于许多npm的源 ...
- Windows 环境下vue+webpack前端开发环境搭建
一.开发环境搭建 1.前端框架一般依赖node.js,我们首先要安装node.js. 2.由于许多npm 的源都在国外的地址,安装起来特别慢,所以我们这里利用淘宝的镜像服务器. 安装命令为:npm i ...
- 使用jasmine-node 进行NodeJs单元测试 环境搭建
关于jasmine就不多说了,关于语法请参加官方文档.http://pivotal.github.io/jasmine/ 关于NodeJS的单元测试框架有多种,如果要在NodeJS中使用jasmine ...
- Sentinel控制台前端开发环境搭建
Sentinel:分布式系统的流量防卫兵. 官网:https://sentinelguard.io Github:https://github.com/alibaba/sentinel Wiki:ht ...
- webpack前端开发环境搭建
要搭建webpack开发环境,首先要安装NodeJS,后面的过程均在NodeJS已经安装的基础上进行. 1. 首先建立一个工程目录,命名为,其目录结构如下: 其中dist目录用于存放生成的文件,src ...
- 转 ShowSlow+Yslow页面前端性能测试环境搭建
----//工具介绍 Yslow:YSlow是Yahoo发布的一款基于FireFox的插件. YSlow可以对网站的页面进行分析,并告诉你为了提高网站性能,如何基于某些规则而进行优化. ShowSlo ...
- dubbo应用程序的单元测试环境搭建(springtest,powermock,mockito)
转:http://blog.csdn.net/yys79/article/details/66472797 最近,项目中频繁用到dubbo,而且java工程用引用了几十个关联系统的服务(如用户认证,基 ...
随机推荐
- Java类型简介
1 java基本数据类型 1.1 基本数据类型 java的基础数据类型有多少个,每个在内存的分配是多少呢? 类型 分配字节 byte 1 short 2 char 2 int 4 long 8 ref ...
- 基于Cython和内置distutils库,实现python源码加密(非混淆模式)
起因 python本身只能做混淆,不能加密,多年的商业软件开发经验导致有某种"洁癖"欲望,将py编译打包 尝试 pyinstaller原理是freeze打包pyc文件,利用工具可完 ...
- Mac 10.12安装图片切换工具ArcSoft Photo+
说明:Mac自带的图片切换不能连续切换,这款工具和美图看看差不多. 下载: (链接: https://pan.baidu.com/s/1i5rLYzr 密码: 49dp)
- [Xamarin.Android] 如何使用Google Map V2 (转帖)
Google Map v1已經在2013年的3月開始停止支援了,目前若要在你的Android手機上使用到Google Map,就必須要使用 到Google Map v2的版本.在Xamarin要使用G ...
- springMVC与Struts2区别
1.拦截级别 Struts2是类级别的拦截,一个类对应一个request上下文 SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应 所以说从架构本身 ...
- 全网最详细的Git学习系列之介绍各个Git图形客户端(Windows、Linux、Mac系统皆适用ing)(图文详解)
不多说,直接上干货! 一.TortoiseGit - The coolest Interface to Git Version Control TortoiseGit 是 TortoiseSVN 的 ...
- web端网页适配移动端注意事项,以及遇到的问题
1.一定要加上 <!-- name=“viewport” 指视口 width=device-width 宽度等于视口宽 initial-scale=1.0 像素比例 maximum-scale= ...
- 关于docker的理解随记
1.容器其实不是什么新技术,说白了就是namespace对资源进行隔离,再加UFS实现分层镜像,以及cgroup实现资源限制.这些技术,都是linux中已有的技术,而且有些技术很早之前就有了. 2.上 ...
- linux 安装jdk 二进制版本,非安装版
0.下载jdk8 登录网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html选择对 ...
- NDK编程jni学习入门,声明native方法,使其作为java与c的交互接口
首先,新建工程,简历一个jave类,在其中声明native方法,关键字为native,表面这个方法是从java以为的语言实现. 其次,要实用javac编译此java文件(javac是jdk中的命令,需 ...