[rspec-users] Running RSpec from within my Ruby application

Daniel Lucraft dan.lucraft at googlemail.com
Sat Sep 27 08:02:32 EDT 2008

Hi all

(I tried to send this question to the list a few hours ago, but it
seems to have not got through. If it did sorry for the duplication.)

Does anyone have any tips or examples of how to run RSpec example
groups programmatically from within a Ruby application?

Essentially I would like to be able to:
  1. load example groups from a set of files I know about
  2. run them
  3. get back an object that contains information about successes and failures
  4. reload the example groups

I think I i can accomplish (3) with a custom formatter, (4) by
reflecting on and undefining the classes Example::Subclass_1 etc as

But I'm having real trouble with the first two steps. Here's what I have so far:

  require 'rubygems'
  require 'spec'

  $spec_runner_at_exit_hook_registered = true
  require 'some_specs'

  err = StringIO.new
  out = StringIO.new
  options = Spec::Runner::Options.new(err, out)

  runner = Spec::Runner::ExampleGroupRunner.new(options)

  puts "results:"
  puts out.string
  puts "\n\n[script end]"

But the results string here claims that there were 0 examples run. By
looking at the output I know that my example is being run however.

Can someone help me put these pieces together?


More information about the rspec-users mailing list