我试图从ruby执行rspec,并从方法或类似的东西中获取失败的状态或数量.其实我正在运行这样的事情:
system("rspec 'myfilepath'")
Run Code Online (Sandbox Code Playgroud)
但我只能得到函数返回的字符串.有没有办法直接使用对象?
act*_*ars 28
我认为最好的方法是使用RSpec的配置和Formatter.这不涉及解析IO流,也以编程方式提供更丰富的结果定制.
require 'rspec'
config = RSpec.configuration
# optionally set the console output to colourful
# equivalent to set --color in .rspec file
config.color = true
# using the output to create a formatter
# documentation formatter is one of the default rspec formatter options
json_formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output)
# set up the reporter with this formatter
reporter = RSpec::Core::Reporter.new(json_formatter)
config.instance_variable_set(:@reporter, reporter)
# run the test with rspec runner
# 'my_spec.rb' is the location of the spec file
RSpec::Core::Runner.run(['my_spec.rb'])
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用该json_formatter对象获取规范测试的结果和摘要.
# gets an array of examples executed in this test run
json_formatter.output_hash
Run Code Online (Sandbox Code Playgroud)
output_hash可以在此处找到值的示例:
require 'rspec'
require 'rspec/core/formatters/json_formatter'
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(['spec.rb'])
# here's your json hash
p formatter.output_hash
Run Code Online (Sandbox Code Playgroud)
我建议你看一下rspec源代码来找出答案.我想你可以从example_group_runner开始
编辑:好的就是这样:
RSpec::Core::Runner::run(options, err, out)
Run Code Online (Sandbox Code Playgroud)
选项 - 目录数组,错误和输出 - 流.例如
RSpec::Core::Runner.run(['spec', 'another_specs'], $stderr, $stdout)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7506 次 |
| 最近记录: |