Thursday, March 02, 2006

Execute all Test::Unit tests in a directory

PickAxe gives an example of how to create test suites in Ruby. It's been awhile since I read it, but if I remember correctly it involves simply adding a require 'test_name' for each test you want to run in your suite. This does work, but it can become a maintenance pain.

When developing a Ruby application I generally put all my tests in a Test folder or in folders that are descendants of Test. When I want to execute all the tests, I simply need to require all files that are in or below Test.
require 'find'

Find.find(File.dirname(__FILE__)) do |path|
Find.prune if path =~ /#{File.expand_path('.')}\/#{__FILE__}$/
require path.reverse.chomp(File.expand_path('.').reverse).chomp('/').reverse.chomp('.rb') if !File.directory?(path)
end
That's it. All files below Test will be required and therefore executed.

3 comments:

  1. Anonymous5:25 AM

    We use the following

    Dir['**/*_test.rb'].each { |testCase| require testCase }

    It is pretty concise. It will require all files in subdirectories. We just end all test files with "_test.rb"

    ReplyDelete
  2. Anonymous7:13 PM

    Hi jay,

    What IDE would you recommend for writing ruby apps/pages please as l have seen a few but nothing that grabs my attention.

    ReplyDelete
  3. Anonymous9:33 PM

    Hello Michael,
    IntelliJ IDEA has many features that are wonderful such as Subversion integration, local history, and JavaScript support. Unfortunately, it does not support Ruby well. However, with a few tricks and a hack I think IntelliJ is your best option.

    ReplyDelete

Note: Only a member of this blog may post a comment.