views:

106

answers:

3

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

+2  A: 

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'.

ho1
I was hoping to see some code :)
Yassir
@Yassir: You might want to clarify you question about that. However, since NCover is open source, you can already see how it does it... I'd suggest seeing if you can look at an as early version as possible, where there will probably be less code and so should show you the structure easier.
ho1
I've actually tried that but the code isn't really clear since the are no comments in the code :)
Yassir
@Yassir: How unusal for an Open Source project :). There's the PartCover project, maybe that's better http://sourceforge.net/projects/partcover/
ho1
A: 

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 block is covered, all code paths are covered. 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.

Tomas Lycken
+1  A: 

Here's a technical paper on 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.

Ira Baxter