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 http://bundler.io/groups.html)

https://github.com/blowmage/minitest-rails

# Gemfile 
 source 'https://rubygems.org'

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

and go on generate “test” subdirectory

$ bundle install
$ bundle list minitest
gems/minitest-5.6.1
$ bundle list minitest-rails
gems/minitest-rails-2.1.1

# now you have three more generators
Minitest:
  minitest:generator
  minitest:install
  minitest:route

$ 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
end

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
  end
end

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

https://github.com/kern/minitest-reporters

# Gemfile 
 source 'https://rubygems.org'

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

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

+require "minitest/reporters"
+Minitest::Reporters.use!
+

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"
-Minitest::Reporters.use!
+Minitest::Reporters.use!(
+  Minitest::Reporters::DefaultReporter.new(:color => true),
+  ENV,
+  Minitest.backtrace_filter
+)

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

https://github.com/guard/guard-minitest

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

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{^test/(.*)\/?test_(.*)\.rb$})
  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\.rb$})
  watch(%r{^test/test_helper\.rb$}) { 'test' }
end

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.

https://github.com/blowmage/minitest-rails-capybara

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

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

+require "minitest/rails/capybara"
+

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

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

https://github.com/kangkyu/setup_minitest

 
8
Kudos
 
8
Kudos

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 →