Execute rspec directly from ruby code #359

phstc opened this Issue May 3, 2011 · 7 comments


None yet
4 participants

phstc commented May 3, 2011

This Feature Request is related to this post http://www.ruby-forum.com/topic/1597560

I would like to execute rspec directly from ruby code.

Maybe something similar to Test::Unit:


dchelimsky commented May 7, 2011

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.



dchelimsky commented May 7, 2011

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).

phstc commented May 7, 2011

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 = [File.new(spec_dir)]
RSpec::Core::Runner::run(options, $stderr, $stdout)

dchelimsky commented May 8, 2011

Just pass the directory in options:

RSpec::Core::Runner.run(['spec'], err, out)

phstc commented May 8, 2011

Perfect! Thank you very much 🍻

phstc closed this May 8, 2011

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 = RSpec::Core::Runner.run(['spec'])

ibusybox commented May 5, 2014

this line is magic... thanks a lot.

RSpec::Core::Runner.run(['spec'], err, out)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment