您的位置:首页 > Web前端 > Node.js

Karma的第一次使用

2016-02-02 21:04 846 查看
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浏览器

内容如下图所示



我们再去命令行中查看



到这里,我的第一个小程序就算是测试成功了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript node.js