8yl45w
Last Updated: May 18, 2017
·
4.115K
· gbmoretti
66fbf5d546d5bcc9986e380ecfeb3edb

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! ;)

Say Thanks
Respond

1 Response
Add your response

28938
71ee1809f53df0a2b4dac0530f630933

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.

7 months ago ·