How do code coverage tools work?

Code Coverage

Code Coverage Problem Overview


How do code coverage tools like NCover know what parts of the code were executed and what parts were not?

Code Coverage Solutions


Solution 1 - Code Coverage

Here's a technical paper on http://www.semdesigns.com/Company/Publications/TestCoverage.pdf">How to Implement test coverage tools for arbitrary languages.

My company builds a family of test coverage tools for Java, C#, C++, PHP, COBOL, PLSQL, ... based on this principle.

Solution 2 - Code Coverage

Quote straight from the NCover FAQ: NCover reports the percentage of branches in the code that have been taken throughout the course of your automated testing. It achieves this by instrumenting the source code at each branch, and writing the 'hit' points to a file. These 'hit' points are then compared to the total possible points that could have been 'hit'.

Solution 3 - Code Coverage

From this source:

>> NCover uses the .NET Framework profiler API to monitor an application's execution. When a method is loaded by the CLR, NCover retrieves the IL and replaces it with instrumented IL code

So in short, it hooks itself into the just-in-time compilation.

Not all tools work the same way though. Other tools work by modifying the bytecode of your application after the code has been compiled.

Solution 4 - Code Coverage

I know this is question is old but if you are still interested you can see an example of how such instrumentation is performed for .NET applications by looking at the open source project OpenCover.

OpenCover inserts instrumentation points at significant points in the code.

  1. For code line coverage it uses the sequence points taken from a PDB file
  2. For branch coverage it instruments COND_BRANCH instructions by instrumenting the jump target(s) and the next instruction after the branch instruction i.e. no jump.
  3. For method instrumentation it instruments the first instruction of any method.

All of these rules are applied in CoverageInstrumentation.cpp after the appropriate points have been located using Mono.Cecil and passed to the profiler from the console host.

The source code to PartCover is also available (as indicated) but this is much harder to follow but it also uses sequence points from PDBs to determine where it instruments the code.

Solution 5 - Code Coverage

It requires that you run your tests once with code coverage analysis enables, and then simply counts the number of blocks (that is, scope blocks) covered and compares to the total number of blocks in the project(s) you are testing.

The basic reasoning is that if each possible combination of code blocks is covered, all code paths are covered1. The main argument against putting too much weight in code coverage numbers is that "easy" blocks like getters and setters, that give no real value (and could hardly go wrong...) count just as much as more error-prone blocks of code.


1) As noted by Ira Baxter in a comment, the previous wording of this sentence was incorrect. Please read the comments for some discussion on this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHannoun YassirView Question on Stackoverflow
Solution 1 - Code CoverageIra BaxterView Answer on Stackoverflow
Solution 2 - Code CoverageHans OlssonView Answer on Stackoverflow
Solution 3 - Code CoveragePeteView Answer on Stackoverflow
Solution 4 - Code CoverageShaun WildeView Answer on Stackoverflow
Solution 5 - Code CoverageTomas AschanView Answer on Stackoverflow