How to set up MiniTest?

Viewed 12930

I'm a fairly novice tester, but have been trying to get better at TDD in Rails.

RSpec works great, but my tests are pretty slow. I've heard that MiniTest is a lot faster, and the MiniTest/Spec DSL looks pretty similar to how I'm used to working with RSpec, so I thought I'd give it a try.

However, I have not been able to find anything on the web that provides a walkthrough of how to setup and run Minitest. I learned how to test from the RSpec book, and I have no idea how Test::Unit or MiniTest are supposed to work. I have the gem in my gemfile, I've written a few simple tests, but I have no idea where to put them or how to run them. I figure this is one of those things that's so obvious nobody has bothered to write it down...

Can anyone explain to me how to setup some some Minitest/spec files and get them running so I can compare the performance against Rspec?

EDIT

Specifically these are the basics I most need to know:

  1. Do you need a test_helper file (like spec_helper) and if so how do you create it?
  2. How do you run minitest? There doesn't seem to be an equivalent to rspec spec or rspec path/to/file_spec.rb, what am I missing?

Thanks!

7 Answers

Patrick wrote a really good explanation and example. For future readers just be aware that global expectations are now depreciated, and will be removed in Minitest 6. It requires an almost trivial change in wrapping the thing you are testing in a call to expect.

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
    expect(Alpha.new.greet('Alice')).must_equal('hello, Alice')
  end
end
Related