Excluding specific tests with rake

In: Ruby|Ruby on Rails

5 Sep 2006

Sometimes, I just want to skip just that one test when I run my tests with rake test because it takes much longer than other tests, and it gets in the way of rapid iterative cycles when coding. I looked around and haven’t found a way to do that with rake, so I set down to writing a custom rake task.

namespace :test do
  namespace :units do
    Rake::TestTask.new(:excluding => 'db:test:prepare') do |t|
      if ENV['THESE']
        def t.file_list
          exclusion_patterns = ENV['THESE'].split(',')
          unit_tests = FileList['test/unit/**/*_test.rb'].select do |path|
            excluded = exclusion_patterns.select { |excl| /#{excl}/.match(path) }
            p "Excluding #{path}" if not excluded.empty?
            excluded.empty?
          end
        end
      else
        p 'Running all unit tests (specify THESE="pattern1, pattern2" to exclude files)'
        t.pattern = 'test/unit/**/*_test.rb'
      end
      t.libs << 'test'
      t.verbose = true
    end
    Rake::Task['test:units:excluding'].comment = 'Run unit tests with exclusions (specify exclusions with THESE="pattern1,pattern2")'
  end
end

Of course, rake test:recent and rake test:uncommitted would suffice for most purposes, but there are times when I just want to not run that a particular test or tests because they just take too long (and by definition, get in the way) and I'm pretty sure they won't be broken. (Always run all the tests before a commit though, don't be naughty.)

You can run the task like this:

rake test:units:excluding THESE="item_test,atte\w*"

Update: As it turns out, the task descriptions weren't being set correctly. Revision 5016 fixes this so rake -T shows the task descriptions properly.

Comments are closed.