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

如何为Cucumber StepDef指定源Jar包?多版本配置方法

Using @CucumberOptions glue to select StepDef versions from different Jars

Great question! To handle having two versions of the same StepDef in separate Jars and use them for different scenarios, you can leverage Cucumber's glue parameter along with tags and separate test runners. Here's a step-by-step breakdown:

1. Organize StepDefs into distinct packages in each Jar

First, make sure the StepDefs in each Jar live in unique packages. This is critical because Cucumber scans packages specified in glue to find StepDefs—if both versions are in the same package, Cucumber will pick all of them, leading to conflicts.

  • Version 1 Jar: Place all StepDef classes in a package like com.yourcompany.stepdefs.v1
  • Version 2 Jar: Place all StepDef classes in a package like com.yourcompany.stepdefs.v2

Both versions can have identical step names (e.g., Given I perform an action), but their implementations will differ based on the Jar version.

2. Create separate test runner classes for each version

You'll need two test classes: one to run scenarios using Version 1 StepDefs, and another for Version 2. Each will target its specific Jar's package via the glue parameter, and use tags to filter the relevant scenarios.

Test runner for Version 1 scenarios:

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features", // Path to your feature files
    glue = "com.yourcompany.stepdefs.v1", // Points directly to Version 1's StepDef package
    tags = "@Version1", // Runs only scenarios tagged with @Version1
    plugin = {"pretty", "html:target/cucumber-v1-report.html"}
)
public class Version1TestRunner {
}

Test runner for Version 2 scenarios:

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features",
    glue = "com.yourcompany.stepdefs.v2", // Points directly to Version 2's StepDef package
    tags = "@Version2", // Runs only scenarios tagged with @Version2
    plugin = {"pretty", "html:target/cucumber-v2-report.html"}
)
public class Version2TestRunner {
}

3. Tag your feature file scenarios

Add tags to your feature files to group scenarios that should use each StepDef version:

Feature: Sample Feature

@Version1
Scenario: Execute with StepDef Version 1
  Given I perform an action
  Then I should see the version 1 result

@Version2
Scenario: Execute with StepDef Version 2
  Given I perform an action
  Then I should see the version 2 result

4. Key notes to avoid issues

  • Classpath setup: Ensure both Jars are included as dependencies in your project (e.g., via Maven/Gradle) so Cucumber can access the StepDef classes.
  • Shared StepDefs: If you have common StepDefs used by both versions, add their package to the glue parameter as a comma-separated list: glue = {"com.yourcompany.stepdefs.common", "com.yourcompany.stepdefs.v1"}
  • Package accuracy: Double-check that the package names in glue exactly match the packages in each Jar—even a small typo will prevent Cucumber from finding the StepDefs.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:08:09