Last Updated: December 29, 2017
·
23.55K
· jonahoffline

RSpec Test Results in HTML

When working with Ruby On Rails and RSpec, you may want to share your test results with teammates. A screenshot of the terminal just won't do it. Let's make RSpec generate an html page for us.

$ rspec --format html --out rspec_results.html

Depending on your tests, it might look Rastafarian if you have tests that are pending and failing. Luckily for us, you can filter them.

Here's a results screenshot of a Rails App I've been working on (SoPR)

Screenshot

Automating the process

There are two ways to achieve this.

Using RSpec's config file

I assume you have an .rspec file in your project's root directory. Let's add the following:

--format html
--out rspec_results.html

Now you should be able to just run the rspec command and get the same results.

Writing a Rake Task

As an alternative, you can also write a simple Rake task. Let's write a new file to lib/tasks/rspec_html.rake inside our Rails project.

require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec) do |t|
  t.rspec_opts = '--format html --out reports/rspec_results.html'
end

namespace :rspec_report do
  desc 'Run all specs and generate RSpec report in HTML'
  task :html => :spec

  desc 'Run all specs, generate RSpec report and open it in the browser'
  task :browser do
    Rake::Task[:spec].invoke
    `open reports/rspec_results.html` # This only works if running OS X.
  end
end

Gist available here.

If we run $ rake -T, we will see two new rake tasks:

rake rspec_report:html
rake rspec_report:browser

The first, rspec:html will run all specs and write the report to reports/rspec_results.html.
The second, rspec:browser will run the first task and open the report in the browser (only works in OS X).

Cheers!

2 Responses
Add your response

Great suggestion. I can see this working great when running the entire test suite. I normally use the --format documentation flag because I normally run the tests per file, as opposed to the entire suite.

over 1 year ago ·

These reports doesn't look nice. Earlier I was using pretty_face gem when I was using with WATIR fw. Is their an alternative of that for rspec.

over 1 year ago ·