0

I have a ruby script which needs to execute rspec tests and collect results. I'm using standard rspec API to achieve this:

require 'rspec'
require 'rspec/core'
require 'rspec/core/formatters/json_formatter'
require 'json'

def run_test(test_location)
 config = RSpec.configuration
 json_formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output)
 reporter =  RSpec::Core::Reporter.new(json_formatter)
 config.instance_variable_set(:@reporter, reporter)
 begin
  ::RSpec::Core::Runner.run([test_location,'--format','j'])
  json_formatter.output_hash
 rescue Exception => e
  e.message
 end
end

run_test(<PATH_TO_RSPEC_TEST_SCRIPT>)

Now, when something fails in rspec test, I can only get error message but I would like to get backtrace from the underlying rspec test that failed. Is there a way to achieve this? I've tried passing arguments: --backtrace or -b to the array that is passed to RSpec::Core::Runner.run method but didn't get much of the help with that

Thanks in advance

Bakir Jusufbegovic
  • 2,806
  • 4
  • 32
  • 48

1 Answers1

1

The backtrace for any example that raises an exception is available in the RSpec::Core::Formatters::JsonFormatter instance. There is no need to add the -b option to the arguments.

json_formatter.examples.each do |example|
    printf( "Example:  [%s]\n", example.metadata[:description] )
    printf( "Backtrace:\n %s\n", example.exception.backtrace.join("\n ") ) unless example.exception.nil?
end

An Exception will include a backtrace as well

rescue Exception => e
  warn e.message
  e.backtrace
end

Be careful when using rescue Exception as you will catch more than you want.

Community
  • 1
  • 1
Matt
  • 68,711
  • 7
  • 155
  • 158
  • Thanks but I think this works only when some spec test fails and we need to get backtrace. On the other hand, if someone makes syntactical error like for example do following: desscribe "test" do it "doesn't work" do 1.should eq(1) end end json_formatter_object contains: #, @failure_count=0, @pending_count=0, @example_count=0, @examples=[], @failed_examples=[], @pending_examples=[], @example_group=nil, @output_hash={}> – Bakir Jusufbegovic Jul 14 '14 at 22:38
  • On the other hand, if we run this same spec file from cmd, we will get this: /Users/bjusufbe/Desktop/rspec.rb:1:in `': undefined method `desscribe' for main:Object (NoMethodError) What I need is handle these cases as well and retrieve line of the code where this error happens (same we have in cmd line execution) – Bakir Jusufbegovic Jul 14 '14 at 22:39
  • `rspec` does not catch exceptions at that level so it exits with the backtrace. The `JsonFormatter` remains in it's initial state when an exception occurs as you were catching all exceptions and returning the exception message. – Matt Jul 15 '14 at 09:56
  • Your other option is to not delve into the internals of Rspec. You can run `rspec` via `system`. Check the exit status and pull the results from file (`--format json --out rspec.json `) or `stderr` for when rspec fails. – Matt Jul 15 '14 at 10:07