当前位置: 首页 > 知识库问答 >
问题:

maven Jacoco:不生成代码覆盖报告

钮博裕
2023-03-14

我试图设置Jacoco为我的项目的代码覆盖

我的项目基于Java1.8

下面是我项目的pom.xml

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.5.10.201208310627</version>
        <configuration>
            <output>file</output>
            <append>true</append>
        </configuration>
        <executions>
            <execution>
                <id>jacoco-initialize</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
            <execution>
                <id>jacoco-site</id>
                <phase>verify</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
        </executions>
    </plugin>  

然后我运行mvn test并看到以下内容

$ mvn test
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building pennyapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- jacoco-maven-plugin:0.5.10.201208310627:prepare-agent (jacoco-initialize) @ pennyapp ---
[INFO] argLine set to -javaagent:/Users/harit/.m2/repository/org/jacoco/org.jacoco.agent/0.5.10.201208310627/org.jacoco.agent-0.5.10.201208310627-runtime.jar=destfile=/Users/harit/code/idea/pennyapp/target/jacoco.exec,append=true,output=file
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ pennyapp ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pennyapp ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/harit/code/idea/pennyapp/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pennyapp ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/harit/code/idea/pennyapp/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ pennyapp ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/harit/code/idea/pennyapp/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ pennyapp ---
[INFO] Surefire report directory: /Users/harit/code/idea/pennyapp/shippable/testresults
[INFO] Using configured provider org.apache.maven.surefire.junit4.JUnit4Provider

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
objc[13225]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Running HelloTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.1 sec - in HelloTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.523 s
[INFO] Finished at: 2014-08-19T17:56:33-07:00
[INFO] Final Memory: 10M/119M
[INFO] ------------------------------------------------------------------------

然后我运行mvn Jacoco: Report,我看到

$ mvn jacoco:report
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building pennyapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- jacoco-maven-plugin:0.5.10.201208310627:report (default-cli) @ pennyapp ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.176 s
[INFO] Finished at: 2014-08-19T17:56:51-07:00
[INFO] Final Memory: 11M/112M
[INFO] ------------------------------------------------------------------------

问题
-配置中有什么不正确
-如何生成报告?

谢啦

共有3个答案

贡建修
2023-03-14

这对我很有效:

mvn clean install
mvn site

即使没有满足最低代码覆盖率,并且mvn清洁安装失败,mvn站点构建成功并在以下位置创建了覆盖率报告:

.../target/site/jacoco/index.html
齐昊
2023-03-14

JaCoco Maven插件正在重写Surefire argLine,如果您还需要重写argLine,请确保保留argLine变量:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <argLine>-Xmx1024M ${argLine}</argLine> 
  </configuration>
</plugin>

注意:您可以更改此属性名称,如jacoco插件文档中所述。

吴西岭
2023-03-14

您使用JaCoCo插件版本过低的任何特殊原因?对于Java8支持,您必须至少使用0.7.0版本(请参阅变更日志)。

在您的配置中,报告目标绑定到验证阶段,因此运行mvn test不会生成任何报告,因为它不会运行验证阶段(测试阶段在验证之前)。您必须使用mvn verify执行测试并生成报告。

JaCoCo项目提供了Maven配置示例。您可以尝试“这个JAR项目的POM文件在代码覆盖率下运行JUnit测试并创建覆盖率报告”。

 类似资料:
  • 使用 Robolectric 和 Android 生成代码覆盖率(测试)报告 原文链接 : Code coverage reports using Robolectric and Android 原文作者 : Kris Vandermast 译文出自 : 开发技术前线 www.devtf.cn 译者 : normalme 校对者: Mr.Simple 状态 : 校对中 我写过许多测试驱动开发与陷阱

  • 我正在使用Jacoco:报告标签生成Jacoco报告。我收到了以下错误: ant报告目标如下所示: 是使用<code>生成的/仅代码/src。那么为什么会出现这样的错误呢。知道吗?

  • 有人知道是否有可能(以及如何)在Postman集成测试执行中生成代码覆盖率吗? 我们有基于Postman的集成测试,并通过Windows shell命令在Jenkins上执行。令人沮丧的是,我们不知道如何生成代码覆盖报告并将其发布到SonarQube中。 提前万分感谢!

  • 免责声明初学者问题! 为了回答这个问题,我的项目结构高度简化,如下所示: 在阅读了Jeff Knupp关于单元测试的博客文章并写了一系列测试之后,我想看看我的代码现在被测试覆盖了多少。所以我安装了coverage.py,以下内容让我困惑: $coverage运行main。py(显示脚本中的打印/日志) $覆盖报告main.py 姓名、Stmts、小姐、封面 主要的py,114,28,75% 问题是

  • 代码覆盖是查找未被测试执行的代码区域的过程。不过要记住的是这并不能说明你测试代码的有效性。 在requirements.txt文件中添加依赖包: coverage==4.4.2 然后,我们在manage.py中新增一个命令: import coverage COV = coverage.coverage( branch=True, include='project/*',