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

时间:2011-10-25 20:31:12

标签: ruby-on-rails ruby testing 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

1 个答案:

答案 0 :(得分:2)

回答我自己的问题。如果有人感兴趣,我可以发布更详细的答案。

它的要点是评估测试文件,然后从评估的测试中获取上下文:

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