Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have JunitFormatter output for Rspec run using Rake?

Tags:

ruby

rake

rspec

I have no problem running the rspec file using :

rspec -f JUnitFormatter -o junit.xml spec_test.rb

However each time I try rake to execute spec file, I get the following error

/formatters/junit_formatter.rb:28:in `require': no such file to load --     rspec/core/formatters/base_formatter (LoadError)
from ./formatters/junit_formatter.rb:28
from /usr/lib/ruby/1.8/spec/runner/option_parser.rb:151:in `require'
from /usr/lib/ruby/1.8/spec/runner/option_parser.rb:151:in `invoke_requires'
from /usr/lib/ruby/1.8/spec/runner/option_parser.rb:150:in `each'
from /usr/lib/ruby/1.8/spec/runner/option_parser.rb:150:in `invoke_requires'
from /usr/lib/ruby/1.8/spec/runner/option_parser.rb:105:in `initialize'
from /usr/lib/ruby/1.8/optparse.rb:1267:in `call'
from /usr/lib/ruby/1.8/optparse.rb:1267:in `parse_in_order' 
from /usr/lib/ruby/1.8/optparse.rb:1254:in `catch'
from /usr/lib/ruby/1.8/optparse.rb:1254:in `parse_in_order'
from /usr/lib/ruby/1.8/optparse.rb:1248:in `order!'
from /usr/lib/ruby/1.8/spec/runner/option_parser.rb:134:in `order!'
from /usr/lib/ruby/1.8/spec/runner.rb:51:in `options'
from /usr/lib/ruby/1.8/spec/runner/command_line.rb:6:in `run'
from /usr/bin/spec:3

My Rake file :

require 'rubygems'
require 'spec/rake/spectask'

Spec::Rake::SpecTask.new(:spec) do |t|
  t.spec_files = FileList['spec_*.rb']
  t.spec_opts = ['--options .rspec','--format JUnitFormatter' '--output junit.xml']
end
like image 983
Jack Avatar asked Jan 09 '12 07:01

Jack


2 Answers

You need to require the junit formatter before using it. Add gem 'rspec_junit_formatter' to your Gemfile, bundle install, then run Rspec like this:

rspec -r rspec_junit_formatter --format RspecJunitFormatter -o junit.xml

Source

Edit due to comment:

You can get this to work by adding a custom rake task:

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |t|
  t.fail_on_error = false
  t.rspec_opts = "--no-drb -r rspec_junit_formatter --format RspecJunitFormatter -o junit.xml"
end
like image 66
Martin Foot Avatar answered Oct 28 '22 20:10

Martin Foot


Take a look at this: https://github.com/natritmeyer/yarjuf if you want a JUnit formatter for RSpec

like image 26
Nat Ritmeyer Avatar answered Oct 28 '22 21:10

Nat Ritmeyer