Alex Pooley's Blog

Hello there, my name is Alex Pooley and I'm a freelance web developer residing in Perth, Western Australia. My passion is in the development of web sites that solve everyday problems. Here's a gallery of some of my notable work. If you need a web site designer or developer, contact me with further details. Lastly, you can read more about me.

Ruby On Rails Test Coverage

January 1st, 2006

Why don't you subscribe to my blog while you're here? I'm a freelance web developer and I blog about Ruby, Rails, and business online.

Go ahead and subscribe to my RSS feed. Thanks for visiting!

*Update:* Ignore the RPA instructions below and grab the latest rcov from “http://eigenclass.org/hiki.rb?rcov”:http://eigenclass.org/hiki.rb?rcov

For a long time now I have developed without a formalised test strategy. Most of the time I will code something, click away in the browser until I’m satisfied it all works and then start on another block of functionality. I am currently in the process of developing a system that I hope to go live, and scale to millions of people. The last thing I want is for the code to break. Recently I started writing functional tests (testing the controllers) and unit tests (testing the models) to ensure that my system is consistently stable. By building up a set of tests I can ensure, years down the track even, that the code is still doing what I intended it to do.

The point of this article is to highlight the importance, and demonstrate how to calculate your test coverage. Test coverage is the amount of code you are actually testing. This helps ensure that you test every code path at least once. In the world of Ruby a good option is to use the rcov tool which is part of the “Ruby Production Archive”:http://rpa-base.rubyforge.org/wiki/wiki.cgi?RpaBase (RPA). Here’s how to get going:

# “Grab RPA”:http://rubyforge.org/frs/?group_id=265
# If your running Debian you will need to _apt-get install libtest-unit-ruby libzlib-ruby libyaml-ruby ruby1.8-dev rdoc_
# Decompress the downloaded archive
# run ‘ruby install.rb’ in a command/shell prompt
# rpa update (this grabs an index of stuff you can grab via RPA)
# rpa install rpa-base ri-rpa rcov

…and you’re done. Now jump in to your tests directory in rails and create a test_all.rb file. This file bootstraps all the tests and is as simple as requiring the unit test module, and all your functional and unit tests. You will end up with something like:

require 'test/unit'

#Functional tests.
require 'functional/employee_controller_test'
require 'functional/business_controller_test'

#Unit tests.
require 'unit/employee_mailer_test'
require 'unit/employee_test'
require 'unit/business_test'

Once you’ve done that, jump in to the test directory shell/command prompt and type ‘rcov test_all.rb’. This runs the coverage analysis tool and dumps the output by default in the ./coverage directory.

The output is quite bland but it shows you what you need to know. The goal is to get your coverage to 100%. You can also drill down to each source file to see exactly what paths are not currently tested In fact I even found a couple of tests that weren’t running because I named them incorrectly.

If you’re serious about whatever it is you’re coding then write tests and check your code coverage. Ruby and Ruby On Rails make this stuff easy. Enjoy.

*Update:* rcov 0.2.0 has been out for a bit. I blogged about it “here.”:http://alexpooley.com/articles/2006/04/23/ruby-coverage-testing-rcov-0-2-0 Also Shanti commented that I left out the step where you install rcov via rpa so I updated step 6 too, even though the rcov rpa version is now outdated.

3 Responses to “Ruby On Rails Test Coverage”

  1. http://feeds.feedburner.com/Asplake Says:

  2. http://feeds.feedburner.com/Asplake Says:

  3. Shanti Braford Says:

Leave a Reply

buy mp3 music uk vpn