<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Autotest without Rails Changes</title>
  <id>tag:opensoul.org,2009:/2007/12/6/autotest-without-rails/changes</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://opensoul.org/2007/12/6/autotest-without-rails/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2007/12/6/autotest-without-rails" rel="alternate" type="text/html"/>
  <updated>2007-12-06T14:28:14Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2007-12-06:383:1</id>
    <updated>2007-12-06T14:28:14Z</updated>
    <link href="http://opensoul.org/2009/3/10/autotest-without-rails" rel="alternate" type="text/html"/>
    <title>Autotest without Rails</title>
<content type="html">&lt;p&gt;Autotest is a gem (well, technically ZenTest is a gem that includes autotest). But seriously, autotest is a gem! I can&#8217;t write code without it anymore. It has become my coding security blanket.&lt;/p&gt;


	&lt;p&gt;I started going through withdrawal last night while I was working on a Ruby library because autotest doesn&#8217;t work out of the box with plain ol&#8217; Ruby projects.  It assumes you&#8217;re using one of the Ruby web frameworks.&lt;/p&gt;


	&lt;p&gt;Here&#8217;s how to remedy that.&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;http://rails.aizatto.com/2007/11/19/autotest-ing-your-rails-plugin/&quot;&gt;Aizatto shows us&lt;/a&gt; how to use autotest with your Rails plugins and RSpec.  That&#8217;s a great start, but unfortunately, not all of my projects use RSpec (yet), and not all of them are Rails plugins.&lt;/p&gt;


	&lt;p&gt;To make autotest work with &lt;code&gt;Test::Unit&lt;/code&gt;, we need to tell it how to map files to their tests.  Here is a simple mapping that maps each ruby file in &lt;code&gt;lib/&lt;/code&gt; to the a corresponding test &lt;code&gt;test/*_test.rb&lt;/code&gt;. For example, &lt;code&gt;lib/foo/bar.rb&lt;/code&gt; would map to to a test in &lt;code&gt;test/foo/bar_test.rb&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;# autotest/testunit.rb
require 'autotest'

class Autotest::Testunit &amp;lt; Autotest
  def initialize # :nodoc:
    super
    @exceptions = /^\.\/(?:config|doc|log|tmp|website)/

    @test_mappings = {
      %r%^test/.*\.rb$% =&amp;gt; proc { |filename, _|
        filename
      },
      %r%^lib/(.*)\.rb$% =&amp;gt; proc { |_, m|
        [&amp;quot;test/#{m[1]}_test.rb&amp;quot;]
      },
      %r%^test/test_helper.rb$% =&amp;gt; proc {
        files_matching %r%^test/.*_test\.rb$%
      },
    }
  end

  # Given the string filename as the path, determine
  # the corresponding tests for it, in an array.
  def tests_for_file(filename)
    super.select { |f| @files.has_key? f }
  end
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;We have the mapping, now all we need to do is tell autotest about it.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;# autotest/discover.rb
require File.dirname(__FILE__) + '/testunit'

Autotest.add_discovery { &amp;quot;testunit&amp;quot; }&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Throw both of those files into a &lt;code&gt;autotest/&lt;/code&gt; directory in your plain ol&#8217; Ruby project and autotest should just work for you.&lt;/p&gt;


	&lt;p&gt;Extra credit: Make it work with RSpec on plain ol&#8217; Ruby projects.&lt;/p&gt;</content>  </entry>
</feed>
