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: falsestops Guard from running the whole suite when it starts. It is a documented option, and its default istrue.- Add
spring: "bin/rails test"to theguard :minitestline 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:
- 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.
- Don’t test that code exists.
assert_respond_toon a method you are about to call anyway adds noise. The test that uses the method already proves it. - 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 #
- Add
guardandguard-minitest, runguard init minitest, adjust the Guardfile above. - Time
bin/rails testfor one test file. Decide whether Spring is worth it. - Enable
parallelizeand run the full suite a few times to find shared-state failures. - Check CI runs the same command.
- Ask the team to keep Guard open in a terminal tab for a week, then talk about what annoyed them.
Sources #
- guard-minitest README (install note,
spring: "bin/rails test",all_on_startoption) - Spring README (preloading, binstubs,
spring stop) - Guard
- Rails guides: Testing Rails Applications (parallel testing, running single tests,
-f,bin/rails test:all)


