Karma的第一次使用

Karma 中文意思为因果报应

这是一个写AngularJs的工程师们写的一个测试工具

这里是Karma的官网Karma


首先我的电脑是事先安装了node.js和npm的



第一步先安装Karma,在命令行中输入

npm install -g Karma


安装成功

接下来安装用Jasmine(茉莉花Jasmine)单元测试框架所必需的插件

npm install -g karma-jasmine
npm install -g karma-chrome-launcher


但是现在还不能够使用Karma start等指令

需要在命令行中输入

install -g karma-cli


接下来进入事先建立好的文件夹

我这里是winterexercise文件夹

该文件夹下有两个子文件夹分别为tobetest和test

两个文件夹中分别是待测js文件和测试代码

//tobetest/add.js
function add(a, b){
	c = a + b;
	return c;
}

//test/testAdd.js
describe("a suit to test", function () {
  it("should it pass", function () {
    expect(add(1, 3)).toEqual(4);
    //expect(add(4, 7)).toEqual(18);
  });
});

在命令行中进入到winterexercise目录下

➜  winterexcise  karma init karma.config.js



根据提示选择一些选项,比如

Which testing framework do you want to use ?
Press tab to list possible options. Enter to move to the next question.
> jasmine

这里我选择的是Jasmine框架




Do you want to capture any browsers automatically ?
Press tab to list possible options. Enter empty string to move to the next question.
> Chrome

这是对浏览器进行选择



What is the location of your source and test files ?
You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js".
Enter empty string to move to the next question.

提示输入待测文件和测试文件的目录

这里输入

> tobetest/*.js
> test/*.js

Karma会自动进行匹配



选择结束之后,会在winterexercise文件夹中生成一个js文件karma.config.js

在这个配置文件中我们可以自己动手对刚才的配置选项进行修改


在命令行中输入

karma start karma.config.js

将自动弹出Chrome浏览器

内容如下图所示


我们再去命令行中查看


到这里,我的第一个小程序就算是测试成功了!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值