Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Execute rspec directly from ruby code #359

phstc opened this Issue · 7 comments

4 participants


This Feature Request is related to this post

I would like to execute rspec directly from ruby code.

Maybe something similar to Test::Unit:

How about something like:


Where options would be a list of command line options (run rspec --help to see what is available). This would allow you to automate anything that you can do from the command line. This way, as the command line features evolve, so would this feature.



Actually, you can do this now, but you have to supply output and error objects (IO objects - could be STDOUT, STDERR, or could be file objects):

RSpec::Core::Runner::run(options, err, out)

Does that satisfy your need? We could make the error and output streams optional (i.e. have them default to STDOUT and STDERR).

RSpec::Core::Runner::run(options, err, out)

This solution is good enough for me. But how can I set the spec directory using RSpec::Core::Runner::run?

I tried using files_or_directories_to_run and files_to_run, but both didn't work.

# spec_dir = File.expand_path(File.dirname(__FILE__)) + '/../spec/*_spec.rb'
spec_dir = File.expand_path(File.dirname(__FILE__)) + '/../spec/main_spec.rb'
RSpec::configuration.files_to_run = []
RSpec::Core::Runner::run(options, $stderr, $stdout)

Just pass the directory in options:['spec'], err, out)

Perfect! Thank you very much :beers:

@phstc phstc closed this

HI David,

I have a question related to this API, are there any way to set a custom formatter before the script runs?

What I am trying to achieve is:
1. Running a specific spec programmatically
2. Get the result and parse it to a JSON format programmatically

The ideal programming flow would be:

# 1. set custom formatter
# 2. run a test and get a hashed results
result =['spec'])

this line is magic... thanks a lot.['spec'], err, out)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.