Class: Buildr::TestTask
- Inherits:
-
Rake::Task
- Object
- Rake::Task
- Buildr::TestTask
- Defined in:
- lib/buildr/core/test.rb
Overview
The test task controls the entire test lifecycle.
You can use the test task in three ways. You can access and configure specific test tasks, e.g. enhance the #compile task, or run code during #setup/#teardown.
You can use convenient methods that handle the most common settings. For example, add dependencies using #with, or include only specific tests using #include.
You can also enhance this task directly. This task will first execute the #compile task, followed by the #setup task, run the unit tests, any other enhancements, and end by executing #teardown.
The test framework is determined based on the available test files, for example, if the test cases are written in Java, then JUnit is selected as the test framework. You can also select a specific test framework, for example, to use TestNG instead of JUnit:
test.using :testng
Instance Attribute Summary collapse
-
#dependencies ⇒ Object
The dependencies used for running the tests.
-
#failed_tests ⇒ Object
readonly
After running the task, returns all the tests that failed, empty array if all tests passed.
-
#options ⇒ Object
readonly
Returns various test options.
-
#passed_tests ⇒ Object
readonly
After running the task, returns all the tests that passed, empty array if no tests passed.
-
#project ⇒ Object
readonly
The project this task belongs to.
-
#tests ⇒ Object
readonly
After running the task, returns all tests selected to run, based on availability and include/exclude pattern.
Class Method Summary collapse
-
.only_run(tests) ⇒ Object
Used by the test/integration rule to only run tests that match the specified names.
-
.only_run_failed ⇒ Object
Used by the test/integration rule to only run tests that failed the last time.
-
.run_local_tests(integration) ⇒ Object
Used by the local test and integration tasks to a) Find the local project(s), b) Find all its sub-projects and narrow down to those that have either unit or integration tests, c) Run all the (either unit or integration) tests, and d) Ignore failure if necessary.
Instance Method Summary collapse
-
#classes ⇒ Object
Deprecated: Use tests instead.
-
#classpath ⇒ Object
Deprecated: Use dependencies instead.
-
#classpath=(artifacts) ⇒ Object
Deprecated: Use dependencies= instead.
-
#compile(*sources, &block) ⇒ Object
:call-seq: compile(*sources) => CompileTask compile(*sources) { |task| .. } => CompileTask.
-
#default_options ⇒ Object
Default options already set on each test task.
-
#exclude(*names) ⇒ Object
:call-seq: exclude(*names) => self.
-
#execute(args) ⇒ Object
:nodoc:.
-
#failures_to ⇒ Object
:call-seq: failures_to => file.
-
#framework ⇒ Object
:call-seq: framework => symbol.
-
#include(*names) ⇒ Object
:call-seq: include(*names) => self.
-
#initialize(*args) ⇒ TestTask
constructor
:nodoc:.
-
#last_failures ⇒ Object
:call-seq: last_failures => array.
-
#last_successful_run_file ⇒ Object
The path to the file that stores the time stamp of the last successful test run.
-
#report_to ⇒ Object
:call-seq: report_to => file.
-
#resources(*prereqs, &block) ⇒ Object
:call-seq: resources(*prereqs) => ResourcesTask resources(*prereqs) { |task| .. } => ResourcesTask.
-
#setup(*prereqs, &block) ⇒ Object
:call-seq: setup(*prereqs) => task setup(*prereqs) { |task| .. } => task.
-
#teardown(*prereqs, &block) ⇒ Object
:call-seq: teardown(*prereqs) => task teardown(*prereqs) { |task| .. } => task.
-
#timestamp ⇒ Object
The time stamp of the last successful test run.
-
#using(*args) ⇒ Object
:call-seq: using(options) => self.
-
#with(*artifacts) ⇒ Object
:call-seq: with(*specs) => self.
Methods inherited from Rake::Task
#invoke, #invoke_with_call_chain
Constructor Details
#initialize(*args) ⇒ TestTask
:nodoc:
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/buildr/core/test.rb', line 193 def initialize(*args) #:nodoc: super @dependencies = FileList[] @include = [] @exclude = [] @forced_need = false parent_task = Project.parent_task(name) if parent_task.respond_to?(:options) @options = OpenObject.new { |hash, key| hash[key] = parent_task.[key].clone rescue hash[key] = parent_task.[key] } else @options = OpenObject.new() end unless ENV["IGNORE_BUILDFILE"] =~ /(true)|(yes)/i enhance [ application.buildfile.name ] enhance application.buildfile.prerequisites end enhance do run_tests if framework end end |
Instance Attribute Details
#dependencies ⇒ Object
The dependencies used for running the tests. Includes the compiled files (compile.target) and their dependencies. Will also include anything you pass to #with, shared between the testing compile and run dependencies.
218 219 220 |
# File 'lib/buildr/core/test.rb', line 218 def dependencies @dependencies end |
#failed_tests ⇒ Object (readonly)
After running the task, returns all the tests that failed, empty array if all tests passed.
379 380 381 |
# File 'lib/buildr/core/test.rb', line 379 def failed_tests @failed_tests end |
#options ⇒ Object (readonly)
Returns various test options.
303 304 305 |
# File 'lib/buildr/core/test.rb', line 303 def @options end |
#passed_tests ⇒ Object (readonly)
After running the task, returns all the tests that passed, empty array if no tests passed.
381 382 383 |
# File 'lib/buildr/core/test.rb', line 381 def passed_tests @passed_tests end |
#project ⇒ Object (readonly)
The project this task belongs to.
440 441 442 |
# File 'lib/buildr/core/test.rb', line 440 def project @project end |
#tests ⇒ Object (readonly)
After running the task, returns all tests selected to run, based on availability and include/exclude pattern.
377 378 379 |
# File 'lib/buildr/core/test.rb', line 377 def tests @tests end |
Class Method Details
.only_run(tests) ⇒ Object
Used by the test/integration rule to only run tests that match the specified names.
173 174 175 176 177 178 |
# File 'lib/buildr/core/test.rb', line 173 def only_run(tests) #:nodoc: tests = tests.map { |name| name =~ /\*/ ? name : "*#{name}*" } # Since the tests may reside in a sub-project, we need to set the include/exclude pattern on # all sub-projects, but only invoke test on the local project. Project.projects.each { |project| project.test.send :only_run, tests } end |
.only_run_failed ⇒ Object
Used by the test/integration rule to only run tests that failed the last time.
181 182 183 184 185 |
# File 'lib/buildr/core/test.rb', line 181 def only_run_failed() #:nodoc: # Since the tests may reside in a sub-project, we need to set the include/exclude pattern on # all sub-projects, but only invoke test on the local project. Project.projects.each { |project| project.test.send :only_run_failed } end |
.run_local_tests(integration) ⇒ Object
Used by the local test and integration tasks to a) Find the local project(s), b) Find all its sub-projects and narrow down to those that have either unit or integration tests, c) Run all the (either unit or integration) tests, and d) Ignore failure if necessary.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/buildr/core/test.rb', line 157 def run_local_tests(integration) #:nodoc: Project.local_projects do |project| # !(foo ^ bar) tests for equality and accepts nil as false (and select is less obfuscated than reject on ^). projects = ([project] + project.projects).select { |project| !(project.test.[:integration] ^ integration) } projects.each do |project| info "Testing #{project.name}" begin project.test.invoke rescue raise unless Buildr..test == :all end end end end |
Instance Method Details
#classes ⇒ Object
Deprecated: Use tests instead.
371 372 373 374 |
# File 'lib/buildr/core/test.rb', line 371 def classes Buildr.application.deprecated 'Call tests instead of classes' tests end |
#classpath ⇒ Object
Deprecated: Use dependencies instead.
221 222 223 224 |
# File 'lib/buildr/core/test.rb', line 221 def classpath Buildr.application.deprecated 'Use dependencies instead.' @dependencies end |
#classpath=(artifacts) ⇒ Object
Deprecated: Use dependencies= instead.
227 228 229 230 |
# File 'lib/buildr/core/test.rb', line 227 def classpath=(artifacts) Buildr.application.deprecated 'Use dependencies= instead.' @dependencies = artifacts end |
#compile(*sources, &block) ⇒ Object
:call-seq:
compile(*sources) => CompileTask
compile(*sources) { |task| .. } => CompileTask
The compile task is similar to the Project’s compile task. However, it compiles all files found in the src/test/source directory into the target/test/code directory. This task is executed by the test task before running any tests.
Once the project definition is complete, all dependencies from the regular compile task are copied over, so you only need to specify dependencies specific to your tests. You can do so by calling #with on the test task. The dependencies used here are also copied over to the junit task.
259 260 261 |
# File 'lib/buildr/core/test.rb', line 259 def compile(*sources, &block) @project.task('test:compile').from(sources).enhance &block end |
#default_options ⇒ Object
Default options already set on each test task.
189 190 191 |
# File 'lib/buildr/core/test.rb', line 189 def { :fail_on_failure=>true, :fork=>:once, :properties=>{}, :environment=>{} } end |
#exclude(*names) ⇒ Object
:call-seq:
exclude(*names) => self
Exclude the specified tests. This method accepts multiple arguments and returns self. See #include for the type of arguments you can use.
365 366 367 368 |
# File 'lib/buildr/core/test.rb', line 365 def exclude(*names) @exclude += names self end |
#execute(args) ⇒ Object
:nodoc:
232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/buildr/core/test.rb', line 232 def execute(args) #:nodoc: if Buildr..test == false info "Skipping tests for #{project.name}" return end setup.invoke begin super rescue RuntimeError raise if [:fail_on_failure] && Buildr..test != :all ensure teardown.invoke end end |
#failures_to ⇒ Object
:call-seq:
failures_to => file
We record the list of failed tests for the current framework in this file.
416 417 418 |
# File 'lib/buildr/core/test.rb', line 416 def failures_to @failures_to ||= file(@project.path_to(:target, "#{framework}-failed")=>self) end |
#framework ⇒ Object
:call-seq:
framework => symbol
Returns the test framework, e.g. :junit, :testng.
387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/buildr/core/test.rb', line 387 def framework unless @framework # Start with all frameworks that apply (e.g. JUnit and TestNG for Java), # and pick the first (default) one, unless already specified in parent project. candidates = TestFramework.frameworks.select { |cls| cls.applies_to?(@project) } candidate = @project.parent && candidates.detect { |framework| framework.to_sym == @project.parent.test.framework } || candidates.first self.framework = candidate if candidate end @framework && @framework.class.to_sym end |
#include(*names) ⇒ Object
:call-seq:
include(*names) => self
Include only the specified tests. Unless specified, the default is to include all tests identified by the test framework. This method accepts multiple arguments and returns self.
Tests are specified by their full name, but you can use glob patterns to select multiple tests, for example:
test.include 'com.example.FirstTest' # FirstTest only
test.include 'com.example.*' # All tests under com/example
test.include 'com.example.Module*' # All tests starting with Module
test.include '*.{First,Second}Test' # FirstTest, SecondTest
355 356 357 358 |
# File 'lib/buildr/core/test.rb', line 355 def include(*names) @include += names self end |
#last_failures ⇒ Object
:call-seq:
last_failures => array
We read the last test failures if any and return them.
425 426 427 |
# File 'lib/buildr/core/test.rb', line 425 def last_failures @last_failures ||= failures_to.exist? ? File.read(failures_to.to_s).split('\n') : [] end |
#last_successful_run_file ⇒ Object
The path to the file that stores the time stamp of the last successful test run.
430 431 432 |
# File 'lib/buildr/core/test.rb', line 430 def last_successful_run_file #:nodoc: File.join(report_to.to_s, 'last_successful_run') end |
#report_to ⇒ Object
:call-seq:
report_to => file
Test frameworks that can produce reports, will write them to this directory.
This is framework dependent, so unless you use the default test framework, call this method after setting the test framework.
406 407 408 |
# File 'lib/buildr/core/test.rb', line 406 def report_to @report_to ||= file(@project.path_to(:reports, framework)=>self) end |
#resources(*prereqs, &block) ⇒ Object
:call-seq:
resources(*prereqs) => ResourcesTask
resources(*prereqs) { |task| .. } => ResourcesTask
Executes by the #compile task to copy resource files over. See Project#resources.
268 269 270 |
# File 'lib/buildr/core/test.rb', line 268 def resources(*prereqs, &block) @project.task('test:resources').enhance prereqs, &block end |
#setup(*prereqs, &block) ⇒ Object
:call-seq:
setup(*prereqs) => task
setup(*prereqs) { |task| .. } => task
Returns the setup task. The setup task is executed at the beginning of the test task, after compiling the test files.
278 279 280 |
# File 'lib/buildr/core/test.rb', line 278 def setup(*prereqs, &block) @project.task('test:setup').enhance prereqs, &block end |
#teardown(*prereqs, &block) ⇒ Object
:call-seq:
teardown(*prereqs) => task
teardown(*prereqs) { |task| .. } => task
Returns the teardown task. The teardown task is executed at the end of the test task.
287 288 289 |
# File 'lib/buildr/core/test.rb', line 287 def teardown(*prereqs, &block) @project.task('test:teardown').enhance prereqs, &block end |
#timestamp ⇒ Object
The time stamp of the last successful test run. Or Rake::EARLY if no successful test run recorded.
435 436 437 |
# File 'lib/buildr/core/test.rb', line 435 def #:nodoc: File.exist?(last_successful_run_file) ? File.mtime(last_successful_run_file) : Rake::EARLY end |
#using(*args) ⇒ Object
:call-seq:
using() => self
Sets various test options from a hash and returns self. For example:
test.using :fork=>:each, :properties=>{ 'url'=>'http://localhost:8080' }
Can also be used to select the test framework, or to run these tests as integration tests. For example:
test.using :testng
test.using :integration
The :fail_on_failure option specifies whether the task should fail if any of the tests fail (default), or should report the failures but continue running the build (when set to false).
All other options depend on the capability of the test framework. These options should be used the same way across all frameworks that support them:
-
:fork – Fork once for each project (:once, default), for each test in each
project (:each), or don't fork at all (false).
-
:properties – Properties pass to the test, e.g. in Java as system properties.
-
:environment – Environment variables. This hash is made available in the
form of environment variables.
327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/buildr/core/test.rb', line 327 def using(*args) args.pop.each { |key, value| [key.to_sym] = value } if Hash === args.last args.each do |name| if TestFramework.has?(name) self.framework = name elsif name == :integration [:integration] = true else Buildr.application.deprecated "Please replace with using(:#{name}=>true)" [name.to_sym] = true end end self end |
#with(*artifacts) ⇒ Object
:call-seq:
with(*specs) => self
Specify artifacts (specs, tasks, files, etc) to include in the dependencies list when compiling and running tests.
296 297 298 299 300 |
# File 'lib/buildr/core/test.rb', line 296 def with(*artifacts) @dependencies |= Buildr.artifacts(artifacts.flatten).uniq compile.with artifacts self end |