【问题标题】:How do I get the output (STDOUT) from Cucumber::CLI::Main.execute into a variable如何从 Cucumber::CLI::Main.execute 获取输出(STDOUT)到变量中
【发布时间】:2017-07-14 04:44:29
【问题描述】:

在 Jruby 9.1.7.0 中运行黄瓜脚本。输出到 STDOUT。我怎样才能把它保存到局部变量中?

require 'cucumber'
require 'stringio'

@output = StringIO.new
features = 'features/first.feature'
args = features.split.concat %w(-f html)

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

【问题讨论】:

    标签: cucumber stdout jruby stdin popen3


    【解决方案1】:

    您也可以将其写入临时文件并从文件中读取值。

    require 'cucumber'
    require 'tempfile'
    require 'securerandom'
    
    filename = "#{SecureRandom.urlsafe_base64}"
    file = Tempfile.new(filename)
    filepath = "#{file.path}"
    features = "cucumber/ars/features/ars_additional.feature"
    args = features.split.concat %w(-f html -o)
    args << filepath
    Cucumber::Cli::Main.new(args).execute!
    
    @output = file.read
    file.close
    file.unlink
    

    【讨论】:

      【解决方案2】:

      如果你输入 cmd "cucumber --help"

      -o, --out [FILE|DIR]             Write output to a file/directory instead of STDOUT. This option
                                       applies to the previously specified --format, or the
                                       default format if no format is specified. Check the specific
                                       formatter's docs to see whether to pass a file or a dir.
      

      你可以修改你的代码

      args = features.split.concat %w(-f html -o test.html)
      

      【讨论】:

        猜你喜欢
        • 2012-07-14
        • 1970-01-01
        • 1970-01-01
        • 2018-04-13
        • 2017-04-18
        • 2023-01-19
        • 1970-01-01
        • 1970-01-01
        • 2011-04-20
        相关资源
        最近更新 更多