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
Note that watchr or spork are not requirements for running tests. They're a convenience for doing autotesting. But the most basic way you can run a set of MiniTest tests is with ruby itself:
$ ruby myfileoftests.rb
What version of Ruby are you using?
In my understanding, 1.9 introduced MiniTest
, which replaces Test::Unit
entirely, with a backwards compatible API. So if you're on 1.9 and your Rails app references Test::Unit
, it's actually already using MiniTest
.
Take a look at your Ruby source - test/unit.rb
for 1.9.2 has the following requires:
require 'minitest/unit'
require 'test/unit/assertions'
require 'test/unit/testcase'
And test/unit/assertions
includes MiniTest::Assertions
.
Getting your tests to run faster is a whole other dark art. Take a look at spork and watchr. The former keeps your app initialized, reloads modified files, and forks before each run of your test suite. The latter watches files in your repo for changes, and automatically runs their associated test cases.
Together they give you a pretty mean TDD setup. You write a test case for your new model, and it automatically runs and fails. Then whenever you save the associated model, that testcase re-runs, very quickly. You get near instant feedback on whether you're red/green.
They're both a little tricky to get set up and behaving nicely together, so check back with some more questions if you get stuck.
Best of luck!