minitest setup - Ruby on Rails

I now do testing in Minitest. I came to think it’s better choice to start with minitest for testing Rails. And I thought it would make more sense when I stay out of Minitest::Spec.

my-experience-with-minitest-and-rspec by tenderlove

7-reasons-why-im-sticking-with-minitest-and-fixtures-in-rails by Brandon Hilkert

minitest-quick-reference by Matt Sears

Now I regenerate the setup process with

$ rails new setup_minitest --skip-test-unit
$ cd setup_minitest
$ rails -v
Rails 4.2.1
$ ruby -v
ruby 2.2.2

and then add minitest-rails gem (in test group; look

# Gemfile 
 source ''

+gem 'minitest-rails', group: :test

and go on generate “test” subdirectory

$ bundle install
$ bundle list minitest
$ bundle list minitest-rails

# now you have three more generators

$ rails generate minitest:install
      create  test/test_helper.rb

and this generated file looks like this

# test/test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"

class ActiveSupport::TestCase
  fixtures :all

And now I am ready for minitest. For the last step, try a assert true test.

$ rails generate test_unit:model Roster
      create  test/models/roster_test.rb

and then uncomment the commented lines in this generated file

# test/models/roster_test.rb
require 'test_helper'

class RosterTest < ActiveSupport::TestCase
  test "the truth" do
    assert true

Run the test

$ rake db:migrate
$ rake test
Run options: --seed 48600

# Running:


Finished in 0.005423s, 184.3884 runs/s, 184.3884 assertions/s.

1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

To add colors (for GREEN - RED - REFACTOR), I use minitest-reporters gem

# Gemfile 
 source ''

-gem 'minitest-rails', group: :test
+group :test do
+  gem 'minitest-rails'
+  gem 'minitest-reporters'

# test/test_helper.rb
 require "rails/test_help"
 require "minitest/rails"

+require "minitest/reporters"

I prefer the quiet “default reporter” style. And also I sometimes really need quiet backtrace.

By default, minitest-reporters has “progress reporter” style. And “you must manually tell minitest-reporters which filter to use.”

# config/initializers/backtrace_silencers.rb
+Rails.backtrace_cleaner.add_silencer { |line| line =~ /rbenv/ }

# test/test_helper.rb
 require "minitest/reporters"
+ => true),
+  ENV,
+  Minitest.backtrace_filter

If one more thing, lot of times I use guard-minitest for time saving.

# Gemfile 
+group :development do
+  gem 'guard'
+  gem 'guard-minitest'

And finish its setup, on command line

$ bundle install
$ guard init
19:16:50 - INFO - Writing new Guardfile to /Users/.../setup_minitest/Guardfile
19:16:50 - INFO - minitest guard added to Guardfile, feel free to edit it

And uncomment code lines on this generated Guardfile accordingly. My Rails version is over 4 and I do not use minitest spec.

# Guardfile
guard :minitest do
  # with Minitest::Unit
  watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
  watch(%r{^test/test_helper\.rb$}) { 'test' }

  # Rails 4
  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_helper\.rb$}) { 'test' }

Now I work with Guard on another terminal window.

$ guard
19:25:58 - INFO - Guard::Minitest 2.4.4 is running, with Minitest::Unit 5.6.1!
19:25:58 - INFO - Running: all tests

# Running tests with run options --seed 57094:


Finished tests in 0.004841s, 206.5644 tests/s, 206.5644 assertions/s.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

19:26:02 - INFO - Guard is now watching at '/Users/ugp/tiny_test_apps/setup_minitest'
[1] guard(main)> exit

19:26:05 - INFO - Bye bye...

If another, I may want to do feature test with Capybara, so I add minitest-rails-capybara gem.

# Gemfile
 group :test do
   gem 'minitest-rails'
   gem 'minitest-reporters'
+  gem 'minitest-rails-capybara'

# test/test_helper.rb
 require "rails/test_help"
 require "minitest/rails"

+require "minitest/rails/capybara"

# Rakefile"test:features" => "test:prepare") do |t|
  t.pattern = "test/features/**/*_test.rb"
Rake::Task["test:run"].enhance ["test:features"]

Now I must stop setting up and go back to work, write some test.


Now read this

PostgreSQL for Rails on Apple OS X

This could be a short tutorial to follow along, how to setup PostgreSQL for your development environment. Basically it’s going to be a sharing my experience about, when I tried to understand more of it from my Rails Meetup Study Group. I... Continue →