从 Ruby 执行 Rspec
我正在尝试从 ruby 执行 rspec,并从方法或类似的东西中获取失败的状态或数量。实际上我正在运行这样的东西:
system("rspec 'myfilepath'")
但我只能获取函数返回的字符串。有没有办法直接使用对象来做到这一点?
I am trying to execute rspec from ruby, and get the status or number of failures from a method or something like that. Actually I am running something like this:
system("rspec 'myfilepath'")
but I only can get the string returned by the function. Is there any way to do this directly using objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为最好的方法是使用 RSpec 的配置和格式化程序。这不涉及解析 IO 流,还可以通过编程方式提供更丰富的结果定制。
RSpec 2:
现在您可以使用 json_formatter 对象来获取规范测试的结果和摘要。
output_hash
值的示例可以在此处:RSpec 3
其他资源
I think the best way would be using RSpec's configuration and Formatter. This would not involve parsing the IO stream, also gives much richer result customisation programmatically.
RSpec 2:
Now you can use the
json_formatter
object to get result and summary of a spec test.An example of
output_hash
value can be found here:RSpec 3
Other Resources
我建议您查看 rspec 源代码以找出答案。我认为你可以从 example_group_runner
编辑:好的,方法是:
选项 - 目录数组、错误和错误;输出-流。例如
I suggest you to take a look into rspec source code to find out the answer. I think you can start with example_group_runner
Edit: Ok here is the way:
Options - array of directories, err & out - streams. For example
您的问题是您正在使用
Kernel#system
方法来执行命令,该方法仅根据是否可以找到该命令并成功运行它来返回 true 或 false。相反,您想要捕获 rspec 命令的输出。本质上,您想要捕获 rspec 输出到 STDOUT 的所有内容。然后,您可以迭代输出以查找并解析该行,该行将告诉您运行了多少个示例以及有多少次失败。大致如下:
这应该输出示例总数和失败数量 - 根据需要进行调整。
Your problem is that you're using the
Kernel#system
method to execute your command, which only returns true or false based on whether or not it can find the command and run it successfully. Instead you want to capture the output of therspec
command. Essentially you want to capture everything that rspec outputs to STDOUT. You can then iterate through the output to find and parse the line which will tell you how many examples were run and how many failures there were.Something along the following lines:
This should output the number of total examples and number of failures - adapt as needed.
该命令打印到控制台并同时捕获消息。 formatter.stop 只是一个存根函数,我不知道它通常的用途,我必须包含它才能使用 DocumentationFormatter。格式化程序输出还包含控制台着色代码。
This one prints to console and at the same time captures the message. The formatter.stop is just a stub function, I don't know what it is for normally, I had to include it to use DocumentationFormatter. Also the formatter output contains console coloring codes.