class Rake::TestTask
rake test TESTOPTS=“–runner=fox” # use the fox test runner
rake test TESTOPTS=“-v” # run in verbose mode
rake test TEST=just_one_file.rb # run just one test file.
rake test # run tests normally
Examples:
suite.
‘–’. This allows Test::Unit options to be passed to the test
then the given options are passed to the test process after a
If rake is invoked with a “TESTOPTS=options” command line option,
to run just one test.
filename specified on the command line. This provides an easy way
then the list of test files will be overridden to include only the
If rake is invoked with a “TEST=filename” command line option,
end
t.verbose = true
t.test_files = FileList[‘test/test*.rb’]
t.libs << “test”
Rake::TestTask.new do |t|
Example:
Create a task that runs a set of tests.
def define
def define desc @description task @name do FileUtilsExt.verbose(@verbose) do 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 fail "Command failed with status (#{status.exitstatus}): " + "[ruby #{args}]" end end end end self end
def file_list # :nodoc:
def file_list # :nodoc: if ENV['TEST'] FileList[ENV['TEST']] else result = [] result += @test_files.to_a if @test_files result << @pattern if @pattern result end end
def file_list_string # :nodoc:
def file_list_string # :nodoc: file_list.map { |fn| "\"#{fn}\"" }.join(' ') end
def find_dir(fn) # :nodoc:
def find_dir(fn) # :nodoc: $LOAD_PATH.each do |path| file_path = File.join(path, "#{fn}.rb") return path if File.exist? file_path end nil end
def find_file(fn) # :nodoc:
def find_file(fn) # :nodoc: $LOAD_PATH.each do |path| file_path = File.join(path, "#{fn}.rb") return file_path if File.exist? file_path end nil end
def fix # :nodoc:
def fix # :nodoc: case ruby_version when '1.8.2' "\"#{find_file 'rake/ruby182_test_unit_fix'}\"" else nil end || '' end
def initialize(name=:test)
def initialize(name=:test) @name = name @libs = ["lib"] @pattern = nil @options = nil @test_files = nil @verbose = false @warning = false @loader = :rake @ruby_opts = [] @description = "Run tests" + (@name == :test ? "" : " for #{@name}") yield self if block_given? @pattern = 'test/test*.rb' if @pattern.nil? && @test_files.nil? define end
def lib_path # :nodoc:
def lib_path # :nodoc: @libs.join(File::PATH_SEPARATOR) end
def option_list # :nodoc:
def option_list # :nodoc: (ENV['TESTOPTS'] || ENV['TESTOPT'] || ENV['TEST_OPTS'] || ENV['TEST_OPT'] || @options || "") end
def rake_include_arg # :nodoc:
def rake_include_arg # :nodoc: spec = Gem.loaded_specs['rake'] if spec.respond_to?(:default_gem?) && spec.default_gem? "" else "-I\"#{rake_lib_dir}\"" end end
def rake_lib_dir # :nodoc:
def rake_lib_dir # :nodoc: find_dir('rake') or fail "unable to find rake lib" end
def rake_loader # :nodoc:
def rake_loader # :nodoc: find_file('rake/rake_test_loader') or fail "unable to find rake test loader" end
def ruby_opts_string # :nodoc:
def ruby_opts_string # :nodoc: opts = @ruby_opts.dup opts.unshift("-I\"#{lib_path}\"") unless @libs.empty? opts.unshift("-w") if @warning opts.join(" ") end
def ruby_version # :nodoc:
def ruby_version # :nodoc: RUBY_VERSION end
def run_code # :nodoc:
def run_code # :nodoc: case @loader when :direct "-e \"ARGV.each{|f| require f}\"" when :testrb "-S testrb #{fix}" when :rake "#{rake_include_arg} \"#{rake_loader}\"" end end
def test_files=(list)
FileList is acceptable). If both +pattern+ and +test_files+ are
test. +list+ is expected to be an array of file names (a
Explicitly define the list of test files to be included in a
def test_files=(list) @test_files = list end