Advanced Testing Setup for Ruby on Rails

EC

Esteban Campos / November 06, 2020

2 min read––– views

There are two main elements that we cover in this post: an enhanced pass/fail reporter (Minitest reporters), and an automated test runner that detects file changes and automatically runs the corresponding tests (Guard).

Setup minitest reporters#

Although many systems will show the appropriate colors for red and green test suites, adding minitest reporters lends a degree of pleasant polish to the test outputs.

Ensure that you have the next gems on your Gemfile:

  gem 'minitest', '5.14.2'
  gem 'minitest-reporters', '1.3.8'
test/test_helper.rb
# Other code

# Add the next two lines
require "minitest/reporters"
Minitest::Reporters.use!

class ActiveSupport::TestCase
  .
  .
end

Automated tests with Guard#

Guard monitors changes in the filesystem so that only tests that could be affected are runned again.

Ensure that you have the next gems on your Gemfile:

  gem 'guard', '2.16.2'
  gem 'guard-minitest', '2.4.6'

Initializes Guard:

bundle exec guard init

Then, select a config for the Guardfile, my suggestions is use the recommended by Learn Enough:

Then, run guard to automatically run tests when it detect changes in the project files.

bundle exec guard

Resources#