Nithin Bekal About

Rails tests: Setting up guard with minitest for quicker feedback

03 Jun 2015

For doing test-driven development effectively, it is important to get instant feedback on your tests. For this, I like to configure my project to run the corresponding tests as soon as I save a file.

Guard is a useful tool that can watch for changes to files. Let’s look at how we can set up guard to run tests on a Rails project using Minitest testing framework.

First we add the guard and guard-minitest gems to our Gemfile.

# Add to gemfile:
group :test do
  gem 'guard'
  gem 'guard-minitest'
end

Run bundle install. Now we need to generate a Guardfile which will define what to do when a file gets changed. To generate the Guardfile, run this command:

guard init minitest

This will generate a Guardfile configured to work with Minitest. This will also contain Rails-specific configuration that is commented out. Uncomment the Rails connfiguration and remove the Minitest::Unit configuration. The file should now have something like this:

# Guardfile
guard :minitest do
  watch(%r{^app/(.+)\.rb$})                               { |m| "test/#{m[1]}_test.rb" }
  watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
  watch(%r{^app/controllers/(.+)_controller\.rb$})        { |m| "test/integration/#{m[1]}_test.rb" }
  watch(%r{^app/views/(.+)_mailer/.+})                    { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
  watch(%r{^lib/(.+)\.rb$})                               { |m| "test/lib/#{m[1]}_test.rb" }
  watch(%r{^test/.+_test\.rb$})
  watch(%r{^test/test_helper\.rb$}) { 'test' }
end

Now you can run guard in the terminal and every time you change a file, the corresponding test file will be run.

Links

Hi, I’m Nithin! This is my blog about programming. Ruby is my programming language of choice and the topic of most of my articles here, but I occasionally also write about Elixir, and sometimes about the books I read. You can use the atom feed if you wish to subscribe to this blog or follow me on Mastodon.