Cucumber-JVM与Cucumber JUnit Runner运行差异及配置疑问
Hey there! Let me clear up this behavior for you—it’s absolutely the default behavior in IntelliJ when working with Cucumber, and I’ve run into this exact same confusion when starting out too.
Why the Difference in Execution?
When you right-click a single .feature file and run it, IntelliJ uses the Cucumber-JVM core runner directly. This bypasses the JUnit test framework entirely, which means your CucumberConfig (with @RunWith and @CucumberOptions) and custom CustomRunner never get invoked—they’re tied to JUnit’s execution pipeline, not the raw Cucumber-JVM runner.
On the other hand, when you run the entire test suite (or specifically run your CucumberConfig class), IntelliJ triggers JUnit’s test runner. JUnit recognizes the @RunWith(CustomRunner.class) annotation, instantiates your custom runner, and applies all the config from CucumberOptions and your @BeforeClass/@AfterClass hooks.
Correcting Your Assumption
You mentioned "using cucumber-junit时无法像cucumber-jvm那样指定单个Feature运行"—that’s not actually true! You absolutely can run a single Feature via Cucumber-JUnit, you just need to adjust how you trigger the run.
How to Make Cucumber Always Use Your Config
Here are a few reliable ways to ensure your CucumberConfig and custom runner are always used, even when running individual Features:
Run via Your CucumberConfig Class (with Targeted Features)
- Instead of running the
.featurefile directly, right-click yourCucumberConfigclass and select "Run". - To target a single Feature, modify the
featuresparameter in your@CucumberOptionsannotation:@CucumberOptions(features = "src/test/resources/features/your_specific_feature.feature") - Or, if you don’t want to hardcode the path, edit the run configuration after launching it: go to Run > Edit Configurations, find your CucumberConfig run config, and add
--features src/test/resources/features/your_specific_feature.featureto the "Program arguments" field.
- Instead of running the
Use Cucumber Tags to Filter Runs
- Add a unique tag to the Feature or scenario you want to run, like:
@single-feature Feature: My Target Feature Scenario: ... - Update your
CucumberOptionsto include this tag:@CucumberOptions(tags = "@single-feature") - Now when you run
CucumberConfig, only the tagged Feature/scenario will execute, and your custom runner/config hooks will all fire as expected.
- Add a unique tag to the Feature or scenario you want to run, like:
Customize IntelliJ’s Cucumber Run Template
- Open Run > Edit Configurations, click the "+" button, and select "JUnit".
- Set the "Test kind" to "Class", and select your
CucumberConfigclass. - Save this as a template, so every time you want to run a Feature, you can duplicate this config and adjust the
featurespath or tags in the arguments. This ensures you always use the JUnit-backed runner.
Your Code Examples (For Reference)
Just to make sure we’re on the same page, here’s your code formatted properly:
CucumberConfig Class
@RunWith(CustomRunner.class) @CucumberOptions() public class CucumberConfig { @BeforeClass public static void beforeClass() { System.out.println("This is run before Once: "); } @AfterClass public static void afterClass() { System.out.println("This is run after Once: "); } }
CustomRunner Class
public class CustomRunner extends Cucumber { public CustomRunner(Class clazz) throws InitializationError, IOException { super(clazz); System.out.println("I am in the custom runner."); } }
内容的提问来源于stack exchange,提问作者user3275095

