从Ruby执行Rspec

从Ruby执行Rspec,ruby,rspec,Ruby,Rspec,我试图从ruby执行rspec,并从一个方法或类似的东西获取失败的状态或数量。实际上,我正在运行这样的程序: system("rspec 'myfilepath'") 但是我只能得到函数返回的字符串。有没有办法直接使用对象来实现这一点?我建议您查看rspec源代码以找到答案。我想你可以从 编辑:好,方法如下: RSpec::Core::Runner::run(options, err, out) 选项-目录数组、错误和输出流。比如说 RSpec::Core::Runner.run(['spe

我试图从ruby执行rspec,并从一个方法或类似的东西获取失败的状态或数量。实际上,我正在运行这样的程序:

system("rspec 'myfilepath'")

但是我只能得到函数返回的字符串。有没有办法直接使用对象来实现这一点?

我建议您查看rspec源代码以找到答案。我想你可以从

编辑:好,方法如下:

RSpec::Core::Runner::run(options, err, out)
选项-目录数组、错误和输出流。比如说

RSpec::Core::Runner.run(['spec', 'another_specs'], $stderr, $stdout) 

您的问题是,您正在使用
Kernel#system
方法执行您的命令,该方法仅根据它是否能够找到命令并成功运行返回true或false。相反,您希望捕获
rspec
命令的输出。本质上,您希望捕获rspec输出到STDOUT的所有内容。然后,您可以遍历输出以查找并解析该行,该行将告诉您运行了多少个示例以及有多少个失败

大致如下:

require 'open3'
stdin, stdout, stderr = Open3.popen3('rspec spec/models/my_crazy_spec.rb')
total_examples = 0
total_failures = 0
stdout.readlines.each do |line|
  if line =~ /(\d*) examples, (\d*) failures/
    total_examples = $1
    total_failures = $2
  end
end
puts total_examples
puts total_failures

这将输出总示例数和故障数-根据需要进行调整。

我认为最好的方法是使用RSpec的配置和格式化程序。这不需要解析IO流,还可以通过编程方式提供更丰富的结果定制

RSpec 2: 现在,您可以使用
json\u格式化程序
对象来获取规范测试的结果和摘要

# gets an array of examples executed in this test run
json_formatter.output_hash
可以找到
输出\u哈希值的示例:

RSpec 3 其他资源

这一条打印到控制台,同时捕获消息。stop只是一个存根函数,我不知道它通常用于什么,我必须包含它才能使用DocumentationFormatter。格式化程序输出还包含控制台着色代码

formatter = RSpec::Core::Formatters::DocumentationFormatter.new(StringIO.new)
def formatter.stop(arg1)
end
RSpec.configuration.reporter.register_listener(formatter, :message, :dump_summary, :dump_profile, :stop, :seed, :close, :start, :example_group_started)

RSpec::Core::Runner.run(['test.rb','-fdocumentation'])

puts formatter.output.string

谢谢你的回复,但还有一些东西会有所帮助,因为这只会给我带来更多的问题:(我已经添加了一些信息来回答。希望这会有所帮助。帮助很大!类似的是:我还有其他问题…当我在for语句中执行此操作时,IO对象崩溃:尝试使用文档格式化程序,结果可以通过编程方式查询-无需解析IO。请参阅此处:如果确实要使用原始命令行-I th墨水最好把它放在RAKE任务中。RSPEC已经有任务来自动化它。你可能想考虑下面我提到的解决方案。没有IO解析。我尝试过这个方法和文档。,“my_spec.rb”是rspec测试文件,它需要包含文件路径。也许你没有正确包含spec文件,那么就没有测试结果。不,测试运行得很好。我在控制台上看到了输出,但文档格式化程序似乎没有捕获它。你有Github要点或代码片段,我可以查看吗?很难诊断I没有示例的问题..我仔细检查了最新的rspec,我认为最好使用JsonFormatter,如果您仍然感兴趣,请查看更新的答案和相关示例链接。说真的,没有办法单独调用
rspec::ExampleGroup
,而不必将其保存到文件中吗?
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
formatter = RSpec::Core::Formatters::DocumentationFormatter.new(StringIO.new)
def formatter.stop(arg1)
end
RSpec.configuration.reporter.register_listener(formatter, :message, :dump_summary, :dump_profile, :stop, :seed, :close, :start, :example_group_started)

RSpec::Core::Runner.run(['test.rb','-fdocumentation'])

puts formatter.output.string