Exclude methods from code coverage with Cobertura

后端 未结 5 1633
情话喂你
情话喂你 2020-11-27 14:22

Is there a way to exclude code from inclusion into Cobertura coverage reports? We have some methods that should not be included in the coverage report and therefore not driv

相关标签:
5条回答
  • 2020-11-27 14:52

    Cobertura doesn't currently provide such a feature, and neither does Emma (which we use) although it is listed as a forthcoming enhancement - although in the form of an extension to the exclusion rules I believe rather than as an annotation.

    Would be handy to cover off those few inaccessible corners cleanly so that you can strive for 100% without being ridiculous.

    I think annotations would probably be a friendlier way to do it, but they ought to be fairly explicitly named and based on a list of acceptable scenarios as I fear otherwise something like '@ExcludeFromCoverage' would get added over generously.

    0 讨论(0)
  • 2020-11-27 14:55

    Remember to exclude inner classes too.

    <exclude>path/to/class/MyClass*.class</exclude>
    

    It took me ages to notice I was missing an asterisk!

    0 讨论(0)
  • 2020-11-27 15:05

    This has been breaking my head for some time now.

    My problem was that I had the cobertura maven plugin setup in the reporting section instead of the build section.

    The instrumentation settings, and hence the excluding of classes or packages, won't be applied if you don't set it up on build section, so watch out for this.

    0 讨论(0)
  • 2020-11-27 15:15

    Since 2.0 you can write your own @CoverageIgnore annotation.

    It will be recognized by Cobertura, which will avoid considering annotated methods (does not work on classes as far as I know).

    1. Create an empty annotation:
    public @interface CoverageIgnore {}
    
    1. Then annotate the methods you want to exclude from the reports:
    public class SomeClass {
        @CoverageIgnore
        public void foo(String baz) {
            // useless stuff
        }
    }
    

    Source: https://github.com/cobertura/cobertura/wiki/Coverage-Annotations

    0 讨论(0)
  • 2020-11-27 15:17

    You can exclude classes from instrumentation. Then they should not appear on reports. See exclude statements below.

    You can also ignore calls to some methods. See ignore statement below.

    If you are using maven, see maven plugin manual.

        <configuration>
          <instrumentation>
            <ignores>
              <ignore>com.example.boringcode.*</ignore>
            </ignores>
            <excludes>
              <exclude>com/example/dullcode/**/*.class</exclude>
              <exclude>com/example/**/*Test.class</exclude>
            </excludes>
          </instrumentation>
        </configuration>
    

    And for ant see this.

    <cobertura-instrument todir="${instrumented.dir}">
      <ignore regex="org.apache.log4j.*" />
      <fileset dir="${classes.dir}">
        <include name="**/*.class" />
        <exclude name="**/*Test.class" />
      </fileset>
      <fileset dir="${jars.dir}">
        <include name="my-simple-plugin.jar" />
      </fileset>
    </cobertura-instrument>
    
    0 讨论(0)
提交回复
热议问题