Skip to content
7rans edited this page Apr 10, 2014 · 7 revisions

Test Runs

Runner Script

Another way to run tests is via rubytest/autorun.rb script. This creates an at_exit runner which will automatically run all tests that have been required. So for example, one could write their own test/runner.rb script.

  require 'rubytest/autorun'
  require 'microtest'

  Dir['test/*_test.rb'].each{ |f| require f }

Then tests can be run simply by running this script.

$ ruby test/runner.rb

For convenience, test.rb provides a shortcut to the rubytest/autorun.rb script so the above could be a little more succinct. It can also be used to run a quick one off test via the ruby command, e.g.

$ ruby -rtest test/test_foo.rb

Command Line Tool

The RubyTest command line tool is a separate gem rubytest-cli. So the first step to running tests on the command line is to $ gem install rubytest-cli or add gem "rubytest-cli" to your project's Gemfile.

Next, when running tests, you need to be sure to load in your test framework or your framework's RubyTest adapter. This is usually done via a helper script in the test files, but it could also be done via command line options, e.g.

$ rubytest -r lemon -r ae test/test_*.rb

The command line tool takes various options, use -h/--help to get a list of all of them.

Sometimes it might not be possible to configure RubyTest properly just via the command line. You need a way to script it. Generally a test helper script that's required by every test can do the trick, but adding such a require to every test isn't particularly DRY. Moreover, you might need to run some code before the tests themselves being to run. The rubytest command will automatically look for .test or etc/test.rb or config/test.rb relative to the project's root directory. and require that file if found. Or, the -c/--config option can be used to specify a configuration file. In such files use the Test.configure interface, like so:

    Test.configure do |run|
      run.format = 'progress'
      run.files << 'test/test_*.rb'
    end

Rake Task

In truth it always best to use a build tool to run tests. Since Rake is the most popular build tool in the Rubydom, RubyTest include a pre-built rake task.

    require 'rubytest/rake'

    Test::RakeTask.new do |run|
      run.format = 'tapy'
      run.files << 'test/*_test.rb'
    end

    task :default => :test

By default this will create a task named test, as you can see by the example, we have attached Rake's default task to it. You can change the name by passing it in as an argument to the new constructor.

Detroit Plugin

(coming soon)

    tester = Detroit::RubyTest.new do |t|
      t.format = 'tapy'
      t.files << 'test/*_test.rb'
    end
    tester.test

Clone this wiki locally