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)
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:
1 2 |
|
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Gist available here.
If we run $ rake -T
, we will see two new rake tasks:
1 2 |
|
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!