Java 如何从 EclEmma 的覆盖率计算中排除类而不实际将它们从覆盖率本身中排除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37004345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:54:25  来源:igfitidea点击:

How to exclude classes from the coverage calculation in EclEmma without actually excluding them from the coverage itself

javaeclipseunit-testingcode-coverageeclemma

提问by Programmer1994

I am using EclEmma to test the coverage of my scenario tests and use case tests on my project. I have a Base package which contains the most general classes and the use case tests. The coverage looks like this:

我正在使用 EclEmma 来测试我的场景测试和用例测试的覆盖范围。我有一个 Base 包,其中包含最通用的类​​和用例测试。覆盖范围如下所示:

Code coverage in our project

我们项目中的代码覆盖率

What I want is to exclude the use case tests (e.g. BugReportTest) from the coverage calculation. But I do want the tests inside it to be considered. I know how to exclude the entire class from the coverage but if I do that, my coverage % drops because the actual tests that check which lines of my code are tested are forgotten. These use case tests do need to stay in the Base package because of privacy reasons.

我想要的是从覆盖率计算中排除用例测试(例如 BugReportTest)。但我确实希望考虑其中的测试。我知道如何从覆盖率中排除整个类,但如果我这样做,我的覆盖率 % 会下降,因为检查我的代码的哪些行被测试的实际测试被遗忘了。由于隐私原因,这些用例测试确实需要保留在 Base 包中。

采纳答案by SkyWalker

For technical reasons it might be necessary to exclude certain classes from code coverage analysis. The following options configure the coverage agent to exclude certain classes from analysis. Except for performance optimization or technical corner cases these options are normally not required.

出于技术原因,可能需要从代码覆盖率分析中排除某些类。以下选项将覆盖代理配置为从分析中排除某些类。除了性能优化或技术极端情况外,通常不需要这些选项。

  1. Excludes:A list of class names that should be excluded from execution analysis. The list entries are separated by a colon (:)and may use wildcard characters (* and ?). (Default: empty)

  2. Exclude classloaders:A list of class loader names that should be excluded from execution analysis. The list entries are separated by a colon (:)and may use wildcard characters (* and ?). This option might be required in case of special frameworks that conflict with JaCoCo code instrumentation, in particular class loaders that do not have access to the Java runtime classes. (Default: sun.reflect.DelegatingClassLoader)

  1. 排除:应从执行分析中排除的类名列表。列表条目由 a 分隔colon (:),可以使用wildcard characters (* and ?). (Default: empty)

  2. 排除类加载器:应从执行分析中排除的类加载器名称列表。列表条目由 a 分隔colon (:),可以使用wildcard characters (* and ?). 如果特殊框架与 JaCoCo 代码检测冲突,则可能需要此选项,特别是无权访问 Java 运行时类的类加载器。(Default: sun.reflect.DelegatingClassLoader)

Warning: Use these options with caution! Invalid entries might break the code coverage launcher. Also do not use these options to define the scope of your analysis. Excluded classes will still show as not covered.

警告:谨慎使用这些选项!无效的条目可能会破坏代码覆盖启动器。也不要使用这些选项来定义分析范围。排除的课程仍将显示为未涵盖。

Resource Link:

资源链接:

  1. EclEmma Code Coverage Preferences
  1. EclEmma 代码覆盖率首选项


The following examples all specify the same set of inclusion/exclusion patterns:

以下示例都指定了一组相同的包含/排除模式:

  1. <filter includes="com.foo.*" excludes="com.foo.test.*, com.foo.*Test*" />
  2. <filter includes="com.foo.*" /> <filter excludes="com.foo.test.*, com.foo.*Test*" />
  3. <filter value="+com.foo.*, -com.foo.test.*, -com.foo.*Test*" />
    <filter excludes="com.foo.*Test*" file="myfilters.txt" />
    where myfilters.txt file contains these lines:

    -com.foo.test.* +com.foo.*

  1. <filter includes="com.foo.*" excludes="com.foo.test.*, com.foo.*Test*" />
  2. <filter includes="com.foo.*" /> <filter excludes="com.foo.test.*, com.foo.*Test*" />
  3. <filter value="+com.foo.*, -com.foo.test.*, -com.foo.*Test*" />
    <filter excludes="com.foo.*Test*" file="myfilters.txt" />
    其中 myfilters.txt 文件包含以下几行:

    -com.foo.test.* +com.foo.*

Resource Link:

资源链接:

  1. Coverage filters
  2. I am certain that all of my classes are built with -g(debug='true') and yet EMMA still complains about missing debug info!
  1. 覆盖过滤器
  2. 我确信我所有的类都是用 -g(debug='true') 构建的,但 EMMA 仍然抱怨缺少调试信息!


Ignore code coverage for unit tests in EclEmma

忽略 EclEmma 中单元测试的代码覆盖率

Preferences->Java->Code Coverageand set the "Only path entries matching"option to src/main/java- seems to work nicely

Preferences->Java->Code Coverage并将"Only path entries matching"选项设置为src/main/java- 似乎工作得很好