【问题标题】:How to generate LCOV report based on Jasmine's SpecRunner.html?如何根据 Jasmine 的 SpecRunner.html 生成 LCOV 报告?
【发布时间】:2015-05-05 21:23:53
【问题描述】:

我们使用 Jasmine 进行 JavaScript 单元测试。我们有一个SpecRunner.html 文件来运行测试。是否存在一个工具,我可以将路径传递给SpecRunner.html 和 JavaScript(不是规范)文件目录的路径,它会生成一个 LCOV 报告。例如,像这样:

phantomjs jasmine_lcov.js SpecRunner.html WebContent/js

【问题讨论】:

    标签: javascript unit-testing jasmine lcov


    【解决方案1】:

    我同意@zaabalonso 的观点,即Karma 是正确的选择。由于您需要 LCOV 报告,因此您还需要 karma-coverage 插件,并且假设您想在 CI 中无头运行,您可能需要 karma-phantomjs-launcher。通过 Grunt 运行是可选的,因为您始终可以使用 karma-cli (npm install -g karma-cli) 从命令行直接运行 karma。

    基本设置(使用 requireJS)如下所示:

    package.json

    {
      "private": "true",
      "devDependencies": {
        "grunt": "^0.4.5",
        "grunt-jasmine-node": "^0.3.1",
        "grunt-karma": "^0.10.1",
        "jasmine-core": "^2.3.4",
        "karma": "^0.12.32",
        "karma-coverage": "^0.3.1",
        "karma-jasmine": "^0.3.5",
        "karma-phantomjs-launcher": "^0.1.4",
        "karma-requirejs": "^0.2.2",
        "requirejs": "^2.1.17"
      }
    }
    

    karma.conf.js(注意 preprocessorscoverageReporter 部分

    module.exports = function(config) {
      config.set({
      basePath: '.',
      frameworks: ['jasmine', 'requirejs'],
      files: [{
            pattern: 'src/**/*.js',
            included: false
          }, {
            pattern: 'spec/**/*.js',
            included: false
          },
          "test-main.js"],
    
      preprocessors: {
        'src/**/*.js': ['coverage']
      },
    
      reporters: ['progress', 'coverage'],
    
      coverageReporter: {
        // specify a common output directory
        dir: 'build/reports/coverage',
        reporters: [
          { type: 'lcov', subdir: 'report-lcov' },
          { type: 'lcovonly', subdir: '.', file: 'report-lcovonly.txt' }
        ]
      },
    
      browsers: ['PhantomJS']
      });
    };
    

    test-main.js

    var allTestFiles = [];
    var TEST_REGEXP = /^\/base\/spec\/\S*(spec|test)\.js$/i;
    
    var pathToModule = function (path) {
        return path.replace(/^\/base\//, '').replace(/\.js$/, '');
    };
    
    Object.keys(window.__karma__.files).forEach(function (file) {
        if (TEST_REGEXP.test(file)) {
            // Normalize paths to RequireJS module names.
            allTestFiles.push(pathToModule(file));
        }
    });
    
    require.config({
        // Karma serves files under /base, which is the basePath from your config file
        baseUrl: '/base/',
        enforceDefine: true,
        xhtml: false,
        waitSeconds: 30,
    
        // dynamically load all test files
        deps: allTestFiles,
        callback: window.__karma__.start
    });
    

    Gruntfile.js(如果您想使用 Grunt,则可选)

    module.exports = function(grunt) {
      grunt.initConfig({
        karma: {
          unit: {
            configFile: 'karma.conf.js',
            options: {
              singleRun: true
            }
          }
        }
      });
      grunt.loadNpmTasks('grunt-karma');
      grunt.registerTask('default', ['karma:unit']);
    };
    

    您可以使用karma start 运行测试命令行。这将启动业力服务器并运行一次测试。它将保持服务器正常运行,并在您修改源或测试源时重新运行测试。如果您只想运行一次测试(也许在 CI 中),您只需运行 karma start --single-run

    【讨论】:

    • 这是一个很好的解决方案,但我实际上有一个节点环境来测试,所以我可以使用浏览器来运行测试,所以我需要在没有 Karma 的情况下进行,你知道有什么办法吗?做吗?
    • 帮助我在短短几分钟内设置了我的 LCOV 报告输出。一个超级好的答案。自从它提供以来已经过去了 3 年多:现在使用 PhantomJS 是相当冒险的,因为该项目的主要贡献者离开了船,实际上该项目已于 2018 年 3 月停止。我会改用 ChromeHeadless。
    【解决方案2】:

    Chutzpah 也会这样做。但是,它专注于 Windows 平台,因此它可能适合您,也可能不适合您。这是完整的命令行选项documentation,但您的命令可能是这样的:

    chutzpah.console.exe SpecRunner.html /coverage /lcov coverage.dat
    

    如果您需要微调覆盖范围排除或引用等内容,您可以在测试区域中使用 json 配置文件位置,如 here 所述。无需在命令行中指定被测 Javascript 代码的位置,因为 SpecRunner.html 中的引用会自动检测到该位置。

    我发现 Chutzpah 非常流畅且易于使用。

    【讨论】:

      【解决方案3】:

      我们正在使用Karma 而不是 grunt,配置看起来像这样:

        options = {
              karma: {
                  unit: {
                      options: {
                          files: ['test/unit/specs/*.js'],
                          reporters: ['progress', 'coverage'],
                          preprocessors: {
                              'src/js/*.js': ['coverage']
                          },
                          coverageReporter: {
                              type : 'html',
                              dir : 'build/coverage/'
                          },
                          frameworks: ['jasmine'],
                          singleRun: true
                      }
                  }
              }
      }
      

      你没有指定

      SpecRunner.js

      但您可以为所有规范文件指定 *.js。

      你可以运行它

      咕噜声

      这将生成与您展示的报告类似的报告。

      【讨论】:

        猜你喜欢
        • 2017-03-31
        • 1970-01-01
        • 2019-09-14
        • 1970-01-01
        • 2015-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-02
        相关资源
        最近更新 更多