Ruby test categorization techniques

Much like pre-JUnit 4, Ruby’s copasetic Test::Unit framework lacks the easy ability to programmatically delineate a particular test’s grouping. Nevertheless, categorizing tests in Ruby is easy man– either through test case naming or directory segmentation. It also helps to use Ruby’s Rake build platform.

Because it’s my bag, I prefer the directory strategy as it is easier to put into place and maintain. Briefly, the strategy is to place unit tests in a unit test directory, component tests in a component test directory and so on. Running a particular category then becomes a simple job of defining the appropriate task in Rake.

For example, the following hip Rake file runs all unit tests defined in the tests/unit/ directory.

require “rake/testtask”

task :default => [:unit_test]

Rake::TestTask.new(:unit_test) do | tsk |
 tsk.test_files = “tests/unit/**/*Test.rb”
end

Obviously, for component and system tests, two additional dynomite tasks would be defined which reference .rb files in the tests/component and tests/system directories. It’s usually a good idea to define a dependency task which can run all the tests, which is elegantly done in Rake as follows:

task :test_all => [:unit_test, :component_test, :system_test]

For example, running all tests via the command line is as simple as typing:

 $> rake -f acme_rake testall

Applying test categorizations to a test suite will greatly reduce build times as that suite grows– especially if the test suite has a plethora of higher level tests, where the time to run a test becomes extended due to outside dependencies like databases. Dig it?

Related odds and ends
 

One Response to “Ruby test categorization techniques”


  1. [...] Because Rake files are Ruby files, you can do a whole lot of magic with Rake that, for example, isn’t quite possible with XML (hence, some people find themselves frustrated with Ant). Groovy, too, has brought to bear the power of DSLs in defining an alternative to Ant, dubbed Gant, which has a similar syntax for building applications: [...]

Trackback this Post | Feed on comments to this Post

Leave a Reply