03-如何使用Karma

Karma 是Google 开源的一个基于Node.js 的 JavaScript 测试执行过程管理工具(Test Runner)。该工具可用于测试所有主流Web浏览器,也可集成到 CI (Continuous integration)工具,也可和其他代码编辑器一起使用。

起步

  1. 安装
$ npm install karma -g
$ npm install -g karma-cli

进行配置,最终生成karma-conf.js配置文件

$ mkdir karma-sample
$ cd karma-sample
$ karma init karma-conf.js

在生成配置文件的过程中,有几个选择是比较重要的!假设我们的源码都在js目录下,测试用例都在test目录下。
回答测试文件位于哪里这个问题时, 我们键入如下命令:

js/**/*.js
test/**/*.js

在回答测试框架的时候可以自行选择,例子中使用jasmine框架。

  1. 安装包
$ npm install
  1. 生成代码
    写一个简单的待测试文件add.js
function add(a,b){
  return a+b;
}

写一个测试用例add-test.js

describe("my great and huge math lib", function () {
  it("should perfectly complete complex addition", function () {
    var result = add(3, 5);
    expect(result).toEqual(9);
  });
});
  1. 启动运行
$ karma start karma-conf.js

配置文件

下面是一个标准的配置文件,设置在Firefox和Chorme中进行测试

// Karma configuration
// Generated on Mon Mar 17 2014 12:46:02 GMT+0800 (CST)
module.exports = function(config) {
  config.set({
    // base path, that will be used to resolve files and exclude
    basePath: '',
    // frameworks to use
    frameworks: ['mocha'],
    // list of files / patterns to load in the browser
    files: [
      'src/**/*.js',
      'test-lib/**/*.js',
      'test/**/*.js'
    ],
    // reporters : ['progress'],
    // list of files to exclude
    exclude: [
    ],
    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    reporters: ['spec'],
    // 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, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera (has to be installed with `npm install karma-opera-launcher`)
    // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
    // - PhantomJS
    // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
    browsers: ['Chrome', 'Firefox'],
    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,
    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false
  });
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文介绍了基于karma+jasmine的web前端自动化测试的方案和详细操作指导。 名词解释 Node.js 是...
    belllee阅读 2,345评论 0 7
  • 中文翻译 ng help ng build 构建您的应用程序并将其放入输出路径(dist /默认情况下)。 别名:...
    4ea0af17fd67阅读 2,062评论 0 0
  • 现在是二零一七年四月一日下午四点十分,我今天背了217个单词,看了53条回答,花了17分钟写了一首诗,已经听了十分...
    _欢喜_阅读 595评论 0 1
  • 江南,我的江南 染有了我的所有的生活 装载上了我的所有的梦想 我吸吮了你的所有的气息 可对于你的温柔,你的美丽 可...
    一只独角羊阅读 402评论 1 1
  • 1. HashMap的数据结构 HashMap实际上是一个“链表散列”的数据结构,是数组与链表的结合体。HashM...
    JohnShen阅读 185评论 0 0