Search code examples
rubyrakeruby-2.0rake-test

How do I get Rake TestTask to execute a Ruby script before testing?


I have a testing "bootstrap" Ruby script that handles test setup (requires libraries, prepares test variables etc.) and I want the Rake TestTask to run this file before it runs the tests. Is this possible?

I know that Rake starts the test in a new Ruby instance, so doing setup in the Rakefile won't work. The docs say that I can pass options to the Ruby command using TestTask.ruby_opts, but I haven't been able to find a Ruby option that executes a script file before the main script. I did find:

-rlibrary       require the library before executing your script

But that doesn't let me reference my testing bootstrap script


Solution

  • What you have to do is to include the directory where your script is to the $LOAD_PATH passing the -I options to the ruby command, for example if your script is some_directory/your_script.rb, you can execute it like this:

    Rake::TestTask.new do |t|
      # ...
      t.ruby_opts = ['-I some_directory', '-r your_script']
    end