Ruby/Rails/Riot以编程方式访问测试结果



我使用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

最新更新