我正在使用RSpec,我想在完成后获得每个测试的结果(通过与否),类名,测试名称(或描述)和错误消息(如果存在)。
所以这是一个非常简单的代码
describe MyClass do
after :each do
#how do I know a test passed, its name, class name and error message?
end
it "should be true" do
5.should be 5
end
it "should be false" do
5.should be 6
end
end
您的建议?
有一些用于测试输出的格式化程序可以显示哪些测试通过/失败/挂起,听起来您对名为 documentation
的测试感兴趣。
为了使此格式化程序可用于所有 rspec 文件,请创建一个名为 .rspec
的文件,其中包含以下内容:
--color # I just like this one for more easily read output
--formatter documentation
这意味着当您运行上面这样的测试套件时,您将看到:
MyClass
should be true
should be false (Failed - 1)
# Individual output for each error would show up down here
参考: https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/formatters/custom-formatters
格式化程序获取额外的信息,但由于after
钩子是潜在的故障点,因此它们本身不会公开故障信息。
查看 https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/formatters/custom-formatters 和 https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/formatters/base_formatter.rb