我正在尝试从ruby执行rspec,并从一个方法或类似的东西中获取失败的状态或数量。实际上,我运行的是这样的东西:
system("rspec 'myfilepath'")
但是我只能得到函数返回的字符串。有没有办法直接使用对象来做这件事呢?
发布于 2011-08-09 01:58:48
我建议你去看看rspec的源代码来找出答案。我认为你可以从example_group_runner开始
Edit: Ok,方法如下:
RSpec::Core::Runner::run(options, err, out)
Options -目录数组,错误和流出。例如
RSpec::Core::Runner.run(['spec', 'another_specs'], $stderr, $stdout)
发布于 2011-08-09 13:23:18
您的问题是,您正在使用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
这应该会输出总示例数和失败数-根据需要进行调整。
发布于 2019-06-13 15:54:47
这个命令打印到控制台,同时捕获消息。formatter.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
https://stackoverflow.com/questions/6986319
复制相似问题