Karma+Jasmine实现自动化测试

###Cyper的笔记

需要先装plugin:

可以在这里找到全部的:https://www.npmjs.com/browse/keyword/karma-launcher

比如

sudo npm install karma-chrome-launcher --save-dev

sudo npm install karma-firefox-launcher --save-dev

可以同时测试多个browser,比如:

cyper@zerorun:~$ karma start karma.conf.js --browsers=Firefox,Chrome

在命令行中指定的参数会覆盖karma.config.js中的参数。

###以下为转载部分

一、Karma和Jasmine介绍

1.Karma介绍
Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma。Karma是一个让人感到非常神秘的名字,表示佛教中的缘分,因果报应,比Cassandra这种名字更让人猜不透!

Karma是一个基于Node.js的JavaScript测试执行过程管理工具(Test Runner)。该工具可用于测试所有主流Web浏览器,也可集成到CI(Continuous integration)工具,也可和其他代码编辑器一起使用。这个测试工具的一个强大特性就是,它可以监控(Watch)文件的变化,然后自行执行,通过console.log显示测试结果。

2.Jasmine介绍
Jasmine (茉莉)是一款 JavaScript BDD(行为驱动开发)测试框架,它不依赖于其他任何 JavaScript 组件。它有干净清晰的语法,让您可以很简单的写出测试代码。对基于 JavaScript 的开发来说,它是一款不错的测试框架选择。

比较流行的有Qunit和Jasmine,如果你想更详细了解二者的区别,请狠狠的点击Javascript单元测试框架Qunit和Jasmine的比较

二、基本实现

1.环境安装

1 root@ubuntu:/var/www/yeomanProj# npm install -g karma

2.配置karma.config.js

01 root@ubuntu:/var/www/yeomanProj# karma init
02  
03 Which testing framework do you want to use ?
04 Press tab to list possible options. Enter to move to the next question.
05 > jasmine
06  
07 Do you want to use Require.js ?
08 This will add Require.js plugin.
09 Press tab to list possible options. Enter to move to the next question.
10 > no
11  
12 Do you want to capture any browsers automatically ?
13 Press tab to list possible options. Enter empty string to move to the next question.
14 > Chrome
15 >
16  
17 What is the location of your source and test files ?
18 You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js".
19 Enter empty string to move to the next question.
20 >
21  
22 Should any of the files included by the previous patterns be excluded ?
23 You can use glob patterns, eg. "**/*.swp".
24 Enter empty string to move to the next question.
25 >
26  
27 Do you want Karma to watch all the files and run the tests on change ?
28 Press tab to list possible options.
29 > yes
30  
31 Config file generated at "/var/www/yeomanProj/karma.conf.js".

3.安装集成包

1 root@ubuntu:/var/www/yeomanProj# npm install karma-jasmine

 

4.新建测试文件isInteger.js

01 root@ubuntu:/var/www/yeomanProj# vi isInteger.js
02  
03 /**
04  * isInteger
05  * @param {Number}
06  * @return {Boolean}
07  */
08  
09 function isInteger(num) {
10     if(typeof num !=="number") return false;
11     var pattern = /^[1-9]\d*$/g;
12     return pattern.test(num);
13 }

5.新建测试用例integerTest.js

1 root@ubuntu:/var/www/yeomanProj# vi isIntegerTest.js
2  
3 describe("This is a integer test!", function(){
4     it("Is integer", function() {
5         expect(true).toEqual(isInteger(20));
6         expect(false).toEqual(isInteger("20"));
7         expect(false).toEqual(isInteger(0));
8     })
9 });

6.修改配置文件karma.config.js

01 root@ubuntu:/var/www/yeomanProj# vi karma.conf.js
02  
03 // Karma configuration
04 // Generated on Tue Jul 15 2014 08:34:25 GMT-0700 (PDT)
05  
06 module.exports = function(config) {
07   config.set({
08  
09     // base path that will be used to resolve all patterns (eg. files, exclude)
10     basePath: '',
11  
12     // frameworks to use
13     // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
14     frameworks: ['jasmine'],
15  
16     // list of files / patterns to load in the browser
17     files: ["*.js"],
18  
19     // list of files to exclude
20     exclude: ["karma.config.js"],
21  
22     // preprocess matching files before serving them to the browser
23     // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
24     preprocessors: {
25     },
26  
27     // test results reporter to use
28     // possible values: 'dots', 'progress'
29     // available reporters: https://npmjs.org/browse/keyword/karma-reporter
30     reporters: ['progress'],
31  
32     // web server port
33     port: 9876,
34  
35     // enable / disable colors in the output (reporters and logs)
36     colors: true,
37  
38     // level of logging
39     // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
40     logLevel: config.LOG_INFO,
41  
42     // enable / disable watching file and executing tests whenever any file changes
43     autoWatch: true,
44  
45     // start these browsers
46     // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
47     browsers: ['Chrome'],
48  
49     // Continuous Integration mode
50     // if true, Karma captures browsers, runs the tests and exits
51     singleRun: false
52   });
53 };

7.运行测试

1 root@ubuntu:/var/www/yeomanProj# karma start karma.conf.js
2 INFO [karma]: Karma v0.12.17 server started at http://localhost:9876/
3 INFO [launcher]: Starting browser Chrome
4 INFO [Chrome 31.0.1650 (Linux)]: Connected on socket SBOihLxtQbirXMCTxFB_ with id 5395451

8.查看测试结果及自动化

1 Chrome 31.0.1650 (Linux): Executed 1 of 1 SUCCESS (0.938 secs / 0.213 secs)
2 INFO [watcher]: Changed file "/var/www/yeomanProj/isIntegerTest.js".
3 Chrome 31.0.1650 (Linux): Executed 1 of 1 SUCCESS (3.503 secs / 0.494 secs)

上述测试环境中有文件增加或者改变都会随时监控并自动化测试。
karma-jasmine

三、参考链接

http://jasmine.github.io/1.3/introduction.html

转载声明:

本文标题:Karma+Jasmine实现自动化测试

本文链接:http://www.zuojj.com/archives/517.html,转载请注明转自Benjamin-专注前端开发和用户体验

转载于:https://my.oschina.net/uniquejava/blog/481487

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值