前端单元测试环境搭建 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工程用引用了几十个关联系统的服务(如用户认证,基 ...
随机推荐
- 快速沃尔什变换 FWT 学习笔记【多项式】
〇.前言 之前看到异或就担心是 FWT,然后才开始想别的. 这次学了 FWT 以后,以后判断应该就很快了吧? 参考资料 FWT 详解 知识点 by neither_nor 集训队论文 2015 集合幂 ...
- HBase 使用外部的 zookeeper
HBase 启动时,默认会根据hbase-site.xml文件中的如下设置端口上启动一个zookeeper服务: <property> <name>hbase.zookeepe ...
- 解决Visual C++ Redistributable for Visual Studio 2015的安装问题(摘录)
1. Visual C++ Redistributable for Visual Studio 2015系统要求:Windows 7情况下必须是Windows 7 with SP1.或者Windows ...
- 【Vim】Vim学习
1. 三种模式 (1)命令模式:刚启动vim便进入命令模式,此时敲击键盘会被当做命令来处理 以下是常用的几个命令: i 切换到插入模式,以输入字符.x 删除当前光标所在处的字符.: 切换到底线命令模式 ...
- ele
vue饿了么app项目实战视频 5-1 1.项目代码规范修改.
- JS写游戏
最近在看萧井陌的视频.感觉一些东西挺有意思的,尤其是解决问题的过程,以及一个好程序应该改进的地方. 萧大的GITHUB:github.com/guaxiao/gua.game.js 视频:https: ...
- tomcat监控工具probe
probe官网:http://www.lambdaprobe.org/ 但是已经链接至github了:https://github.com/psi-probe/psi-probe 下载psi-prob ...
- mysql经典查询
建立数据库 1.建立一个数据库 create database work; 2.进入数据库work use work; 3.数据库默认编码可能不支持中文,可以在这里设置下 set names gbk; ...
- 04 Thread的方法(源代码) 和 线程的状态
1 .Thread中重要的属性 publicclass Thread implements Runnable { //继承自Runnable接口private char name[]; // 以c ...
- dll ocx cab IE 自动安装
我们打开淘宝等网站时,IE浏览器会提示安装空间,这个控件便是用于对用户名密码进行加密的ActiveX控件.如何在我们的站点上安装如此控件,让用户可以通过简单的点击便可方便使用我们的空间呢? 下面是如何 ...