title: Generate Rake test tasks dynamically (based on existing test files) in a Rakefile
body: <p>I'm generating test tasks dynamically based on existing test files in a Rakefile. Consider you have various unit test files named after the pattern <code>test_&lt;name&gt;.rb</code>. So what I'm doing is creating a task named after the file name inside the 'test' namespace.
  With the code below I can then call all the tests with <code>rake test:&lt;name&gt;</code></p>
  
  <pre><code>require 'rake/testtask'
  
  task :default =&gt; 'test:all'
  
  namespace :test do
  
    desc "Run all tests"
    Rake::TestTask.new(:all) do |t|
      t.test_files = FileList['test_*.rb']
    end
  
    FileList['test_*.rb'].each do |task|
      name = task.gsub(/test_|\.rb\z/, '')
      desc "Run #{name} tests"
      Rake::TestTask.new(:"#{name}") do |t|
        t.pattern = task
      end
    end
  
  end
  </code></pre>
  
  <p>The above code works, it just seems too much code for simple task generation.
  And I still haven't figured out a way to print some description text to the console like <code>puts "Running #{name} tests:"</code></p>
  
  <p><strong>Is there a more elegant way than the above method?</strong></p>
  
  <p><strong>EDIT:</strong> What I really expected to get was an alternative to the loop to define the tasks dynamically but I guess the rake lib doesn't provide any helper to that so I'm stuck with the loop.</p>