123456789_123456789_123456789_123456789_123456789_

Class: Rake::TestTask

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, TaskLib
Instance Chain:
Inherits: Rake::TaskLib
Defined in: lib/rake/testtask.rb

Overview

Create a task that runs a set of tests.

Example:

require "rake/testtask"

Rake::TestTask.new do |t|
  t.libs << "test"
  t.test_files = FileList['test/test*.rb']
  t.verbose = true
end

If rake is invoked with a “TEST=filename” command line option, then the list of test files will be overridden to include only the filename specified on the command line. This provides an easy way to run just one test.

If rake is invoked with a “TESTOPTS=options” command line option, then the given options are passed to the test process after a '–'. This allows Test::Unit options to be passed to the test suite.

Examples:

rake test                           # run tests normally
rake test TEST=just_one_file.rb     # run just one test file.
rake test TESTOPTS="-v"             # run in verbose mode
rake test TESTOPTS="--runner=fox"   # use the fox test runner

Constant Summary

::FileUtils - Included

LN_SUPPORTED, RUBY

FileUtilsExt - Included

DEFAULT

Class Method Summary

Instance Attribute Summary

  • #deps rw

    Task prerequisites.

  • #description rw

    Description of the test task.

  • #libs rw

    List of directories added to $LOAD_PATH before running the tests.

  • #loader rw

    Style of test loader to use.

  • #name rw

    Name of test task.

  • #options rw

    Test options passed to the test suite.

  • #pattern rw

    Glob pattern to match test files.

  • #ruby_opts rw

    Array of command line options to pass to ruby when running test loader.

  • #verbose rw

    True if verbose test output desired.

  • #warning rw

    Request that the tests be run with the warning flag set.

  • #test_files=(list) writeonly

    Explicitly define the list of test files to be included in a test.

Instance Method Summary

  • #define

    Create the tasks defined by this task lib.

DSL - Included

#desc

Describes the next rake task.

#directory

Declare a set of files tasks to create the given directories on demand.

#file

Declare a file task.

#file_create

Declare a file creation task.

#import

Import the partial Rakefiles fn.

#multitask

Declare a task that performs its prerequisites in parallel.

#namespace

Create a new rake namespace and use it for evaluating the given block.

#rule

Declare a rule for auto-tasks.

#task

Declare a basic task.

FileUtilsExt - Included

#nowrite

Get/set the nowrite flag controlling output from the ::FileUtils utilities.

#rake_check_options

Check that the options do not contain options not listed in optdecl.

#rake_merge_option

Merge the given options with the default values.

#rake_output_message

Send the message to the default rake output (which is $stderr).

#verbose

Get/set the verbose flag controlling output from the ::FileUtils utilities.

#when_writing

Use this function to prevent potentially destructive ruby code from running when the :nowrite flag is set.

::FileUtils - Included

#ruby

Run a Ruby interpreter with the given arguments.

#safe_ln

Attempt to do a normal file link, but fall back to a copy if the link fails.

#sh

Run the system command cmd.

#split_all

Split a file path into individual directory names.

Constructor Details

.new(name = :test) {|_self| ... } ⇒ TestTask

Create a testing task.

Yields:

  • (_self)

Yield Parameters:

  • _self (TestTask)

    the object that the method was called on

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 84

def initialize(name=:test)
  @name = name
  @libs = ["lib"]
  @pattern = nil
  @options = nil
  @test_files = nil
  @verbose = false
  @warning = true
  @loader = :rake
  @ruby_opts = []
  @description = "Run tests" + (@name == :test ? "" : " for #{@name}")
  @deps = []
  if @name.is_a?(Hash)
    @deps = @name.values.first
    @name = @name.keys.first
  end
  yield self if block_given?
  @pattern = "test/test*.rb" if @pattern.nil? && @test_files.nil?
  define
end

Instance Attribute Details

#deps (rw)

Task prerequisites.

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 73

attr_accessor :deps

#description (rw)

Description of the test task. (default is 'Run tests')

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 70

attr_accessor :description

#libs (rw)

List of directories added to $LOAD_PATH before running the tests. (default is 'lib')

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 41

attr_accessor :libs

#loader (rw)

Style of test loader to use. Options are:

  • :rake::Rake provided test loading script (default).

  • :testrb – Ruby provided test loading script.

  • :direct – Load tests using command line loader.

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 64

attr_accessor :loader

#name (rw)

Name of test task. (default is :test)

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 37

attr_accessor :name

#options (rw)

Test options passed to the test suite. An explicit TESTOPTS=opts on the command line will override this. (default is NONE)

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 49

attr_accessor :options

#pattern (rw)

Glob pattern to match test files. (default is 'test/test*.rb')

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 56

attr_accessor :pattern

#ruby_opts (rw)

Array of command line options to pass to ruby when running test loader.

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 67

attr_accessor :ruby_opts

#test_files=(list) (writeonly)

Explicitly define the list of test files to be included in a test. list is expected to be an array of file names (a FileList is acceptable). If both #pattern and test_files are used, then the list of test files is the union of the two.

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 79

def test_files=(list)
  @test_files = list
end

#verbose (rw)

True if verbose test output desired. (default is false)

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 44

attr_accessor :verbose

#warning (rw)

Request that the tests be run with the warning flag set. E.g. warning=true implies “ruby -w” used to run the tests.

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 53

attr_accessor :warning

Instance Method Details

#define

Create the tasks defined by this task lib.

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 106

def define
  desc @description
  task @name => Array(deps) do
    FileUtilsExt.verbose(@verbose) do
      puts "Use TESTOPTS=\"--verbose\" to pass --verbose" \
        ", etc. to runners." if ARGV.include? "--verbose"
      args =
        "#{ruby_opts_string} #{run_code} " +
        "#{file_list_string} #{option_list}"
      ruby args do |ok, status|
        if !ok && status.respond_to?(:signaled?) && status.signaled?
          raise SignalException.new(status.termsig)
        elsif !ok
          status  = "Command failed with status (#{status.exitstatus})"
          details = ": [ruby #{args}]"
          message =
            if Rake.application.options.trace or @verbose
              status + details
            else
              status
            end

          fail message
        end
      end
    end
  end
  self
end