Configure cobertura to ignore certain blocks of code
Asked Answered
C

2

5

Is it possible using Cobertura to tell it to ignore certain bits of code that are identified by start and end comments?

For example,

public class Foo {

    public void static doSomething() {
        ...
        // Cobertura-Ignore-Start
        ...
        // Cobertura-Ignore-End
}

would result in Cobertura not including the code in between the start and end comments when calculating coverage statistics.

Edit: I am using the cobertura Ant task.

Clamshell answered 20/8, 2010 at 2:19 Comment(6)
why would you want to do this?Costive
Plenty of reasons but I'll give an example. Let's say you have a test class and the code looks like this: public void testSomething() { try { doSomething(); fail(); } catch (Exception e) { // check exception} } - so essentially this is testing to make sure that doSomething() will throw an exception however fail(); will never be called unless your test is actually broken. So if you want 100% test coverage and if all your tests pass as they should, the line containing fail(); would need to be denoted as a line that should not be checked by cobertura. Hopefully that made some sense.Clamshell
How will fail not getting called affects your code coverage... as this is part of test class...Pokey
Coverage should be something that you run over your test code as well. You don't want stale dead code lying around in your code base. Test code is a first class citizen as well as production code. We have 100% coverage on our system as long as we can identify areas such as fail(); and tell cobertura not to look at that stuff which obviously would not be something that would get called in a working build.Clamshell
Furthermore, when you have 100% coverage, it is easy to see dead code (i.e. when your coverage drops below 100% - in either production or test code)Clamshell
Another example of why this is a needed feature: if(LOG.isDebugEnabled()) LOG.debug("blabla");Contributory
N
5

No, it's not possible. Cobertura does not have a feature that lets it skip over code. The only thing you can ignore is method calls to certain packages and/or classes.

You'd probably have to dig into the code and see whether it's possible to extend it in such a way as to ignore blocks.

Nada answered 20/8, 2010 at 6:22 Comment(0)
G
0

You can ignore at the class level only as far as I know (which means any method from the ignored classes is not counted, it is instrumented though so no speed up by doing so).

Gainly answered 20/8, 2010 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.