3 # Define a task library for running unit tests.
10 # Create a task that runs a set of tests.
14 # Rake::TestTask.new do |t|
16 # t.test_files = FileList['test/test*.rb']
20 # If rake is invoked with a "TEST=filename" command line option,
21 # then the list of test files will be overridden to include only the
22 # filename specified on the command line. This provides an easy way
23 # to run just one test.
25 # If rake is invoked with a "TESTOPTS=options" command line option,
26 # then the given options are passed to the test process after a
27 # '--'. This allows Test::Unit options to be passed to the test
32 # rake test # run tests normally
33 # rake test TEST=just_one_file.rb # run just one test file.
34 # rake test TESTOPTS="-v" # run in verbose mode
35 # rake test TESTOPTS="--runner=fox" # use the fox test runner
37 class TestTask < TaskLib
39 # Name of test task. (default is :test)
42 # List of directories to added to $LOAD_PATH before running the
43 # tests. (default is 'lib')
46 # True if verbose test output desired. (default is false)
47 attr_accessor :verbose
49 # Test options passed to the test suite. An explicit
50 # TESTOPTS=opts on the command line will override this. (default
52 attr_accessor :options
54 # Request that the tests be run with the warning flag set.
55 # E.g. warning=true implies "ruby -w" used to run the tests.
56 attr_accessor :warning
58 # Glob pattern to match test files. (default is 'test/test*.rb')
59 attr_accessor :pattern
61 # Style of test loader to use. Options are:
63 # * :rake -- Rake provided test loading script (default).
64 # * :testrb -- Ruby provided test loading script.
65 # * :direct -- Load tests using command line loader.
69 # Array of commandline options to pass to ruby when running test loader.
70 attr_accessor :ruby_opts
72 # Explicitly define the list of test files to be included in a
73 # test. +list+ is expected to be an array of file names (a
74 # FileList is acceptable). If both +pattern+ and +test_files+ are
75 # used, then the list of test files is the union of the two.
80 # Create a testing task.
81 def initialize(name=:test)
91 yield self if block_given?
92 @pattern = 'test/test*.rb' if @pattern.nil? && @test_files.nil?
96 # Create the tasks defined by this task lib.
98 lib_path = @libs.join(File::PATH_SEPARATOR)
99 desc "Run tests" + (@name==:test ? "" : " for #{@name}")
102 RakeFileUtils.verbose(@verbose) do
106 "-e 'ARGV.each{|f| load f}'"
112 @ruby_opts.unshift( "-I#{lib_path}" )
113 @ruby_opts.unshift( "-w" ) if @warning
114 ruby @ruby_opts.join(" ") +
115 " \"#{run_code}\" " +
116 file_list.collect { |fn| "\"#{fn}\"" }.join(' ') +
123 def option_list # :nodoc:
124 ENV['TESTOPTS'] || @options || ""
127 def file_list # :nodoc:
129 FileList[ ENV['TEST'] ]
132 result += @test_files.to_a if @test_files
133 result += FileList[ @pattern ].to_a if @pattern
142 def rake_loader # :nodoc:
143 find_file('rake/rake_test_loader') or
144 fail "unable to find rake test loader"
147 def find_file(fn) # :nodoc:
148 $LOAD_PATH.each do |path|
149 file_path = File.join(path, "#{fn}.rb")
150 return file_path if File.exist? file_path