Karma 简介
Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma。Karma是一个让人以为异常神奇的名字,示意释教中的缘分,因果报应,比Cassandra这类名字更让人猜不透!
Karma是一个基于Node.js的JavaScript测试实行历程管理东西(Test Runner)。该东西可用于测试一切主流Web浏览器,也可集成到CI(Continuous integration)东西,也可和其他代码编辑器一同运用。这个测试东西的一个壮大特征就是,它能够监控(Watch)文件的变化,然后自行实行,经由过程console.log显现测试效果。
Karma 和 mocha,Qunit,jasmine区分
But I still want to use _insert testing library_
Karma is not a testing framework, nor an assertion library. Karma just launches an HTTP server, and generates the test runner HTML file you probably already know from your favourite testing framework. So for testing purposes you can use pretty much anything you like. There are already plugins for most of the common testing frameworks:
Jasmine
Mocha
QUnit
and many others
If you can't find an adapter for your favourite framework, don't worry and write your own. It's not that hard and we are here to help.
援用官方的申明,karma不是一个测试框架,没有断言库。Karma只是运转了一个HTTP效劳并天生一个测试进口HTML文件Test Runner
。所以运用Karma须要本身挑选测试框架,主流的测试框架有:
Jasmine
Mocha
QUnit
Karma 默许的是运用jasmine
当你项目有以下需求的时刻,能够斟酌运用Karma
你想在实在的浏览器中测试代码
你想测试代码在多平台,多个浏览器(桌面,挪动装备,平台)
你想在当地开辟时期实行测试
你想在延续集成效劳器实行测试
你想实行测试在每次保留
You love your terminal.
You don’t want your (testing) life to suck.
你想运用Instanbul去自动化天生覆蓋率报告
You want to use RequireJS for your source files.
正式最先运用
装置
# Install Karma:
$ npm install karma --save-dev
# Install plugins that your project needs:
$ npm install karma-jasmine karma-chrome-launcher jasmine-core --save-dev
初始化配置文件
karma init my.conf.js
这里一起按enter就能够了,我们运用默许的测试框架jasmine
启动
karma start my.conf.js
我的项目中会报找不到Error: Cannot find module 'jasmine-core'
毛病,最先采纳的是 npm install --save-dev
的形式有这个题目,剖析了源码以为是他们代码或者是node
环境下CMD的题目,我运转的Node版本是6.9.1
。厥后经由过程npm install -g
的体式格局临时修复了
karma.conf.js
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// 测试框架
frameworks: ['jasmine'],
// 要监听的文件列表,支撑通配符形式
files: [**/*.test.js],
// 要消除的文件列表,支撑通配符
exclude: [**/*.js],
// 在文件提交给浏览器做预处理
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
总结
是否是以为Karma挺简朴的?确实是挺简朴的,这就是Test-Runner
而不是Test Framework
。Karma中心功用就是启动一个效劳并监听项目文件转变,文件转变后再革新效劳器。