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)
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:
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.
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = '--format html --out reports/rspec_results.html'
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
`open reports/rspec_results.html` # This only works if running OS X.
Gist available here.
If we run
$ rake -T, we will see two new rake tasks:
rspec:html will run all specs and write the report to
rspec:browser will run the first task and open the report in the browser (only works in OS X).