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

如何用JaCoCo插件实现Cucumber功能测试覆盖率并设置质量门禁

How to Integrate JaCoCo with Cucumber for Coverage Reporting & Quality Gates

I’ve tackled this exact scenario before—getting Cucumber functional tests to feed into JaCoCo coverage just like unit tests, then locking it down with quality gates. Here’s a step-by-step breakdown that works for most Java projects (I’ll use Maven as the example; Gradle steps are similar with minor tweaks):

1. Configure Dependencies & JaCoCo Plugin

First, make sure your pom.xml has the necessary Cucumber and JaCoCo bits. You’ll need the Cucumber JUnit runner (to tie into your test lifecycle) and the JaCoCo Maven plugin set up to instrument code during tests.

Add these dependencies if you don’t have them already:

<dependencies>
    <!-- Cucumber Core & JUnit Integration -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.14.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit-platform-engine</artifactId>
        <version>7.14.0</version>
        <scope>test</scope>
    </dependency>
    <!-- JUnit 5 (required for Cucumber's JUnit platform engine) -->
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
        <version>1.10.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Then configure the JaCoCo Maven plugin to include both unit tests and Cucumber tests in coverage tracking. The key here is ensuring the plugin runs after all tests (unit + Cucumber) and instruments the code properly:

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.10</version>
            <executions>
                <!-- Instrument code before tests run -->
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <!-- Generate coverage report after all tests -->
                <execution>
                    <id>report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <!-- Optional: Enforce quality gates during the build -->
                <execution>
                    <id>check</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <!-- Define your coverage thresholds here -->
                            <rule>
                                <element>BUNDLE</element>
                                <limits>
                                    <limit>
                                        <counter>LINE</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.80</minimum> <!-- 80% line coverage -->
                                    </limit>
                                    <limit>
                                        <counter>BRANCH</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.70</minimum> <!-- 70% branch coverage -->
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. Ensure Cucumber Tests Are Included in the Test Lifecycle

Cucumber tests need to run as part of the Maven test phase so JaCoCo can capture their coverage. If you’re using JUnit 5, create a suite runner class in your src/test/java directory to trigger Cucumber tests:

import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;

@Suite
@IncludeEngines("cucumber")
@SelectPackages("com.yourpackage.cucumber") // Replace with your Cucumber step definitions package
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
public class CucumberTestSuite {
}

This class will make Maven run your Cucumber tests alongside unit tests during mvn test, so JaCoCo picks up coverage from both.

3. Verify Coverage Reporting

Run mvn clean verify to execute all tests and generate the JaCoCo report. You’ll find the HTML report at target/site/jacoco/index.html—open it up, and you should see coverage data from both your unit tests and Cucumber functional tests combined.

If you want separate reports for unit vs. Cucumber tests, you can configure multiple JaCoCo executions with different destFile paths, but most teams prefer a single merged report for quality gates.

4. Set Up Quality Gates

The check execution in the JaCoCo plugin (from step 1) will enforce your coverage thresholds during the build. If the coverage falls below the minimums you defined, the build will fail—this is your quality gate.

You can tweak the rules to target specific packages, classes, or metrics (line, branch, method, etc.). For example, if you want to enforce stricter coverage on core business logic packages:

<rule>
    <element>PACKAGE</element>
    <includes>
        <include>com.yourpackage.business.*</include>
    </includes>
    <limits>
        <limit>
            <counter>LINE</counter>
            <value>COVEREDRATIO</value>
            <minimum>0.90</minimum> <!-- 90% coverage for business logic -->
        </limit>
    </limits>
</rule>

Key Notes

  • Gradle Users: The setup is similar—use the JaCoCo plugin, ensure Cucumber tests are part of the test task, and configure jacocoTestCoverageVerification for quality gates.
  • Test Execution: Make sure your Cucumber tests are actually exercising the application code (not just stubbing everything)—otherwise, coverage numbers will be misleading.
  • JaCoCo Version: Always use the latest stable version of JaCoCo to avoid compatibility issues with newer Java/Cucumber versions.

内容的提问来源于stack exchange,提问作者Mohamed Ait Taleb

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 13:47:55