Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage for Protractor tests in 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.

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?

like image 933
user1206050 Avatar asked Mar 12 '14 11:03

user1206050


People also ask

How do you find the code coverage of a test?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.

What is code coverage Angular?

Code coverage, also called test coverage, tells you which parts of your code are executed by running the unit and integration tests. Code coverage is typically expressed as percent values, for example, 79% statements, 53% branches, 74% functions, 78% lines.

What is unit test code coverage?

Unit tests help to ensure functionality and provide a means of verification for refactoring efforts. Code coverage is a measurement of the amount of code that is run by unit tests - either lines, branches, or methods.


1 Answers

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

  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))
    });
    
  2. Configure Protractor with the plugin protractor-istanbul-plugin.

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

  4. Extract the reports using istanbul report.

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"
},
like image 179
Dag Høidahl Avatar answered Sep 19 '22 01:09

Dag Høidahl