kmikael
kmikael

Reputation: 5022

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.

Upvotes: 36

Views: 23863

Answers (3)

Artur INTECH
Artur INTECH

Reputation: 7396

In order to run tests written in Minitest:

  1. Create Rakefile (or rakefile, rakefile.rb, Rakefile.rb) file in your app's root with the following contents:
require 'minitest/test_task'

Minitest::TestTask.create

It is assumed that your test files have _test suffix and located under test directory.

  1. Run rake test.

Minitest rake files

Upvotes: 0

DNNX
DNNX

Reputation: 6255

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

Upvotes: 68

KL-7
KL-7

Reputation: 47678

The problem is that your lib directory is not included into ruby's loading path. You can fix it like that:

$:.unshift 'lib'
require 'test_entry'

or more reliable alternative that adds expanded path of lib directory to the loading path:

$:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
require 'test_entry'

Btw, global variable $: has more verbose alias $LOAD_PATH.

Upvotes: 3

Related Questions