Automatically Run Tests with MiniTest Framework

Install the gems:

gem install guard
gem install guard-minitest

Create the Guardfile:

guard init minitest

The modified Guardfile to use Minitest::Spec:

guard :minitest do
  # with Minitest::Spec
  watch(%r{^spec/(.*)_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})         { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
end

Run the guard in the project folder:

guard

Create a lib folder with a file meme.rb:

class Meme
  def i_can_has_cheezburger?
    "OHAI!"
  end

  def will_it_blend?
    "YES!"
  end
end

Create a spec folder with meme_spec.rb:

require "minitest/autorun"
require_relative '../lib/meme'

describe Meme do
  before do
    @meme = Meme.new
  end

  describe "when asked about cheeseburgers" do
    it "must respond positively" do
      @meme.i_can_has_cheezburger?.must_equal "OHAI!"
    end
  end

  describe "when asked about blending possibilities" do
    it "won't say no" do
      @meme.will_it_blend?.wont_match /^no/i
    end
  end
end

Guard will now automatically run the tests whenever the meme.rb changes.

Color Output

For color output, install:

gem install minitest-reporters

Create spec/spec_helper.rb:

require 'minitest/reporters'

minitest_options = { color: true, detailed_skip: false }
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new( minitest_options )]

Include this file in the tests, output of the tests will now be in color.

References


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.