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. Contest Hunter 1401 兔子与兔子

    1401 兔子与兔子 0x10「基本数据结构」例题 描述 很久很久以前,森林里住着一群兔子.有一天,兔子们想要研究自己的 DNA 序列.我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DN ...

  2. c#移位运算符("<<"及">>")详细说明

    以前感觉移位运算符自己挺明白的,也许是学的时间长了,后来一看,忘得差不多了.现在参考一些网上的学习资料,将位移运算符整理一下,作为知识点总结,也算个积累.在讲移位运算符之前,先简单补充一下原码与补码的 ...

  3. centos 7编译安装Python3.6.1

    1.准备必要的库文件 yum install -y gcc zlib-devel openssl-devel sqlite-devel 2.进入源代码包 ./configure prefix=/usr ...

  4. 释放linux端口

    感谢作者的共享,在此表示感谢 有时候关闭软件后,后台进程死掉,导致端口被占用.下面以TOMCAT端口8060被占用为例,列出详细解决过程. 解决方法: 1.查找被占用的端口 netstat -tln ...

  5. 从setTimeout谈js运行机制

    众所周知,JavaScript是单线程的编程,什么是单线程,就是说同一时间JavaScript只能执行一段代码,如果这段代码要执行很长时间,那么之后的代码只能尽情地等待它执行完才能有机会执行,不像人一 ...

  6. web服务器/应用服务器

    1.概念 Web服务器的基本功能就是提供Web信息浏览服务.它只需支持HTTP协议.HTML文档格式及URL.与客户端的网络浏览器配合.因为Web服务器主要支持的协议就是HTTP,所以通常情况下HTT ...

  7. 【JVM调优系列】----CPU过高的分析与解决方案

    1.首先通过top命令查询当前进程所占cpu的一个比重

  8. -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment avariable and mvn script match.

    eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...

  9. 【转】MVC Model建模及Entity Framework Power Tool使用

    MVC如使用Code-First代码优先约定,先建实体类,再根据实体类创建数据库. 在创建实体类后,新建一个数据上下文类,如下: publicclassMusicStoreDB : DbContext ...

  10. ubuntu上安装R的时候遇到的问题总结

    首先感谢这两篇博客的指导,第一篇是关于报错的总结,第二篇是第一篇中没有提到的错误,也就是我在安装的时候出现的错误. 1.下载R包 (去官网选择一个离你最近的镜像网址,我的是清华提供的镜像下载速度比较快 ...