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
Class Method Summary
-
.new(name = :test) {|_self| ... } ⇒ TestTask
constructor
Create a testing task.
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.
-
#test_files=(list)
writeonly
Explicitly define the list of test files to be included in a test.
-
#verbose
rw
True if verbose test output desired.
-
#warning
rw
Request that the tests be run with the warning flag set.
Instance Method Summary
-
#define
Create the tasks defined by this task lib.
- #file_list Internal use only
- #file_list_string Internal use only
- #find_dir(fn) Internal use only
- #find_file(fn) Internal use only
- #lib_path Internal use only
- #option_list Internal use only
- #rake_include_arg Internal use only
- #rake_lib_dir Internal use only
- #rake_loader Internal use only
- #ruby_opts_string Internal use only
- #ruby_version Internal use only
- #run_code Internal use only
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 |
#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 |
#rake_check_options | Check that the options do not contain options not listed in |
#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 |
#when_writing | Use this function to prevent potentially destructive ruby code from running when the |
::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 |
#split_all | Split a file path into individual directory names. |
#create_shell_runner, #set_verbose_option, #sh_show_command |
Cloneable
- Included
#initialize_copy | The hook that is invoked by ‘clone’ and ‘dup’ methods. |
Constructor Details
.new(name = :test) {|_self| ... } ⇒ TestTask
Create a testing task.
# File 'lib/rake/testtask.rb', line 86
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.
# File 'lib/rake/testtask.rb', line 75
attr_accessor :deps
#description (rw)
Description of the test task. (default is ‘Run tests’)
# File 'lib/rake/testtask.rb', line 72
attr_accessor :description
#libs (rw)
List of directories added to $LOAD_PATH before running the tests. (default is ‘lib’)
# File 'lib/rake/testtask.rb', line 42
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.
# File 'lib/rake/testtask.rb', line 66
attr_accessor :loader
#name (rw)
Name of test task. (default is :test
)
# File 'lib/rake/testtask.rb', line 38
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)
# File 'lib/rake/testtask.rb', line 50
attr_accessor :
#pattern (rw)
Glob pattern to match test files. (default is ‘test/test*.rb’)
# File 'lib/rake/testtask.rb', line 58
attr_accessor :pattern
#ruby_opts (rw)
Array of command line options to pass to ruby when running test loader.
# File 'lib/rake/testtask.rb', line 69
attr_accessor :ruby_opts
#test_files=(list) (writeonly)
# File 'lib/rake/testtask.rb', line 81
def test_files=(list) @test_files = list end
#verbose (rw)
True if verbose test output desired. (default is false)
# File 'lib/rake/testtask.rb', line 45
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. (default is true)
# File 'lib/rake/testtask.rb', line 55
attr_accessor :warning
Instance Method Details
#define
Create the tasks defined by this task lib.
# File 'lib/rake/testtask.rb', line 108
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}]" = if Rake.application. .trace or @verbose status + details else status end fail end end end end self end
#file_list
#file_list_string
# File 'lib/rake/testtask.rb', line 158
def file_list_string # :nodoc: file_list.map { |fn| "\"#{fn}\"" }.join(" ") end
#find_dir(fn)
# File 'lib/rake/testtask.rb', line 215
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
#find_file(fn)
# File 'lib/rake/testtask.rb', line 193
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
#lib_path
# File 'lib/rake/testtask.rb', line 154
def lib_path # :nodoc: @libs.join(File::PATH_SEPARATOR) end
#option_list
# File 'lib/rake/testtask.rb', line 138
def option_list # :nodoc: (ENV["TESTOPTS"] || ENV["TESTOPT"] || ENV["TEST_OPTS"] || ENV["TEST_OPT"] || @options || "") end
#rake_include_arg
# File 'lib/rake/testtask.rb', line 201
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
#rake_lib_dir
# File 'lib/rake/testtask.rb', line 210
def rake_lib_dir # :nodoc: find_dir("rake") or fail "unable to find rake lib" end
#rake_loader
# File 'lib/rake/testtask.rb', line 188
def rake_loader # :nodoc: find_file("rake/rake_test_loader") or fail "unable to find rake test loader" end
#ruby_opts_string
# File 'lib/rake/testtask.rb', line 147
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
#ruby_version
# File 'lib/rake/testtask.rb', line 173
def ruby_version # :nodoc: RUBY_VERSION end
#run_code
# File 'lib/rake/testtask.rb', line 177
def run_code # :nodoc: case @loader when :direct "-e \"ARGV.each{|f| require f}\"" when :testrb "-S testrb" when :rake "#{rake_include_arg} \"#{rake_loader}\"" end end