【问题标题】:Karma disconnects before all tests runKarma 在所有测试运行之前断开连接
【发布时间】:2016-09-13 11:09:59
【问题描述】:

我们使用 karma 作为 jasmine 单元测试的测试运行器,测试 Angular 应用程序。我们有超过 6000 次测试。 Karma 运行测试的速度非常慢,而且从来没有超过 2700 次测试(11 分钟)。

Karma 可以运行的测试数量是否有限制,或者测试运行器的性能随着测试数量的增加而下降是正常的吗?

// Karma Config file
module.exports = function(config) {
  config.set({
    basePath: '../',
    frameworks: ['jasmine'],
    files: [
       ...list of files
    ],
    proxies: {
        '/images/': '/base/src/client/images/',
    },
    exclude: [
      'src/client/app/window/window.js'
    ],
    preprocessors: {
      '**/*.partial.html': ['ng-html2js']
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: ['Chrome'],
    singleRun: true,
    concurrency: Infinity,
    browserNoActivityTimeout: 100000,
    plugins: [
      'karma-jasmine',
      'karma-chrome-launcher',
      'karma-phantomjs-launcher',
      'karma-firefox-launcher',
      'karma-ie-launcher',
      'karma-ng-html2js-preprocessor'
    ],
    ngHtml2JsPreprocessor: {
      moduleName: 'templates',
      stripPrefix: 'src/client'
      }
  })
}

【问题讨论】:

  • 首先,恭喜您如此专注于单元测试。 6K是一个很大的数字。现在,关于速度慢,有一个已知问题是当 karma 选项卡不在焦点上时,测试执行速度很慢,特别是在 chrome 上。如果您手动运行,请确保您的 chrome 业力标签处于焦点位置。
  • 我们确实以 Chrome 选项卡为中心运行测试,但它们仍然运行缓慢。我们也尝试过使用 Phantomjs,但它们同样慢。是否有 Karma 报告器来监控测试执行?

标签: javascript angularjs unit-testing karma-runner karma-jasmine


【解决方案1】:

为了帮助减缓测试执行,我发现使用 ng-bullet (https://www.npmjs.com/package/ng-bullet) 对加快测试非常有帮助。对于 6k 次测试,让您的组件移动可能是一项艰巨的任务,但您应该能够一次执行一个规范文件,从更长的规范开始,看看它对执行时间有多大帮助。

我用来帮助测试的另一件事是将测试分解为多个套件,这样如果测试应用程序的特定部分,您就可以并行使用多个套件或分成更小的块。本文讨论了这个概念以及使用 ng-bullet:https://www.forbes.com/sites/forbesdigitalgroup/2019/05/14/improve-your-angular-jasmine-unit-test-speeds-by-500/#45ee76a14163

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-26
    • 2014-07-11
    • 2020-03-04
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多