Ruby + Cucumber:如何在代码中执行黄瓜?

我想从Ruby代码中执行Cucumberfunction。

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

但是,我想定义创建动态function执行流程的逻辑。 换句话说,程序可以确定应该执行哪些function。

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

我从邮件列表和一些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) 

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

要转换此示例命令,请使用指定的function和选项:

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

进入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 

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