Code Coverage

An Introduction

Trey Hunner / @treyhunner

What is code coverage?

“In computer science, code coverage is a measure used to describe the degree to which the source code of a program is tested by a particular test suite.” Wikipedia

Code coverage: in practice

Run my code and measure which parts were utilized.

Popular Coverage Metrics

  • Statement coverage or line coverage
  • Function coverage
  • Branch coverage

Statement coverage


function doSomething() {
    return;
    console.log('hello world');
}

doSomething();
						

Statement coverage

Statements covered: 75% ( 3 / 4 )

Function coverage


function pass() {}

console.log('hello world');
						

Function coverage

Functions covered: 0% ( 0 / 1 )

Branch coverage


if (true || false) {
    console.log('hello world!');
}
						

Branch coverage

Branches covered: 50% ( 2 / 4 )

Branch coverage

Branches covered: 50% ( 2 / 4 )

Demo Time!

What is it good for?

  • 100% coverage is not an indicator of good tests
  • < 100% coverage tells us we missed something

JS Code Coverage Tools

Gamifying Coverage

  • Continuous Integration: did my tests break?
  • Coverage reporting: did my code coverage decrease?

Code Coverage Reporting

  • Coveralls: popular and widely used
  • Codecov: supports branch coverage and partial line coverage

Final

Trey Hunner / @treyhunner

Web consultant available for hire.
http://truthful.technology.