English 中文(简体)
Is there a way to determine code coverage without running the code?
原标题:

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?

最佳回答

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.

问题回答

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!

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

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.

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.

If you are using Visual Studio, you can first run Analyze Code Coverage , Then you can export code Coverage results using below Button(marked in Green) in Visual Studio:

Image From Visual Studio

Later you can import the Coverage Result file back to Visual Studio





相关问题
State based testing(state charts) & transition sequences

I am really stuck with some state based testing concepts... I am trying to calculate some checking sequences that will cover all transitions from each state and i have the answers but i dont ...

Running genhtml using cygwin Perl.exe in Windows

I m trying to run genhtml using perl.exe from Cygwin in Windows. I have installed cygwin and placed genhtml in the bin directory of cygwin. I went to that directory and used the command line in ...

Highlight text from Visual Studio 2008 add-in

I m writing another code coverage tool for .NET with Visual Studio 2008 integration. Everything goes well except one thing: I can t find a way to highlight some code chunks. I need it to inform user ...

Cobertura ant script is missing Log4J classes

I tried to get Cobertura running inside my ant script, but I m stuck right at the beginning. When I try to insert the cobertura taskdef I m missing the Log4J libraries. Ant properties & ...

How to omit using python coverage lib?

I would like to omit some module that are in some particular directory : eggs and bin coverage -r -i --omit=/usr/lib/,/usr/share/,eggs,bin Name ...

Code coverage with nUnit? [closed]

Is there a way to see the code coverage when using nUnit? I know there s such a feature in Visual Studio, but can you use it with nUnit or only with the built-in vs unit tests?

run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

热门标签