public
Last active

  • Download Gist
01 example_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13
require 'rspec'
 
describe "Behaviour" do
it "should pass" do
true.should eq true
end
 
it "should fail" do
true.should eq false
end
 
it "should pending"
end
02 rspec_runner.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
require 'rspec'
 
config = RSpec.configuration
json_formatter = RSpec::Core::Formatters::JsonFormatter.new(config.out)
 
# create reporter with json formatter
reporter = RSpec::Core::Reporter.new(json_formatter)
# set reporter for rspec configuration
config.instance_variable_set(:@reporter, reporter)
 
# execute rspec runner
# 'example_spec.rb' is the location of the spec file
RSpec::Core::Runner.run(['example_spec.rb'])
 
# output test result as json
# see example output in `rspec_json_formatter_result.rb`
puts json_formatter.output_hash
03 rspec_json_formatter_result.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
{:examples => [{:description => "should pass",
:full_description => "Behaviour should pass",
:status => "passed",
:file_path => "./my_spec.rb",
:line_number => 4
}, {:description => "should fail",
:full_description => "Behaviour should fail",
:status => "failed",
:file_path => "./my_spec.rb",
:line_number => 8,
:exception => {:class => "RSpec::Expectations::ExpectationNotMetError",
:message => "\nexpected:false\n got:true\n\n(compared using ==)\n",
:backtrace => ["/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/fail_with.rb:33:in `fail_with'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/handler.rb:31:in `handle_matcher'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/syntax.rb:53:in `should'", "/Users/jing/Documents/other/test/my_spec.rb:9:in `block (2 levels) in <top (required)>'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:114:in `instance_eval'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:114:in `block in run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:254:in `with_around_each_hooks'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:111:in `run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:388:in `block in run_examples'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `map'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `run_examples'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:369:in `run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `map'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block in run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/reporter.rb:34:in `report'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:25:in `run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'", "runner.rb:19:in `<main>'"]
}
}, {:description => "should pending",
:full_description => "Behaviour should pending",
:status => "pending",
:file_path => "./my_spec.rb",
:line_number => 12
}],
:summary => {:duration => 0.001543,
:example_count => 3,
:failure_count => 1,
:pending_count => 1
},
:summary_line => "3 examples, 1 failure, 1 pending"
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.