Search code examples
rubyrake

Running Ruby unit tests with Rake


I was investigating using the Rake build tool to automate running unit tests. I searched the web, but all the examples were for using rails. I usually just write small command-line programs or simple Sinatra applications.

So I came up with the following (probably bad) solution that just emulates what I would do on the command-line: (I just ran one unit test as an example.)

desc 'Run unit tests'
task :test do
    sh 'ruby -I lib test/test_entry.rb'
end
task :default => :test

It works, but I can't help thinking there must be a better way, just writing require 'test/test_entry.rb' doesn't work. I get require problems, Ruby can't find the lib directory, where all the files are.


Solution

  • Use Rake::TestTask http://rake.rubyforge.org/classes/Rake/TestTask.html . Put this into your Rake file and then run rake test:

    require 'rake/testtask'
    
    Rake::TestTask.new do |t|
      t.libs << "test"
      t.test_files = FileList['test/test*.rb']
      t.verbose = true
    end