views:

139

answers:

5

I am not asking the static code analysis which is provided by StyleCop or Fxcop. Both are having different purpose and it serves well. I am asking whether is there a way to find the code coverage of your user control or sub module? For ex, you have an application which uses the helper classes in a separate assembly. Inorder to ensure the unit testing code coverage, we need to run the application and ensure using NCover or similar tool.

My requirement is, without running it, is there any possible to find code coverage of the helper classes or similar kind of assemblies?

+2  A: 

In general, the answer is no. This is equivalent to the halting problem, which is not computable.

Simon Nickerson
A: 

Coverity Static Analysis is a tool that is can identify many secuirty flaws in a program. It can also identify dead code and can be used to help satisfy testing regulations such as D0178B which requires that the developers demonstrate that all code can be executed.

Rook
+2  A: 

There are (research) tools based on abstract interpretation or model checking that can show coverage properties without execution, for subsets of language. See, e.g.

"Analyzing Functional Coverage in Bounded Model Checking", Grosse, D. Kuhne, U. Drechsler, R. 2008

In general, yes, there are approaches, but they're specialized, and may require some formal methods experience. This kind of stuff is still cutting edge research.

Don Stewart
+5  A: 

See Static Estimation for Test Coverage for a technique that estimates coverage without executing the source code.

The basic idea is to compute a program slice for each test case, and then "count" what the slice enumerates. A (forward) slice is effectively that part of a program that you can reach from a specific starting point in the code, in this case, the test code.

While the technical paper above is hard to get if you're not an ACM member [or you didn't attend the conference where it was presented :], there's a slide presentation here.

Of course, running this static estimator only tells you (roughly) what code will be exercised. It doesn't substitute for actually running the tests, and verifying that they pass!

Ira Baxter
A: 

I would say no; with the exception of 'dead code' which a compiler can determine.

My definition of code coverage is a result which indicates how many times each line of code is run in your program: which, of course, means running the program. The determining factor here is usually the values of data passing through the program which the determine the paths of executions taken by conditionals. A static analysis, like a compiler, could deduce lines of code that cannot run under any conditions.

An example here is if your program uses a third-party library, but there is a bug in the library. If your program never uses those parts of the library, or the data you send to the library causes it to avoid the bug, then you won't be affected.

You could write a program that, by reflection, assumes that all conditionals will be taken, and follows all function calls, through all derived classes, but I'm not sure what this will tell you. It certainly can't tell you whether or not there are any bugs in the lines of code covered.

quamrana
In general, I've heard the term "code coverage" refer to measuring _whether_ particular lines of code are executed, and not how many times they've been executed.
John Saunders
@John Saunders: IIRC gcov tells you how many times each line is executed. See: http://gcc.gnu.org/onlinedocs/gcc/Gcov-Intro.html#Gcov-Intro But, yes, you only really need to know *if* a line has been executed or not.
quamrana
@quamrana: was this a question about gcov? If so, then I missed that. I'm only familiar with about a half dozen other code coverage systems, none of which bother with anything beyond which lines are covered.
John Saunders
@John Saunders: No, you're right, its only about code coverage, but I was giving my definition, which was influenced by using gcov. I only have experience of about four different coverage programs and its probably true that of those only gcov counts execution times.
quamrana
@quamrana: No problem. The first coverage tool I used was a "Performance and Coverage Analyzer". It did both, using the same underlying technology.
John Saunders