###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 |
03 | Which testing framework do you want to use ? |
04 | Press tab to list possible options. Enter to move to the next question. |
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. |
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. |
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. |
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. |
27 | Do you want Karma to watch all the files and run the tests on change ? |
28 | Press tab to list possible options. |
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 |
09 | function isInteger(num) { |
10 | if(typeof num !=="number") return false; |
11 | var pattern = /^[1-9]\d*$/g; |
12 | return pattern.test(num); |
5.新建测试用例integerTest.js
1 | root@ubuntu:/var/www/yeomanProj# vi isIntegerTest.js |
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)); |
6.修改配置文件karma.config.js
01 | root@ubuntu:/var/www/yeomanProj# vi karma.conf.js |
03 | // Karma configuration |
04 | // Generated on Tue Jul 15 2014 08:34:25 GMT-0700 (PDT) |
06 | module.exports = function(config) { |
09 | // base path that will be used to resolve all patterns (eg. files, exclude) |
14 | frameworks: ['jasmine'], |
16 | // list of files / patterns to load in the browser |
19 | // list of files to exclude |
20 | exclude: ["karma.config.js"], |
22 | // preprocess matching files before serving them to the browser |
27 | // test results reporter to use |
28 | // possible values: 'dots', 'progress' |
30 | reporters: ['progress'], |
35 | // enable / disable colors in the output (reporters and logs) |
39 | // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG |
40 | logLevel: config.LOG_INFO, |
42 | // enable / disable watching file and executing tests whenever any file changes |
45 | // start these browsers |
49 | // Continuous Integration mode |
50 | // if true, Karma captures browsers, runs the tests and exits |
7.运行测试
1 | root@ubuntu:/var/www/yeomanProj# karma start karma.conf.js |
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) |
上述测试环境中有文件增加或者改变都会随时监控并自动化测试。
三、参考链接
http://jasmine.github.io/1.3/introduction.html