Cucumber can't find steps when running a single feature

天大地大妈咪最大 提交于 2019-12-04 15:42:56

It will only find files at the same level or lower in the features directory tree. So if you try to run just the scenarios in features/campaigns/donating_campaigns.feature it cannot find the step_definitions directory.

You can use the --require flag to explicitly tell cucumber what to include before it runs your feature. For example:

bundle exec cucumber --require features/step_definitions features/campaigns/donating_campaigns.feature
kaimonster

I had this problem in one of my projects. When I compared the cucumber.yml file of a functional project with the problematic one, I found that the functional file set -r features on the std_opts and rerun assignments. When I added that to the broken project, Cucumber could find my step definitions.

Here's my successful cucumber.yml file:

<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip -r features"
%>
default: <%= std_opts %> features
wip: --tags @wip:3 --wip features
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip -r features

Kudos to this coder for pointing me in the right direction: http://web.archive.org/web/20121026012412/http://old.thoughtsincomputation.com/posts/cucumber-step-definitions-and-autorequire-hell

I think you may be misunderstanding what step definitions are. In features/step_definitions is where you define your steps. So it looks like you have two features that contain scenarios you will or have written. Run the scenarios and you will see the step definitions you need to define. one for each line of your scenario. Now you need to define the steps in your campaign_steps.rb file using Capybara. For a fictitious example,

Given /^a user visits the campaign page$/ do
  visit campaign_path
end

When /^the user submits valid campaign information$/ do
  fill_in "Email",    with: @user.email
  fill_in "Donation", with: @user.donation 
  click_button "donate"
end

Cucumber used to come with web_steps.rb that defined many steps for you. However, that was changed and left out in recent versions. Hope this gets you started in the right direction. Stick with it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!