martes, 5 de agosto de 2008

RSpec in Ubuntu: Setting up testing environment

Recently, I had a difficult time when my computer crashed.. After reinstalling Ubuntu desktop, and all the tools I realized how important is to maintain documents about how things are installed and configured :P

One of the things that I've been forced to research was rspec (installation and configuration) in order to restore what I've already had. This is the motivation for this entry: document the way I consider useful to install and customize rspec with notifications, and reports, etc.

Install the gems

Assuming that you already have installed rails (http://wiki.rubyonrails.org/rails/pages/RailsOnUbuntu) and an application (let's called it myapp) suitable to use with rspec. In this context, we are going to install the ZenTest gem (which includes rspec & autotest):

gem install ZenTest
gem install diff-lcs

After that, You'll need to install both rspec and rspec-rails plugins within your Rails application. For that matter, you should check: github.com/dchelimsky/rspec-rails/wikis/home (where it shows the different procedures you should follow to install it depending on your version of Rails).


After this step, you'll be able to run the autotest tool from the command line.

Configure autotest


Then in the myapp you have to create the configuration file .autotest:


#!/usr/bin/env ruby
require 'autotest/redgreen'
def self.notify title, msg, img, pri='low', time=3000
`notify-send -i #{img} -u #{pri} -t #{time} '#{msg}'`
end
Autotest.add_hook :ran_command do |at|
results = [at.results].flatten.join("\n")
output = results.slice(/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+not implemented)?(,\s*(\d+)\s+pending)?/)
folder = "~/Pictures/autotest/"
if output =~ /([123456789]|[\d]{2,})\sfailures?/
notify "FAIL:", "#{output}", folder+"rails_fail.png", 'critical', 10000
elsif output =~ /[1-9]\d*\spending?/
notify "PENDING:", "#{output}", folder+"rails_pending.png", 'normal', 10000
else
notify "PASS:", "#{output}", folder+"rails_ok.png"
end
end

This code hooks in the autotest evaluation process and invokes the command line utility notify-send with the result of the evaluation. It also uses this pictures within their notifications (that enhance the user experience):














Configure Ubuntu notifiactions

Finally, you have to install the following package in your system:


sudo apt-get install libnotify-bin


This tool enable us to display pop-ups within Ubuntu's environment.

RSpec reports

Other important file for autotest is /spec.opts, which is located at myapp/spec:

#myapp/spec/spec.opts
--colour
--format html:doc/rspec_results.html
--format progress
--loadby mtime
--reverse

In this configuration I specify that I want autotest to keep running in the console and, at the same time, I want to generate the html report (rspec_results.html).

Run autotest & see results.

After you configure all that I mentioned, you are able to execute the utility autotest from myapp directory and obtain the following results:

marcelo@host:~/workspace/myapp$ autotest
loading autotest/rails_rspec
/usr/bin/ruby1.8 -S script/spec -O spec/spec.opts spec/controllers/users_routing_spec.rb spec/views/users/show.html.erb_spec
...




This will generate the following output in the console:


You can also check the details results of doc/rspec_results.html in your browser:

5 comentarios:

Anónimo dijo...

I tend to install rspec as a plugin in vendor along with the rspec_rails plugin, usually they both work "in tandem" so I find it more practical to have both of them as plugins.

Off Topic: I find the styling you use a little dark and with low contrast... Is just my humble opinion but I found it a little difficult to read (my two cents :)

Greets!

mgiorgi dijo...

Absolutely, I've totally forgot to mentioned rails-rspec :P. In fact I use this page: http://github.com/dchelimsky/rspec-rails/wikis/home, in order to install both plugins.

PD: Now that you mention, it is true...it a little dark in here :P. I'll try a little brighter for a change

Anónimo dijo...

I followed your guide and get a script/plugin install svn://rubyforge.org/var/svn/rspec/tags/REL_0_7_2/vendor/rspec_on_rails/vendor/plugins/rspec

error when I try to start the script. :(

mgiorgi dijo...

Maybe, you can check on this page http://github.com/dchelimsky/rspec-rails/wikis/home, to see what is the procedure you need to install rspec. As you can see, the procedure depends on your current Rails version & RSpec version that you will install.

After you install RSpec, you can configure autotest using this blog instructions. If you have any other problem, please let me know!

Thanks,
Marcelo

App Development dijo...

Thanks for the valuable information and insights you have so provided here. Keep it up!. Regards, michaelvk from 3D Animation