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的更多相关文章

  1. 搭建Karma+Jasmine的自动化单元测试

    最近在打算将以前的代码进行重构,过程中发现自己不写自动化测试代码,而是手动的写,这样并不好,所以就学了Karma+Jasmine的自动化单元测试,以后写代码尽量要写自动化单元测试,也要测一下istan ...

  2. Karma和Jasmine 自动化单元测试环境搭建

    最近初学AngularJS ,看到的一些教程中经常有人推荐使用Karma+Jasmine来进行单元测试.自己之前也对Jasmine有些了解,jasmine也是一个不错的测试框架. 1. karma介绍 ...

  3. windows下vue+webpack前端开发环境搭建及nginx部署

    一.开发环境搭建 1.前端框架一般都依赖nodejs,我们首先要安装node.js.请参考http://www.cnblogs.com/wuac/p/6381819.html. 2.由于许多npm的源 ...

  4. Windows 环境下vue+webpack前端开发环境搭建

    一.开发环境搭建 1.前端框架一般依赖node.js,我们首先要安装node.js. 2.由于许多npm 的源都在国外的地址,安装起来特别慢,所以我们这里利用淘宝的镜像服务器. 安装命令为:npm i ...

  5. 使用jasmine-node 进行NodeJs单元测试 环境搭建

    关于jasmine就不多说了,关于语法请参加官方文档.http://pivotal.github.io/jasmine/ 关于NodeJS的单元测试框架有多种,如果要在NodeJS中使用jasmine ...

  6. Sentinel控制台前端开发环境搭建

    Sentinel:分布式系统的流量防卫兵. 官网:https://sentinelguard.io Github:https://github.com/alibaba/sentinel Wiki:ht ...

  7. webpack前端开发环境搭建

    要搭建webpack开发环境,首先要安装NodeJS,后面的过程均在NodeJS已经安装的基础上进行. 1. 首先建立一个工程目录,命名为,其目录结构如下: 其中dist目录用于存放生成的文件,src ...

  8. 转 ShowSlow+Yslow页面前端性能测试环境搭建

    ----//工具介绍 Yslow:YSlow是Yahoo发布的一款基于FireFox的插件. YSlow可以对网站的页面进行分析,并告诉你为了提高网站性能,如何基于某些规则而进行优化. ShowSlo ...

  9. dubbo应用程序的单元测试环境搭建(springtest,powermock,mockito)

    转:http://blog.csdn.net/yys79/article/details/66472797 最近,项目中频繁用到dubbo,而且java工程用引用了几十个关联系统的服务(如用户认证,基 ...

随机推荐

  1. CentOS开放端口号

    #vi /etc/sysconfig/iptables 在打开的文件中增加一份端口配置信息: A INPUT -p tcp -m state --state NEW -m tcp --dport 81 ...

  2. testng多线程

    1.设置多线程,同一浏览器运行两个用例,但是有其中有一个运行较慢

  3. SpringBoot 整合 Logback

    Logback is intended as a successor to the popular log4j project, picking up where log4j leaves off.L ...

  4. AngularJS $timeout和 $interval ,定时器手机验证码倒计时

    $interval是 window.setInterval的Angular包装形式.$interval指每次延迟时间后执行响应的函数(也可以说:每次执行函数的间隔时间).$interval多数用于计时 ...

  5. svn 被锁,清理恢复过程

    svn 被锁,清理恢复过程 http://stackoverflow.com/questions/18000363/tortoisesvn-wont-allow-me-to-add-any-files ...

  6. 使用C++11 开发一个半同步半异步线程池

    摘自:<深入应用C++11>第九章 实际中,主要有两种方法处理大量的并发任务,一种是一个请求由系统产生一个相应的处理请求的线程(一对一) 另外一种是系统预先生成一些用于处理请求的进程,当请 ...

  7. 开始使用 Vuejs 2.0 ---简单总结2

    Vuejs的常用指令 v-html v-show v-if v-for v-on 1 .v-html v-html 更新元素或者变量的innerHTML,按普通html解析,和v-text的区别是在变 ...

  8. 人脸识别(基于Caffe)

    人脸识别(基于Caffe, 来自tyd) 人脸识别(判断是否为人脸) LMDB(数据库, 为Caffe支持的分类数据源) mkdir face_detect cd face_detect mkdir ...

  9. 设置主外键时 ORA-02298: 无法验证 - 未找到父项关键字 --NOVALIDATE;

    主要原因是: 在添加CONSTRAINT的时候,默认是需要VALIDATE表中的已有数据的. 你要插入的表A里,有外键连接到另一个表B的主键,你在表A的外键列插入的值,在表B的主键列找不到就不能插入. ...

  10. 仿淘宝商品详情页上拉弹出新ViewController

    新项目就要开始做了,里面有购物那块,就试着先把淘宝商品详情页的效果做了一下. 1.需求 1.第一次上拉时,A视图拉到一定距离将视图B从底部弹出,A视图也向上 2.显示B视图时下拉时,有刷新效果,之后将 ...