Skip to main content

_ruby

Example

In RSpec, you need a plugin to configure JUnit XML output. There are two popular ones:

Let's step through how to use the latter of the two.

After installing RSpec JUnit Formatter we need to specify two new parameters at run-time: a formatter, and an output file.

rspec --format RspecJunitFormatter  --out result.xml

Running tests with these parameters will format the test results into JUnit XML and pipe them into a result.xml file (and overwrite it on subsequent runs).

If we didn't want to type these parameters out every time we could place them into an RSpec command helper file (e.g., .rspec).

# filename: .rspec

--format RspecJunitFormatter
--out result.xml

If we want to run our tests in parallel (using something like parallel_tests), then we will want to generate a uniquely named result file for each parallel process (otherwise concurrent test runs will overwrite the same file rendering the results effectively useless). To do that we can use interpolation thanks to ERB and an environment variable made available by parallel_tests.

Thankfully parallel_tests has a RSpec command helper file as well (e.g., .parallel_rspec).

# filename: .parallel_rspec

--format RspecJunitFormatter
--out result<%= ENV['TEST_ENV_NUMBER'] %>.xml

Now with numerous tests being run through parallel_tests the final result will be a series of JUnit XML files (e.g., result1.xml, result2.xml, etc.).

And if we didn't want this to run all the time (like if we only wanted JUnit XML output to get generated when running our tests on a CI server) then we can add a conditional to our .parallel_rspec file.

# filename: .parallel_rspec

<% if ENV['CI'] %>
--format RspecJunitFormatter
--out results/result<%= ENV['TEST_ENV_NUMBER'] %>.xml
<% end %>

Now all we need to do is specify an environment variable at run time in order to trigger JUnit XML output (e.g., CI=on parallel_rspec).

Summary

Now you're ready to plug your Selenium tests into a CI Server and let them report the results.