Write more effective tests.

You’ve been through all the blog posts and tutorials, all the screencasts and programming exercises. You care about your code and want yours to be awesome.

All this time, you’ve had the importance of testing hammered into your head, but you still can’t help but feel that your tests should be doing more for you.

Does any of this sound familiar?

  • You twiddled with your testing stack configuration for weeks to get it working reasonably well, but you’re still not sure you’ve got all the kinks worked out.
  • You know that certain parts of your code base aren’t covered - just because you never could figure out how to test them.
  • That awesome testing framework that lets you write tests that read like English? They just released a new version with a new syntax, and now the docs and all your favorite blogs use examples that look nothing like what you wrote.
  • Your whole test suite used to run in no time at all, but now it’s a minimum of 20 minutes, and you can only afford to run it when you know you’re going to be away from your computer.

Ruby and Rails developers emphasize testing for good reasons, and you want to get on board, but even so, it can be damn hard to learn. You just want to write tests that tell you when you’re breaking something, run fast, and guide you in your work.

Introducing: The Minitest Cookbook

If you want to use even the most simple tool effectively, you need to know how to hold it. That’s why I’m writing The Minitest Cookbook - to help Ruby and Rails developers at all skill levels master the techniques they need to write clear, maintainable tests and help others to do the same. That process begins with a set of fundamentals where you’ll learn how to:

  • Write tests using both assertions and specs
  • Structure your tests for clarity and confidence
  • Validate your code with assertions and expectations
  • Use mocks, stubs, and other kinds of test doubles
  • Effectively manage test data
  • Share common code between tests
  • Run one test case or your whole suite

Once you’ve covered the basics and your feel you’re ready to move to the next level, we’ve got you covered, and so does Minitest. Because even though it’s compact, Minitest is also super flexible and ready to test whatever sort of Ruby code you want to throw at it. It’s based around a simple architecture and supports customizations via plugins, so you’ll be able to trick out your testing stack with third-party gems from a large and growing ecosystem of plugins. Later sections of the book will deal with advanced topics on a deeper level including how to:

  • Test your Rails 5 applications from top to bottom
  • Customize your test reports with better, more actionable information
  • Cover hard-to-reach code that you couldn’t test before
  • Write custom assertions and expectations specifically for your application
  • Build and release your own framework extensions

Why Minitest?

Minitest is a full-featured framework for automated testing that supports both assertion-style and spec-style tests along with basic mocking, performance benchmarking, parallelization, and a simple plugin architecture. All this in around 1500 lines of code compared to over 12,000 for RSpec.

Keeping things simple is the whole point, and it’s one major reason why a lot of developers have been drawn to Minitest. It takes a restrained approach by providing developers with a minimal set of standard assertions and expectations that they can use to test their code or build on with their own creations. For you the developer, that means:

  • A shorter learning curve
  • More readable tests (and fewer WTF moments)
  • Fewer stylistic decisions to make
  • Faster load time for testing jobs

You’ve heard that good things come in small packages. This is what they were talking about.

Table of Contents

  • Introduction
  • How Minitest Works
  • Add Minitest to a Ruby Project
  • Running Your Tests
  • Writing Tests and Specs
  • Configuring Pre-Test State
  • Comparing Things
  • Having Fun with Minitest::Pride & Friends
  • Using Mocks, Stubs and Test Fakes
  • Customizing Test Reports
  • Testing Mixin Behavior
  • Sharing Code Between Tests
  • Continuous Testing
  • Custom Assertions and Expectations
  • Developing Your Own Extensions
  • Adding Minitest to a Rails Project
  • Managing Test Data
  • Testing Rails Models
  • Testing Rails Controllers
  • Testing Rails Helpers
  • Testing Rails Background Jobs
  • Testing Your Rails Application End-to-End

About the author

I’m Chris Kottom, and I’ve been building web applications since the mid-90s. When I first discovered Rails in 2006, I’d been a Java developer for over ten years and was pretty burned out by the experience. Discovering Ruby reminded me how much I loved coding and brought me back to it.

Like a lot of longtime Rubyists, I’ve used plenty of different testing frameworks over the years - Test::Unit, Shoulda, RSpec, and now arriving at Minitest. I think I’ve finally found a way of testing that’s reliable and that I can live with for the long haul. I’m hoping to share that with other developers and help them do the same.

Get in touch with questions or comments via email or Twitter.