AngularJS 中量角器测试的代码覆盖率 [英] Code coverage for Protractor tests in AngularJS

查看:24
本文介绍了AngularJS 中量角器测试的代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用量角器在我的 angularJS 应用程序中运行一些 e2e 测试(如 angularJS 文档中所建议的那样).我四处搜索,找不到任何有关如何测量量角器测试覆盖率的信息.

I am running some e2e tests in my angularJS app with protractor (as recommended in the angularJS documentation). I've googled around and cannot find any information on how to measure coverage for my protractor tests.

我想我在这里遗漏了一些东西......有没有办法获得量角器 e2e 测试的代码覆盖率报告?或者它只是单元测试的一个功能?

I think I'm missing something here... is there any way to get a code coverage report for protractor e2e tests? Or is it simply a feature for unit tests?

推荐答案

这可以使用 Istanbul 来实现.这是过程,以及我从我们的项目中提取的一些示例配置(未测试):

This is achievable using Istanbul. Here is the process, with some example configurations that I've extracted from our project (not tested):

  1. 使用命令 istanbul instrument 检测您的代码.确保伊斯坦布尔的覆盖变量是 __coverage__.

  1. Instrument your code using the command istanbul instrument. Make sure that istanbul's coverage variable is __coverage__.

// gulpfile.js

gulp.task('concat', function () {
    gulp.src(PATH.src)
      // Instrument for protractor-istanbul-plugin:
      .pipe(istanbul({coverageVariable: '__coverage__'}))
      .pipe(concat('scripts.js'))
      .pipe(gulp.dest(PATH.dest))
});

  • 使用插件 protractor-istanbul-plugin 配置 Protractor..p>

  • Configure Protractor with the plugin protractor-istanbul-plugin.

    // spec-e2e.conf.js
    var istanbulPlugin = require('protractor-istanbul-plugin');
    
    exports.config = {
        // [...]
        plugins: [{ inline: istanbulPlugin }]
    };
    

  • 运行您的测试.

  • Run your tests.

    这种方法对我很有效,并且很容易与来自单元测试的覆盖率报告结合使用.为了自动化,我将第 1 步放入我的 gulpfile.js 中,将第 3 步和第 4 步放入 testposttest 脚本中package.json,或多或少是这样的:

    This approach has worked for me and is easy to combine with coverage reports from unit tests as well. To automate, I've put step 1 into my gulpfile.js and step 3 and 4 in the test and posttest scripts in package.json, more or less like this:

    // In package.json:
    "scripts": {
      "test": "gulp concat && protractor tests/spec-e2e.conf.js",
      "posttest": "istanbul report --include coverage/**/.json --dir reports/coverage cobertura"
    },
    

    这篇关于AngularJS 中量角器测试的代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆