简体   繁体   English

如何为 JAR 文件生成 JaCoCo 报告?

[英]How do I generate a JaCoCo report for a JAR file?

We are trying to report code coverage of tests against a pre-packaged JAR file using JaCoCo.我们正在尝试使用 JaCoCo 报告针对预先打包的 JAR 文件的测试代码覆盖率。 To do this we start the JAR file using java -jar with the additional argument:为此,我们使用带有附加参数的java -jar启动 JAR 文件:

-javaagent:${project.basedir}/tools/jacocoagent.jar=output=tcpserver,port=${jacoco.port}

JaCoCo's Maven plugin is configured to then dump the execution file and report the results: JaCoCo 的 Maven 插件配置为转储执行文件并报告结果:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.0</version>
    <executions>
        <execution>
            <id>dumpData</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>dump</goal>
            </goals>
            <configuration>
                <address>localhost</address>
                <port>${jacoco.port}</port>
                <destFile>target/jacoco.exec</destFile>
            </configuration>
        </execution>
        <execution>
            <id>report</id>
            <phase>verify</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <dataFile>target/jacoco.exec</dataFile>
            </configuration>
        </execution>
    </executions>
</plugin>

This works to the point that a jacoco.exec file is generated which has a non-zero size (~350kB).这适用于生成具有非零大小 (~350kB) 的jacoco.exec文件。

But the report shows no coverage at all.但报告显示根本没有报道。 Following the "Sessions" link I can see the classes in the JAR listed, but the report's home page shows this:在“会话”链接之后,我可以看到 JAR 中列出的类,但报告的主页显示如下: JaCoCo报告

Based on the logs we have the code seems to be exercised.根据日志,我们的代码似乎已被执行。 Is there a step missing in the JaCoCo setup or should this work? JaCoCo 设置中是否缺少一个步骤,或者这应该有效吗?

Here are the steps to achieve this:以下是实现此目的的步骤:

  1. Run the java application in a separate JVM with -javaagent parameter passed.在传递-javaagent参数的情况下,在单独的 JVM 中运行 java 应用程序。

  2. Run your tests against the application(like calling an endpoint on the web server, etc.)针对应用程序运行测试(例如调用 web 服务器上的端点等)

  3. Run the dump and report goals to generate the report.运行dumpreport目标以生成报告。

You can check out my article on the this topic.你可以查看我关于这个主题的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM