The Minitest Cookbook

Published on

I’m not a professional developer (at least not as of 2017-01-26), but I’ve always been enamored with the idea of testing. The first time I jumped into testing, I went straight for the Rspec/Cucumber stack. Rspec seemed to be the thing that all the serious developers used, and Cucumber seemed like an interesting way to write tests. I tried both. Rspec is a fine system, but it can feel overwhelming at times. There are almost too many options. Cucumber can be a good way to go, if you need to develop tests with non-developers, but I haven’t had a need for it, and using Cucumber when it’s just you isn’t necessary.

I started writing a Rails app for the first time again recently. It’s called Stocker. I definitely wanted to write a test suite for this app, as I want it to be an example of my skills for job hunting purposes, and test should help keep it from developing bugs in the future. This time around though, I thought it would be best to avoid the same pitfalls from my first forays with testing. I like the Rspec syntax, but I thought I could get most of the benefits of Rspec from Minitest::Spec and I since I’m the stakeholder, I don’t need Cucumber. I wanted to get a good grasp of what Minitest can do, so I wanted a guide. I remembered Chris Kottom’s Minitest Cheatsheet from the end of 2016 and I looked it up again and was reminded that he wrote a book called The Minitest Cookbook.

Over the course of the book, he has you add some extra gems and extensions to Minitest to add some helpful features, but I think he does a great job of how functional and how flexible Minitest is. He shows you how to use the spec syntax, how to extend Minitest’s reporting capabilities, and how to set it up to enhance Rails testing. Sure, you aren’t using vanilla Minitest, but you don’t need any of them. I also really like that you can mix and match assert-style and spec-syntax styles any time you like.

require 'minitest/autorun'

describe "Foo" do
  let(:one) {1}

  it "is cool" do
    expect(one).must_equal 1

  def test_something
    assert_equal one, 1

Save as foo.rb and run with ruby -Iminitest foo.rb and both tests pass. That’s pretty cool. I really like the Minitest reporters gem. I output the spec reporter and the HTML reports on every run. Also, minitest-rails will enable the spec syntax with Rails and the minitest-rails-capybara gem will get Capybara working. Very cool. If you haven’t given Minitest a fair shot before, check out The Minitest Cookbook.