JT Book a call

Blog

TDD Workflow Automation for Rails: Guard, Spring, Parallel Tests

In this post

TL;DR: TDD stops when the red-green loop gets slow or needs manual steps. Four small pieces keep it fast in a Rails app: Guard re-runs the matching tests when you save, optional preloading removes boot time, Rails’ built-in parallelize spreads the full suite over cores, and CI runs everything on every push. This post shows each one and what to check before you trust it.

Why TDD Falls Apart #

Teams that say “we practice TDD” often don’t, because the loop is too slow or too manual. If running the test means switching windows, remembering the command and waiting for Rails to boot, you stop running it after every small change. Then you write code in bigger batches, and the tests turn into something you add at the end.

The fix is mostly tooling, not discipline. We have no measured before/after figures to publish for this, so the post makes no speed-up claims. Time your own suite before and after each step.

Layer 1: Guard Runs the Right Tests on Save #

Guard watches files and runs a handler when they change. guard-minitest is the Minitest handler.

# Gemfile
group :development do
  gem 'guard'            # guard-minitest does not depend on guard; add it explicitly
  gem 'guard-minitest'
end
bundle install
bundle exec guard init minitest

The guard-minitest README says that, with Spring, the best command is spring: "bin/rails test". A Guardfile that maps Rails files to their tests:

# Guardfile
guard :minitest, all_on_start: false do
  watch(%r{^test/(.+)_test\.rb$})
  watch('test/test_helper.rb') { 'test' }

  watch(%r{^app/models/(.+)\.rb$}) { |m| "test/models/#{m[1]}_test.rb" }

  watch(%r{^app/controllers/(.+)_controller\.rb$}) do |m|
    "test/controllers/#{m[1]}_controller_test.rb"
  end

  watch(%r{^app/services/(.+)\.rb$}) { |m| "test/services/#{m[1]}_test.rb" }

  watch('config/routes.rb') { 'test/controllers' }
end

Notes:

  • all_on_start: false stops Guard from running the whole suite when it starts. It is a documented option, and its default is true.
  • Add spring: "bin/rails test" to the guard :minitest line only if you use Spring (next section).
  • A mapping can point to a test file that doesn’t exist. Add .select { |t| File.exist?(t) } to any block where that can happen.

Layer 2: Preloading Is Optional #

Rails boot time is paid on every bin/rails test run. Spring keeps the app running in the background, so later runs skip the boot. Its README shows this with a rake test run, and the second run is much quicker; your numbers depend on your app.

If you want it:

bundle exec spring binstub --all   # make bin/* use Spring
bin/spring status
bin/spring stop                    # restart with fresh state

Spring can still hold stale state. When tests fail in a way that makes no sense, run bin/spring stop and try again. If the boot time is small in your app, or Spring causes more confusion than it removes, leave it out. Guard works without it.

Layer 3: Parallel Tests Are Built In #

Rails runs tests in parallel without extra gems. The Rails testing guide says the default method forks processes, that the default worker count is the machine’s core count, and that Active Record creates and loads a database per worker.

# test/test_helper.rb
class ActiveSupport::TestCase
  parallelize(workers: :number_of_processors)
end

You can override the count per run, for example lower on CI and higher on a laptop:

PARALLEL_WORKERS=4 bin/rails test

Parallel runs expose tests that share state (a global, a file on disk, an external service). Fix those rather than turning parallelism off.

Layer 4: Run Only What You Need #

While you work on one thing, run one thing. Guard does this on save. By hand, the Rails test runner takes a file, a line, or a test name:

bin/rails test test/models/user_test.rb
bin/rails test test/models/user_test.rb:12
bin/rails test test/models/user_test.rb -n test_requires_email
bin/rails test -f   # stop at the first failure

All of these come from the Rails testing guide. Save the full suite for before you push.

Layer 5: CI Runs Everything #

Fast local runs only work if something runs the full suite for you. Keep CI simple: install gems, prepare the test database, run bin/rails test. Add bin/rails test:system or switch to bin/rails test:all if you need system tests, since the guide says bin/rails test skips them.

Tests That Survive Refactoring #

Automation makes tests run often. It doesn’t make them good. Three habits keep tests useful:

  1. Test behavior, not implementation. Assert on what a caller sees (a return value, a record, a response), not on which private methods ran. A refactor that keeps behavior should not break a test.
  2. Don’t test that code exists. assert_respond_to on a method you are about to call anyway adds noise. The test that uses the method already proves it.
  3. Don’t test configuration values. Asserting that a setting equals the value written in the config file only repeats the file.

For small steps, write the simplest code that makes the test pass, commit, then refactor with the tests green. Small commits keep the loop short enough that you can revert a bad step.

A One-Hour Setup Order #

  1. Add guard and guard-minitest, run guard init minitest, adjust the Guardfile above.
  2. Time bin/rails test for one test file. Decide whether Spring is worth it.
  3. Enable parallelize and run the full suite a few times to find shared-state failures.
  4. Check CI runs the same command.
  5. Ask the team to keep Guard open in a terminal tab for a week, then talk about what annoyed them.

Sources #

Read next

Tell us what you need built

JetThoughts has been building software since 2008. Book a call, or write to sales@jetthoughts.com. You'll leave the call with a written one-page scope within 2 working days.

#01 Paul Keen
Paul KeenFounder & Open Source Contributor

A 30-minute call; pick a time.