java Eclemma 总是报告 0% 的代码覆盖率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13932211/
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
Eclemma always reporting 0% of code coverage
提问by renanleandrof
I have a Maven test project for my application.
我的应用程序有一个 Maven 测试项目。
The JUnit tests run fine, and the code coverage test run too.
JUnit 测试运行良好,代码覆盖率测试也运行良好。
But the report always shows 0% of code coverage.
但该报告始终显示代码覆盖率为 0%。
What should i do?
我该怎么办?
回答by Joao Piccinini
According to the official site, Eclemmais a code coverage plugin for Eclipse, based on JaCoCo library.
根据官方网站,Eclemma是 Eclipse 的代码覆盖插件,基于 JaCoCo 库。
As you want to use the same code coverage engine outside eclipse, you should include the plugin Jacocoinside the Maven configuration (pom) of your project, as the following (this code was copied from the Agile Engineering blog):
由于您想在 eclipse 之外使用相同的代码覆盖引擎,您应该在项目的 Maven 配置 (pom) 中包含插件Jacoco,如下所示(此代码是从 Agile Engineering 博客复制的):
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.0.201210061924</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
To run the tests just type the following on the command line tool:
要运行测试,只需在命令行工具上键入以下内容:
mvn clean test
p.s.: you also could use other code coverage plugins like Coberturaor Emma.
回答by Joao Piccinini
Now I realized that you just want to get a report using the tool inside Eclipse...
现在我意识到您只想使用 Eclipse 中的工具获取报告...
How is the code coverage in the Eclipse Dialog? Did you tried to use the mouse right click on this dialog to export session (report), or inside File -> Export?
Eclipse 对话框中的代码覆盖率如何?您是否尝试使用鼠标右键单击此对话框以导出会话(报告),或在File -> Export 中?
回答by KrishPrabakar
It's a known issuefor many years and unfortunately there's no official solution yet for it.
这是一个多年来的已知问题,不幸的是,目前还没有官方解决方案。
You can see it here, hereand here
One not-so-honey solution might be to try using eCobertura(or downgrading eclemma from 2.x to 1.x)
一种不太好的解决方案可能是尝试使用eCobertura(或将 eclemma 从 2.x 降级到 1.x)
回答by Steve
Just in case you forgot to do these:
以防万一您忘记执行以下操作:
- Are you annotating your tests using @Test?
- Are you running the class as a JUnit test case or from the coverage button?
- 您是否使用@Test 注释您的测试?
- 您是将类作为 JUnit 测试用例还是从覆盖率按钮运行?
I'm not sure what the cause of the problem is, cause it always worked for me. Have you installed it from eclipse itself? Try to uninstall it, and reinstall from eclipse. Here's how to do it just in case:
我不确定问题的原因是什么,因为它总是对我有用。您是否从 eclipse 本身安装了它?尝试卸载它,然后从 eclipse 重新安装。以防万一,请执行以下操作:
- In Eclipse, Click "Help" > "Install new Software"
- Click "Add", and type the following:
- Name: EclEmma (or any name you want)
- Path: http://update.eclemma.org/
- Select EclEmma, and install
- 在 Eclipse 中,单击“帮助”>“安装新软件”
- 单击“添加”,然后键入以下内容:
- 姓名:EclEmma(或任何你想要的名字)
- 路径:http: //update.eclemma.org/
- 选择 EclEmma,并安装
回答by swaroop
If you are using eclemma, you need to add jacoco dependency. if jacoco has been added and still, you are facing this issue, refer the eclemma faq: "Why does a class show as not covered although it has been executed?"
如果您使用的是 eclemma,则需要添加 jacoco 依赖项。如果已添加 jacoco 并且您仍然面临此问题,请参阅eclemma 常见问题解答:“为什么一个类虽然已执行,但仍显示为未覆盖?”
it says,
它说,
First make sure execution data has been collected. For this select the Sessions link on the top right corner of the HTML report and check whether the class in question is listed. If it is listed but not linked the class at execution time is a different class file. Make sure you're using the exact same class file at runtime as for report generation. Note that some tools (e.g. EJB containers, mocking frameworks) might modify your class files at runtime.
首先确保已收集执行数据。为此,请选择 HTML 报告右上角的“会话”链接,并检查是否列出了相关类。如果已列出但未链接,则在执行时该类是不同的类文件。确保在运行时使用与生成报告完全相同的类文件。请注意,某些工具(例如 EJB 容器、模拟框架)可能会在运行时修改您的类文件。
So, Mockito / PowerMockito can cause this problem. In my case, I have added the class in @PrepareForTest(). I was shown that the test case was executed fine without errors but Jacoco did't improve the code coverage in its report.
因此,Mockito / PowerMockito 可能会导致此问题。就我而言,我在@PrepareForTest() 中添加了该类。我看到测试用例执行得很好,没有错误,但 Jacoco 没有提高其报告中的代码覆盖率。
Finally, removing the class from @PrepareForTest() annotation improved the code coverge. check if you have added it or not and remove it from annotation if added.
最后,从@PrepareForTest() 注释中删除该类提高了代码覆盖率。检查您是否添加了它,如果添加,则将其从注释中删除。
回答by slocumro
I just came across this issue and it was caused by an incorrectly configured classpath. When the unit tests were executed, they were executing against a compiled jar (actual source compiled outside of eclipse) and not my actual source code. After removing the jar from my classpath, the unit tests correctly hit my package source.
我刚遇到这个问题,它是由错误配置的类路径引起的。执行单元测试时,它们是针对已编译的 jar(在 eclipse 之外编译的实际源代码)而不是我的实际源代码执行的。从我的类路径中删除 jar 后,单元测试正确地命中了我的包源。
回答by Westin Christensen
I was able to resolve the issue on mine by calling a instance of the class at the top of the test cases. i.e.
我能够通过在测试用例顶部调用类的实例来解决我的问题。IE
public hotelOccupancy hotel = new hotelOccupancy();
@Test
public void testName() {
// some test here
}
Once I did that all my coverage began working and the issues were resolved.
一旦我这样做了,我所有的报道就开始工作,问题也得到了解决。
回答by Kurohige
I'm using eclemma 2.3.2 and it's working perfectly on eclipse
我正在使用 eclemma 2.3.2,它在 eclipse 上运行良好
I only need to add these dependencies in my pom.xml
我只需要在我的 pom.xml 中添加这些依赖项
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-jacoco</artifactId>
<version>1.0.0.Alpha6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.core</artifactId>
<version>0.7.1.201405082137</version>
<scope>test</scope>
</dependency>
Then I build the project, update maven projects configuration and run coverage plugin as expected
然后我构建项目,更新 Maven 项目配置并按预期运行覆盖插件