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.
-
#forced_need ⇒ Object
Whether the tests are forced.
-
#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
-
.clear ⇒ Object
Used by the test/integration rule to clear all previously included/excluded tests.
-
.exclude(excludes) ⇒ Object
Used by the test/integration to exclude specific tests.
-
.include(includes) ⇒ Object
Used by the test/integration to include specific tests.
-
.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.
-
#clear ⇒ Object
Clear all test includes and excludes and returns self.
-
#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:
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/buildr/core/test.rb', line 224 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.
249 250 251 |
# File 'lib/buildr/core/test.rb', line 249 def dependencies @dependencies end |
#failed_tests ⇒ Object (readonly)
After running the task, returns all the tests that failed, empty array if all tests passed.
417 418 419 |
# File 'lib/buildr/core/test.rb', line 417 def failed_tests @failed_tests end |
#forced_need ⇒ Object
Whether the tests are forced
481 482 483 |
# File 'lib/buildr/core/test.rb', line 481 def forced_need @forced_need end |
#options ⇒ Object (readonly)
Returns various test options.
334 335 336 |
# File 'lib/buildr/core/test.rb', line 334 def @options end |
#passed_tests ⇒ Object (readonly)
After running the task, returns all the tests that passed, empty array if no tests passed.
419 420 421 |
# File 'lib/buildr/core/test.rb', line 419 def passed_tests @passed_tests end |
#project ⇒ Object (readonly)
The project this task belongs to.
478 479 480 |
# File 'lib/buildr/core/test.rb', line 478 def project @project end |
#tests ⇒ Object (readonly)
After running the task, returns all tests selected to run, based on availability and include/exclude pattern.
415 416 417 |
# File 'lib/buildr/core/test.rb', line 415 def tests @tests end |
Class Method Details
.clear ⇒ Object
Used by the test/integration rule to clear all previously included/excluded tests.
188 189 190 191 192 |
# File 'lib/buildr/core/test.rb', line 188 def clear() Project.projects.each do |project| project.test.send :clear end end |
.exclude(excludes) ⇒ Object
Used by the test/integration to exclude specific tests
204 205 206 207 208 209 210 |
# File 'lib/buildr/core/test.rb', line 204 def exclude(excludes) excludes = wildcardify(Array(excludes)) Project.projects.each do |project| project.test.send :exclude, *excludes if excludes.size > 0 project.test.send :forced_need=, true end end |
.include(includes) ⇒ Object
Used by the test/integration to include specific tests
195 196 197 198 199 200 201 |
# File 'lib/buildr/core/test.rb', line 195 def include(includes) includes = wildcardify(Array(includes)) Project.projects.each do |project| project.test.send :include, *includes if includes.size > 0 project.test.send :forced_need=, true end end |
.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 = wildcardify(tests) # 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.
409 410 411 412 |
# File 'lib/buildr/core/test.rb', line 409 def classes Buildr.application.deprecated 'Call tests instead of classes' tests end |
#classpath ⇒ Object
Deprecated: Use dependencies instead.
252 253 254 255 |
# File 'lib/buildr/core/test.rb', line 252 def classpath Buildr.application.deprecated 'Use dependencies instead.' @dependencies end |
#classpath=(artifacts) ⇒ Object
Deprecated: Use dependencies= instead.
258 259 260 261 |
# File 'lib/buildr/core/test.rb', line 258 def classpath=(artifacts) Buildr.application.deprecated 'Use dependencies= instead.' @dependencies = artifacts end |
#clear ⇒ Object
Clear all test includes and excludes and returns self
402 403 404 405 406 |
# File 'lib/buildr/core/test.rb', line 402 def clear @include = [] @exclude = [] self 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.
290 291 292 |
# File 'lib/buildr/core/test.rb', line 290 def compile(*sources, &block) @project.task('test:compile').from(sources).enhance &block end |
#default_options ⇒ Object
Default options already set on each test task.
220 221 222 |
# File 'lib/buildr/core/test.rb', line 220 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.
396 397 398 399 |
# File 'lib/buildr/core/test.rb', line 396 def exclude(*names) @exclude += names self end |
#execute(args) ⇒ Object
:nodoc:
263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/buildr/core/test.rb', line 263 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.
454 455 456 |
# File 'lib/buildr/core/test.rb', line 454 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.
425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/buildr/core/test.rb', line 425 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
386 387 388 389 |
# File 'lib/buildr/core/test.rb', line 386 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.
463 464 465 |
# File 'lib/buildr/core/test.rb', line 463 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.
468 469 470 |
# File 'lib/buildr/core/test.rb', line 468 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.
444 445 446 |
# File 'lib/buildr/core/test.rb', line 444 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.
299 300 301 |
# File 'lib/buildr/core/test.rb', line 299 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.
309 310 311 |
# File 'lib/buildr/core/test.rb', line 309 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.
318 319 320 |
# File 'lib/buildr/core/test.rb', line 318 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.
473 474 475 |
# File 'lib/buildr/core/test.rb', line 473 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.
358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/buildr/core/test.rb', line 358 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.
327 328 329 330 331 |
# File 'lib/buildr/core/test.rb', line 327 def with(*artifacts) @dependencies |= Buildr.artifacts(artifacts.flatten).uniq compile.with artifacts self end |