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.

digiarnie
  • 22,305
  • 31
  • 78
  • 126
  • why would you want to do this? – matt b Aug 20 '10 at 02:31
  • 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. – digiarnie Aug 20 '10 at 03:47
  • How will fail not getting called affects your code coverage... as this is part of test class... – Ankit Bansal Aug 20 '10 at 04:26
  • 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. – digiarnie Aug 20 '10 at 04:50
  • 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) – digiarnie Aug 20 '10 at 04:59
  • Another example of why this is a needed feature: if(LOG.isDebugEnabled()) LOG.debug("blabla"); – Guillaume Feb 24 '11 at 09:53

2 Answers2

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.

aberrant80
  • 12,815
  • 8
  • 45
  • 68
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).

TofuBeer
  • 60,850
  • 18
  • 118
  • 163