我正在使用Riot(https://github.com/thumblemonks/riot)进行测试,尽管我们可以使用其他方法。
我得到这样的测试输出:
> ruby my_test_file.rb
Running a test
+ something works
+ something else works
- something failed
我想以编程方式访问此信息。就像是:
test = TestClass.load("my_test_file.rb")
result = test.run
result.errors # some array
最佳答案
回答我自己的问题。如果有人感兴趣,我可以发布更详细的答案。
要点是评估测试文件,然后从评估的测试中获取上下文:
Riot.alone! # so Riot doesn't automatically run the tests
["/path/to/test/file"].each do |test_file|
eval(IO.read(path), binding, test_file) # load test file
Riot.root_contexts.each do |context|
reporter = MyReporter.new
context.run reporter
# do something cool with reporter results
end
Riot.root_contexts.clear # clean out root_contexts so it's clean for the next run
end
MyReporter是您的实现通过/失败测试结果的实现。见https://github.com/thumblemonks/riot/blob/master/lib/riot/reporter.rb
关于ruby-on-rails - Ruby/Rails/Riot以编程方式访问测试结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7895493/