autotest snippets

How to use RSpec and ZenTest in a standalone Ruby project

Tagged rspec, autotest, zentest, standalone, ruby  Languages ruby

First install the RSpec and ZenTest gem:

$ sudo gem install rspec zentest

Next create the spec folder:

$ cd project_folder
$ mkdir spec

Save the following to spec/helper.rb:

$LOAD_PATH.unshift File.dirname(__FILE__) + '/..'

require 'rubygems'
require 'spec'
#require 'spec/rake/spectask' not needed, because ZenTest supports rspec now

Now create spec/transcoder_spec.rb, and add the following test to it:

require File.dirname(__FILE__) + '/helper'
require File.dirname(__FILE__) + '/../transcoder.rb'

context "Transcoder" do
  setup do
    # Setup your stuff here
  end
  
  it "should support 3gp format" do
    Transcoder.convert("me_and_you.3gp").should == true
  end
end

Let's not forget the class we're testing, put this code in lib/transcoder.rb:

class Transcoder
  def initialize
  end
  
  def self.convert(file)
    return true
  end
end

Note that autotest automatically looks for your code in the lib folder.

Now run the test with the zentest command:

autotest

Change your files and autotest will run the test again.

Tip: Read Getting started with Autotest - Continuous Testing and Setting up autotest to use Growl on OSX for more information on how to increase your productivity.

Test functionality in isolation with Autospec

Tagged autotest, testing, bdd, tdd, autospec, zentest  Languages ruby

This configuration will make autospec run "spec/models/xyz_spec" when a file in "lib/xyz/ is modified". No other tests are run.

Put the following code in ./.autotest:

Autotest.add_hook :initialize do |at|
  at.clear_mappings
  %w{.svn .hg .git vendor}.each {|exception| at.add_exception(exception)}

  at.add_mapping(%r%^lib/xyz/.*\.rb$%) {
    at.files_matching %r%^spec/models/xyz_spec\.rb$%
#
# Uncomment if more tests are needed...
# +   at.files_matching %r%^spec/models/xyz_spec\.rb$%
#
  }
end

Autotest for Sinatra

Tagged watchr, autotest, sinatra, guard  Languages ruby

Guard

Add this to a file named Guardfile in your app directory:

# gem install guard guard-minitest
# https://github.com/guard/guard#readme

guard 'minitest' do
  watch(%r{^app/(.+)\.rb$})   { |m| "test/app/#{m[1]}_test.rb" }
  watch(%r{^lib/(.+)\.rb$})   { |m| "test/lib/#{m[1]}_test.rb" }
  watch(%r{^test/(.+)\.rb$})  { |m| "test/#{m[1]}.rb" }
end

Watchr

Put this in test/test.watchr (inspired by Padrino):

#
# From https://raw.github.com/padrino/padrino-recipes/master/files/watchrs/test.watchr
#
# install watchr
# $ gem install watchr
#
# Run With:
# $ watchr test.watchr
#

# --------------------------------------------------
# Helpers
# --------------------------------------------------

def run(cmd)
  exec = "bundle exec ruby #{cmd}"
  puts(exec)
  system(exec)
end
 
def run_all 
  system("bundle exec rake test")
end

# --------------------------------------------------
# Watchr Rules
# --------------------------------------------------
watch("^lib/(.*)\.rb")                     { |m| run("test/lib/#{m[1]}_test.rb") }

watch("^(.*)/controllers/(.*).rb")           { |m| run("test/#{m[1]}/controllers/#{m[2]}_controller_test.rb") }
watch("^test/(.*)/controllers/(.*)_test.rb") { |m| run("test/#{m[1]}/controllers/#{m[2]}_test.rb") }

watch("^(.*)/models/(.*).rb")                { |m| run("test/#{m[1]}/models/#{m[2]}_test.rb") }
watch("^test/(.*)/(.*)_test.rb")             { |m| run("test/#{m[1]}/models/#{m[2]}_test.rb") }

watch("test.*/test_helper\.rb")              { run_all }
watch("^test/(.*)_test\.rb")                 { |m| run("test/#{m[1]}_test.rb") }

# --------------------------------------------------
# Signal Handling
# --------------------------------------------------
# Ctrl-\ or Ctrl-ö (Swedish keyboard)
Signal.trap('QUIT') do
  puts "Quitting..."
  abort("\n") 
end

# Ctrl-C
Signal.trap('INT') do 
  puts " --- Running all tests ---\n\n"
  run_all
end

# Find your signals...
#Signal.list.each do |name, num|
#  Signal.trap(num) do 
#    puts "#{name}"
#  end rescue "xx"
#end

Put this in .bash_profile or .zsh/aliases:

alias autotest='watchr test/test.watchr'

Install watchr:

gem install watchr

Test your code:

exec # reload profile
autotest

You can also use Guard or autotest. Although improvements have been made during the years, they are all IMHO flawed pieces of software and badly documented.