2

I am attempting to programmatically run a series of Rspec tests and capture the output of each.

What I have so far is this:

RSpec.configure do |config|
  config.formatter = :json
end

out = StringIO.new
err = StringIO.new
RSpec::Core::Runner.run([file], err, out)

This does run the given file correctly, and in the terminal that I run this script from I see the JSON output that I expect... but since I'm giving the runner a StringIO stream, I would expect it to write the JSON output to that instead of stdout. In this case, the out stream is always empty.

Anyone have any idea what I'm doing wrong?

4
  • You can write everything that goes to $stdout to a file instead. If that helps stackoverflow.com/questions/26894443/… Commented Apr 22, 2017 at 0:20
  • I've tried doing something similar by doing $stdout = StringIO.new, but I still get the output on the screen and there's nothing in $stdout, it's a blank StringIO object. Commented Apr 22, 2017 at 0:24
  • I ended up working around this as I only needed to know if the spec failed or not, and the run function returns 0 on success and 1 on failure. Still no idea why the output stream doesn't seem to work. Commented Apr 22, 2017 at 13:58
  • I'm finding I have this problem in rspec 3.3, but it seems to be working fine in rspec 3.6 ... Commented Jul 28, 2017 at 5:38

1 Answer 1

1

To run a series of Rspec tests I use Rake and to record their output I use a RSpec::Core::RakeTask in which I can specify rspec_opts with which you can pass a location and format for rspecs output. Example:

Rakefile

require 'rake'
require 'rspec/core/rake_task'

desc "Run tests, recording their output"

RSpec::Core::RakeTask.new(:spec) do |task|
  task.pattern = 'spec/*_spec.rb' 
  task.rspec_opts = '--format documentation' +
  ' --format json --out logs/output.json' 
end

I made a basic github project demonstrating this at https://github.com/SamuelGarratt/rake_demo

The above Rakefile assumes your rspec tests are in a folder called 'spec' and end in '_spec'. Type 'rake spec' on the command line to run all the rspec tests and have their output recorded.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.