How to set up MiniTest?

This question is similar to How to run all tests with minitest?

Using Ruby 1.9.3 and Rake 0.9.2.2, given a directory layout like this:

Rakefile
lib/alpha.rb
spec/alpha_spec.rb

Here is what alpha_spec.rb might look like:

require 'minitest/spec'
require 'minitest/autorun'  # arranges for minitest to run (in an exit handler, so it runs last)

require 'alpha'

describe 'Alpha' do
  it 'greets you by name' do
    Alpha.new.greet('Alice').must_equal('hello, Alice')
  end
end

And here’s Rakefile

require 'rake'
require 'rake/testtask'

Rake::TestTask.new do |t|
  t.pattern = 'spec/**/*_spec.rb'
end

You can run

  • all tests: rake test
  • one test: ruby -Ilib spec/alpha_spec.rb

I don’t know if using a spec_helper.rb with minitest is common or not. There does not appear to be a convenience method for loading one. Add this to the Rakefile:

require 'rake'
require 'rake/testtask'

Rake::TestTask.new do |t|
  t.pattern = 'spec/**/*_spec.rb'
  t.libs.push 'spec'
end

Then spec/spec_helper.rb can contain various redundant things:

require 'minitest/spec'
require 'minitest/autorun'
require 'alpha'

And spec/alpha_spec.rb replaces the redundant parts with:

require 'spec_helper'
  • all tests: rake test
  • one test: ruby -Ilib -Ispec spec/alpha_spec.rb

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)