You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jacoco:如何为外部端到端/功能测试统计代码覆盖率?

Absolutely you can track code coverage for your external end-to-end/functional tests in this setup! Since you're already familiar with EclEmma (which is built on JaCoCo), you’ll be glad to know JaCoCo is the perfect tool for this scenario too. Here’s a step-by-step guide tailored to your Maven + JAR deployment workflow:

1. Configure Maven to integrate JaCoCo agent support

First, update your pom.xml to include the JaCoCo Maven plugin. This lets you set up the necessary hooks to capture coverage data, either during packaging or runtime:

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.11</version> <!-- Use the latest stable version -->
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
2. Deploy your app with the JaCoCo agent attached

When starting your deployed JAR, pass JVM arguments to load the JaCoCo agent. This agent runs alongside your app and records every code path executed during your tests. Use a command like this:

java -javaagent:/path/to/jacocoagent.jar=destfile=/tmp/jacoco.exec,output=tcpserver,port=6300 -jar your-deployed-app.jar

Key parameter breakdown:

  • destfile: The file where final coverage data will be saved
  • output=tcpserver: Configures the agent to listen on a TCP port, so you can remotely dump coverage data after tests finish
  • port=6300: The port the agent listens on (pick one open on your server)
3. Run your external end-to-end/functional tests

Execute your tests as usual—let them send requests to your deployed app. The JaCoCo agent will quietly track all code that gets executed during these test runs.

4. Dump coverage data and generate reports

Once tests are complete, connect to the JaCoCo agent’s port to export the coverage data to your specified destfile. Use JaCoCo’s CLI tool for this:

java -jar jacococli.jar dump --address your-app-server-ip --port 6300 --destfile /tmp/jacoco.exec

Then run the Maven JaCoCo report goal to generate familiar HTML/XML reports (just like EclEmma’s output):

mvn jacoco:report -Djacoco.dataFile=/tmp/jacoco.exec

Reports will be generated in target/site/jacoco/ by default—open index.html to view detailed coverage metrics.

Extra Tips
  • Ensure the JaCoCo agent’s port is open on your server’s firewall so you can connect to dump data.
  • Automate these steps in your CI/CD pipeline: build with JaCoCo support, deploy with the agent, run tests, dump data, and generate reports as part of your workflow.
  • Since JaCoCo powers EclEmma, the report format will feel familiar—no steep learning curve!

内容的提问来源于stack exchange,提问作者tryingToLearn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:20:44