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

问题描述 投票:0回答: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
ruby-on-rails ruby testing ruby-riot
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

© www.soinside.com 2019 - 2024. All rights reserved.