RSpec JUnit Formatter for Jenkins

While RSpec is great, it is missing a built in JUnit formatter so you don’t get the pretty analysis stuff from Jenkins that you otherwise would. There have been a few attempts at writing an RSpec JUnit formatter, but I’ve never seen something work consistently across versions of RSpec. In my experience, the ci_reporter gem works well, but only with some versions of RSpec. Since it doesn’t work with the current version (2.10), I decided to write my own simple JUnit formatter that will allow me to run my tests in Jenkins. To use it, save the code that follows to a file called junit.rb and then call rspec with the following options:

rspec my_spec.rb -r ./junit.rb -f JUnit -o results.xml

That will cause RSpec to require the file containing the formatter, invoke the formatter and save the JUnit-formatted results to a file called results.xml. A better option would be to require the junit.rb file from your spec_helper.rb file (which in turn is being required by your .rspec file, right?). If you do that then you’ll only need to call the following:

rspec my_spec.rb -f JUnit -o results.xml

Anyway, here’s the code. Hope it helps!

11 thoughts on “RSpec JUnit Formatter for Jenkins

  1. Pingback: RSpec and ci_reporter | Nat On Testing

  2. thanks for this, very helpful.. one minor issue:

    rspec -r ./junit.rb -f JUnit -o foo.xml
    /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require’: ./junit.rb:72: syntax error, unexpected ‘:’, expecting kEND (SyntaxError)
    builder = Builder::XmlMarkup.new indent: 2

    i had to change this to
    builder = Builder::XmlMarkup.new(:indent=> 2)

    to make it work correctly

  3. Has anyone tried getting this to work with Rspec 1.3.2?

    Seems the JUnit class should inherit from this instead:
    Spec::Runner::Formatter::BaseFormatter

    And, import from this:
    ‘spec/runner/formatter/base_formatter’
    instead of:
    ‘rspec/core/formatters/base_formatter’

    But, the cli options are different as well… if I run this:
    bundle exec spec my_spec.rb –require spec/junit.rb –format JUnit

    I get:
    .rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/runner/options.rb:247:in `initialize’: wrong number of arguments (2 for 1) (ArgumentError)

    Any ideas/experiences?

  4. BRILLIANT! Tried with 2 other frameworks then I stumbled on this. Got it up and running in about 5 minutes on my Jenkins server. Thanks a lot :) .

  5. Your formatter works just great! Thanks for this!
    I made a small change, replacing the use of Builder gem and use REXML instead. The reason is that I have many projects in Jenkins already that I’d like not to change.
    I posted the changed version in a forked Gist: https://gist.github.com/4012658

    Of course yours look better and it’s probably much faster, but not big deal for a CI server.

  6. Pingback: Yarjuf – Yet Another RSpec JUnit Formatter (for Jenkins) | Nat On Testing

  7. Pingback: Parallelizing Test Execution: Speed Run! | RTR Tech Blog

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>