Last Updated: May 18, 2017
·
10.89K
· gbmoretti

Creating a test rake task with Minitest

A simple way to create a rake task to run with Minitest

First, in your Rakefile (or lib/tasks/test.task or yet tasks/test.task) we need to require the rake/testtask :

require "bundler/gem_tasks"
require "rake/testtask"

Now we can instantiate a new TestTask object, wich receives a block where we can configure the task.

Rake::TestTask.new do |t|
end

In the configuration block, we set the test files that will run in the test task (everything inside tests directory.

Rake::TestTask.new do |t|
    t.test_files = FileList['tests/**/*_test.rb'] #my directory to tests is 'tests' you can change at you will
end
desc "Run tests"

A good practice is to set the test task as default, we can do this adding this line in after creating the TestTask object.
ruby task default: :test
Don't forget to require minitest/autorun to your test files.

The final file looks like this:

require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new do |t|
  t.test_files = FileList['tests/**/*_test.rb']
end
desc "Run tests"

task default: :test

Check other configuration options in the Rake::TestTask documentation.

Hope this helps! ;)

1 Response
Add your response

Thanks for this, I did want to note that typically the folder for tests is simply test without the s. Also from minitest's readme, they prefix files with test_ instead of postfixing them. So if you're following all conventions it ends up being

require "rake/testtask"

Rake::TestTask.new do |t|
  t.test_files = FileList['test/**/test_*.rb']
end
desc "Run tests"

task default: :test

Thanks for the write up, just wanted to save someone else a bit of time.

over 1 year ago ·