我希望能够在Ruby代码中执行Cucumber功能。
通常使用与gem安装的cucumber
二进制文件,在命令行上指定一个或多个功能来执行。
然而,我想定义一些逻辑来创建动态的功能执行流。换句话说,程序可以确定应该执行哪些功能。
是否可能从Ruby代码中实例化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)
如果你希望执行宝石中 features/
目录下所有的功能,请将一个空数组传递给 Main.new
。
将指定了功能和选项的示例命令转换为:
cucumber features/first.feature features/second.feature -d -f Cucumber::Formatter::Custom
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进行了测试。
[features]
是作为一个args
参数传递的。这个参数会被cucumber/cli/configuration解析,使用cucumber/cli/options类。所以在我的例子中,你可能只需要将参数附加到features
字符串的末尾。请确认是否有效。 - KomodoDaveinitialize' C:/Ruby200/lib/ruby/gems/2.0.0/gems/cucumber-1.3.16/lib/cucumber/feature_file.rb:58:in
open' C:/Ruby200/lib/ruby/gems/2.0.0/gems/cucumber-1.3.16/lib/cucumber/feature_file.rb:58:insource' C:/Ruby200/lib/ruby/gems/2.0.0/gems/cucumber-1.3.16/lib/cucumber/feature_file.rb:37:in
parse' C:/Ruby200/lib/ruby/gems/2.0.0/gems/cucumber-</code> - Sunilkumar V[features,'--color','-r features','-p test_profile']
。或者是[features,'--color','-r','features','-p','test_profile']
。如果这两种方法都不行,那我就不知道解决方法了。 - KomodoDave