Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7589239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:06:40+00:00 2026-05-30T20:06:40+00:00

I’m generating test tasks dynamically based on existing test files in a Rakefile. Consider

  • 0

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 test_<name>.rb. 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 rake test:<name>

require 'rake/testtask'

task :default => '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

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 puts "Running #{name} tests:"

Is there a more elegant way than the above method?

EDIT: 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:06:41+00:00Added an answer on May 30, 2026 at 8:06 pm

    Here’s another way to solve the problem using rules in Rake.

    A rake rule kicks in whenever rake wants to build “X”, and it finds a rules that says “to build X, use Y”. We will setup a rule that triggers when someone specifies a target in the format “test:XXX”, it will attempt to use a file named “test/test_XXX.rb”.

    require 'rake/testtask'
    
    task :default => 'test:all'
    
    TEST_FILES = FileList['test/test_*.rb']
    
    namespace :test do
      desc "Run all tests"
      Rake::TestTask.new(:all) do |t|
        t.test_files = TEST_FILES
      end
    
      rule /^test:/ => lambda { |tn| "test/test_%s.rb" % tn.gsub(/^test:/,'') } do |rule|
        ruby rule.source
      end
    end
    

    Suppose you have a test file named “test/test_my_code.rb”. To execute that test file, just type:

    rake test:my_code
    

    The rule is triggered whenever there is a target beginning with “test:” that cannot matched by any other task. It then looks for a file given by the lamdba function. The lambda transforms the target name “test:XXX” into a filename “test/test_XXX.rb”. If the filename exists, the body of the rule is executed.

    The body of the rule just runs the test file as an executable. This is generally enough to run the tests of a single file. If you need to add library paths (e.g. “lib”) to the load path for the tests, you can change the rule body to be something like

    ruby "-Ilib", rule.source
    

    Another difference between this and the explicit loop solution is that rake will not print out descriptions for rules, so the “rake -T” output won’t include the individual tests in its output.

    I don’t know if this is better than the the original, but it does give you some options.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.