Ruby + Cucumber: How to execute cucumber in code?

2019-04-20 23:25发布

I'd like to execute Cucumber features from within Ruby code.

Typically the cucumber binary installed with the gem is executed on the command line with one or more features specified.

However, I'd like to define logic that creates a dynamic feature execution flow. In other words, the program can work out which features should be executed.

Is it possible to instantiate Cucumber with specified feature files from Ruby code as opposed to the command line?

2条回答
叛逆
2楼-- · 2019-04-20 23:28

I discovered how from the mailing list and some API reading.

features="path/to/first.feature path/to/second.feature"
runtime = Cucumber::Runtime.new 
runtime.load_programming_language('rb') 
Cucumber::Cli::Main.new([features]).execute!(runtime)

If you want all features within your gem's features/ directory to be executed, pass an empty array to Main.new instead.

查看更多
在下西门庆
3楼-- · 2019-04-20 23:52

To convert this example command, with features and options specified:

cucumber features/first.feature features/second.feature -d -f Cucumber::Formatter::Custom

into Ruby code, it boils down to passing Cucumber an args array:

require 'cucumber'

# Method 1 - hardcoded features
args = %w(features/first.feature features/second.feature -d -f Cucumber::Formatter::Custom)

# Method 2 - dynamic features
features = 'features/first.feature features/second.feature'
args = features.split.concat %w(-d -f Cucumber::Formatter::Custom)

# Run cucumber
begin
  Cucumber::Cli::Main.new(args).execute!
rescue SystemExit
  puts "Cucumber calls @kernel.exit(), killing your script unless you rescue"
end

Tested using Ruby 2.0.0p598 and Cucumber 1.3.17

查看更多
登录 后发表回答