1

我正在使用 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
4

1 回答 1

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

于 2011-12-13T09:14:29.240 回答