Thursday, February 1, 2007

Ruby on Rails - Part 2

My previous article Ruby on Rails Part 1, touched briefly on my experiences installing Ruby, Rails and RadRails. Now that I was ready to start coding, I purchased the expectional tome "Agile Web Development with Rails (Second Edition)" by Dave Thomas and others.

Depot and Unit Testing

After working my way through the Depot application tutorial, I arrived at Task T: Testing. I thought JUnit made unit testing relatively easy, but RoR makes the task a no brainer! RadRails made running the tests very easy with a single button click from within the IDE. As I stared blankly at the screen for a moment clicking and watching the green bar of success progress to completion, I started thinking that I needed a little bit more.

Test Suites and Reporting

JUnit has the ability to produce an XML file of the test results. There is an additional Ant task that can read the test result file and generate a very nice set of HTML test reports. I searched around and found test-report, a library that publishes the results of a ruby unit test suite in HTML and XML. Although test-report took care of formatting the test results, I need a way to dynamically create a suite of all tests in my project. Searching the web produced a few results from which I was able to produce the following test suite that did what I wanted:
require File.dirname(__FILE__) + '/../test_helper'
require 'test/test_helper'
require 'test/unit/testsuite'
require 'test/unit/ui/console/testrunner'
require 'test/unit/ui/reporter'

require 'fileutils'

class OpenEprsTestSuite < Test::Unit::TestSuite
  # Override the initialize method
  def initialize


  # Load unit tests.
  def loadTestSuite()
    puts "Loading Unit Test Suite..."
    Dir.glob("test/unit/**/*_test.rb").each { |fn|
      test_klass =
        ::File.basename(fn).gsub('.rb',"").split('_').each {
          |part| part = part.capitalize! }.join
      puts "Found: " << test_klass << " In " << fn
      require fn
      @tests << eval("#{test_klass}.suite")

  # Create the output folder for the test reports
  def createOutputFolder

# Run the tests,
  BUILD_TEST_DIR, :html)


After adding a new task to my project's rake file, I can now run a complete test suite with HTML reporting. This should make including test reports with continuous build integration relatively simple.