Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Execute rspec directly from ruby code #359

Closed
phstc opened this Issue · 7 comments

4 participants

@phstc

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:

Test::Unit::UI::Console::TestRunner.run(MySuite.new(MyTestCase))
@dchelimsky
Owner

How about something like:

RSpec::run(options)

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.

WDYT?

@dchelimsky
Owner

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
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
Owner

Just pass the directory in options:

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

Perfect! Thank you very much :beers:

@phstc phstc closed this
@activars

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

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
Something went wrong with that request. Please try again.