以 JSON 格式获取 Rspec 测试数据

Get Rspec test data in JSON format

如何从 Rspec 测试中获取 JSON 格式的数据?例如,有多少次测试失败等

我知道如果我从 ruby 运行 Rspec 得到它,如 here 所述 为了方便起见,我将代码粘贴在这里。

run(paths)
  # runs rspec tests from ruby
  # source: 

  config = RSpec.configuration

  formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output_stream)

  # create reporter with json formatter
  reporter =  RSpec::Core::Reporter.new(config)
  config.instance_variable_set(:@reporter, reporter)

  # internal hack
  # api may not be stable, make sure lock down Rspec version
  loader = config.send(:formatter_loader)
  notifications = loader.send(:notifications_for, RSpec::Core::Formatters::JsonFormatter)

  reporter.register_listener(formatter, *notifications)

  RSpec::Core::Runner.run(paths)

  formatter.output_hash

end 

有没有办法在 Rspec 测试套件 运行 时将此 json 输出写入文件?

鉴于这只涉及 运行 一个特定的格式化程序,您可以 运行

rspec --format json > output.json