<?xml version='1.0' encoding='utf-8' ?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Social Memory Complex: rake</title>
<link href="https://www.socialmemorycomplex.net/tags/rake/feed.xml" rel="self" />
<link href="https://www.socialmemorycomplex.net/tags/rake/" />
<updated>2026-05-24T21:17:06+00:00</updated>
<id>https://www.socialmemorycomplex.net/tags/rake/</id>
<entry>
  <title>Add your Rails 3 lib tests to rake:test</title>
  <link href="http://socialmemorycomplex.net/2012/06/27/add-your-rails-3-lib-tests-to-rake-test/" />
  <updated>2012-06-27T00:00:00+00:00</updated>
  <id>http://socialmemorycomplex.net/2012/06/27/add-your-rails-3-lib-tests-to-rake-test/</id>
  <author><name>Jeremy Weiland</name></author>
  <content type="html"><![CDATA[<p>I have a large Rails 3 project with lots of reusable code in modules. Tests for these modules are placed in <code class="language-plaintext highlighter-rouge">test/lib</code> to isolate them from database-heavy model tests. In order to run these tests automatically along with my unit, functional, and integration tests, I implemented the solution described <a href="https://stackoverflow.com/a/1588496">here</a> some time ago. However, at some point over the last year, either Rake or Rails or both broke this (I’m leaning towards Rails, since the new tasks in the Railties gem look much more complex, with special subtasks derived from the <code class="language-plaintext highlighter-rouge">Rake::TestTask</code> class). I’ve been looking for a new approach, and today I got fed up and started fixing it myself.</p>

<p>If you want to run <code class="language-plaintext highlighter-rouge">Test::Unit</code> tests in <code class="language-plaintext highlighter-rouge">test/lib</code>, try putting the following in <code class="language-plaintext highlighter-rouge">lib/tasks/test_lib.rake</code>:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>require 'rubygems'
require 'rake'

namespace :test do
  desc "Test lib modules"
  Rake::TestTask.new(:lib) do |t|    
    t.libs &lt;&lt; "test"
    t.pattern = 'test/lib/**/*_test.rb'
    t.verbose = true    
  end
end

class Rake::Task
  def overwrite(&amp;block)
    @actions.clear
    enhance(&amp;block)
  end
end

Rake::Task["test:run"].overwrite do
  errors = %w(test:units test:functionals test:integration test:lib).collect do |task|
    begin
      Rake::Task[task].invoke
      nil
    rescue =&gt; e
      { :task =&gt; task, :exception =&gt; e }
    end
  end.compact

  if errors.any?
    puts errors.map { |e| "Errors running #{e[:task]}! #{e[:exception].inspect}" }.join("\n")
    abort
  end
end
</code></pre></div></div>

<p>For more information on how the test tasks work, <a href="https://gist.github.com/rails/rails/blob/master/railties/lib/rails/test_unit/testing.rake">examine the source</a>.</p>
]]></content>
</entry>
</feed>
