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|
require “rake/testtask”
Example:
Create a task that runs a set of tests.
def define
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
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 += FileList[@pattern].to_a if @pattern result end end
def file_list_string # :nodoc:
def file_list_string # :nodoc: file_list.map { |fn| "\"#{fn}\"" }.join(" ") end
def initialize(name=:test)
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
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 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" when :rake "#{__dir__}/rake_test_loader.rb" 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