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

Cucumber Feature文件无法识别step文件,咨询命名规范与路径配置

Troubleshooting Cucumber Step Definition Not Found Issue

Hey there! I totally get how frustrating this can be when you’ve put in the work to create step files but Cucumber still acts like they don’t exist. Let’s walk through the most common fixes for your scenario:

1. Check Step File Naming & Directory Structure

Cucumber relies on default conventions to find step definitions, and straying from these can make it miss your files:

  • Required Directory: By default, Cucumber looks for step definitions in a step_definitions folder that’s a direct child of your features directory. Since your feature file lives at test/features/test.feature, your step files should be placed at test/features/step_definitions/ (create this folder if it doesn’t exist yet).
  • File Extension: Step files must end with .rb (e.g., test_steps.rb — the actual filename doesn’t matter as long as it has the correct extension).

Your ideal directory structure should look like this:

your-project/
├── test/
│   ├── features/
│   │   ├── test.feature
│   │   └── step_definitions/
│   │       └── test_steps.rb

2. Manually Specify Step Definition Path

If you’ve placed your step files outside the default step_definitions folder, you need to explicitly tell Cucumber where to find them using the --require flag when running the command. For example:

cucumber test/features --require test/features/your-custom-step-folder

Or if your step files are in a completely different location (like lib/step_definitions):

cucumber test/features --require lib/step_definitions

To avoid typing this every time, create a cucumber.yml configuration file in your project root with default settings:

default: --require test/features/step_definitions test/features

Now you can just run cucumber in your terminal, and it’ll use these settings automatically.

3. Verify Step Definition Matching

Even if your files are in the right place, Cucumber won’t recognize steps if the regex in your step definition doesn’t exactly match the text in your .feature file. For example:

  • If your feature has:
    Given I am on the test page
    
  • Your step definition should use a matching regex (or the more readable string literal syntax):
    # Option 1: Regex syntax
    Given /^I am on the test page$/ do
      # Your step logic here
    end
    
    # Option 2: String literal syntax
    Given("I am on the test page") do
      # Your step logic here
    end
    

Double-check for typos, extra spaces, or mismatched phrasing between your feature and step files.

4. Confirm Version Compatibility

Since you’re using Cucumber 3.1.0 (a slightly older version), make sure your step definition syntax aligns with this release. If you’re following a more recent tutorial, avoid syntax introduced in newer Cucumber versions (like updated parameter type handling) that might not work with your setup.

Give these fixes a try, and I bet you’ll get Cucumber recognizing your steps in no time!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:16:17