Ruby + Cucumber: How to execute cucumber in code?

时间:2021-08-23 23:15:04

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

我想从Ruby代码中执行Cucumber功能。

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

通常,与gem一起安装的黄瓜二进制文件在命令行上执行,并指定一个或多个功能。

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?

是否可以使用Ruby代码中的指定功能文件来实例化Cucumber而不是命令行?

2 个解决方案

#1


11  

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

我从邮件列表和一些API阅读中发现了如何。

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.

如果要执行gem的features /目录中的所有功能,请将空数组传递给Main.new。

#2


3  

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:

进入Ruby代码,归结为将Cucumber传递给args数组:

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

使用Ruby 2.0.0p598和Cucumber 1.3.17进行测试

#1


11  

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

我从邮件列表和一些API阅读中发现了如何。

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.

如果要执行gem的features /目录中的所有功能,请将空数组传递给Main.new。

#2


3  

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:

进入Ruby代码,归结为将Cucumber传递给args数组:

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

使用Ruby 2.0.0p598和Cucumber 1.3.17进行测试