Search code examples
ruby-on-railsrubytestingruby-riot

Ruby/Rails/Riot programmatically access test results


I'm using Riot (https://github.com/thumblemonks/riot) for testing though we could use something else.

I get test output like this:

> ruby my_test_file.rb

Running a test
  + something works
  + something else works
  - something failed

I'd like to programmatically access this information. Something like:

test = TestClass.load("my_test_file.rb")
result = test.run
result.errors # some array

Solution

  • Answering my own question. I can post a more detailed answer if anyone's interested.

    The gist of it is evaluating the test files then getting the context from the evaluated test:

    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
    

    Where MyReporter is your implementation that handles pass/fail test results. See https://github.com/thumblemonks/riot/blob/master/lib/riot/reporter.rb