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
readonly
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.
-
.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:.
-
#framework ⇒ Object
:call-seq: framework => symbol.
-
#include(*names) ⇒ Object
:call-seq: include(*names) => self.
-
#initialize(*args) ⇒ TestTask
constructor
:nodoc:.
-
#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:
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/buildr/core/test.rb', line 186 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 enhance [application.buildfile.name] do run_tests if framework end end |
Instance Attribute Details
#dependencies ⇒ Object (readonly)
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.
206 207 208 |
# File 'lib/buildr/core/test.rb', line 206 def dependencies @dependencies end |
#failed_tests ⇒ Object (readonly)
After running the task, returns all the tests that failed, empty array if all tests passed.
367 368 369 |
# File 'lib/buildr/core/test.rb', line 367 def failed_tests @failed_tests end |
#options ⇒ Object (readonly)
Returns various test options.
291 292 293 |
# File 'lib/buildr/core/test.rb', line 291 def @options end |
#passed_tests ⇒ Object (readonly)
After running the task, returns all the tests that passed, empty array if no tests passed.
369 370 371 |
# File 'lib/buildr/core/test.rb', line 369 def passed_tests @passed_tests end |
#project ⇒ Object (readonly)
The project this task belongs to.
409 410 411 |
# File 'lib/buildr/core/test.rb', line 409 def project @project end |
#tests ⇒ Object (readonly)
After running the task, returns all tests selected to run, based on availability and include/exclude pattern.
365 366 367 |
# File 'lib/buildr/core/test.rb', line 365 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 |
.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.
359 360 361 362 |
# File 'lib/buildr/core/test.rb', line 359 def classes Buildr.application.deprecated 'Call tests instead of classes' tests end |
#classpath ⇒ Object
Deprecated: Use dependencies instead.
209 210 211 212 |
# File 'lib/buildr/core/test.rb', line 209 def classpath Buildr.application.deprecated 'Use dependencies instead.' dependencies end |
#classpath=(artifacts) ⇒ Object
Deprecated: Use dependencies= instead.
215 216 217 218 |
# File 'lib/buildr/core/test.rb', line 215 def classpath=(artifacts) Buildr.application.deprecated 'Use dependencies= instead.' self.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.
247 248 249 |
# File 'lib/buildr/core/test.rb', line 247 def compile(*sources, &block) @project.task('test:compile').from(sources).enhance &block end |
#default_options ⇒ Object
Default options already set on each test task.
182 183 184 |
# File 'lib/buildr/core/test.rb', line 182 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.
353 354 355 356 |
# File 'lib/buildr/core/test.rb', line 353 def exclude(*names) @exclude += names self end |
#execute(args) ⇒ Object
:nodoc:
220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/buildr/core/test.rb', line 220 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] ensure teardown.invoke end end |
#framework ⇒ Object
:call-seq:
framework => symbol
Returns the test framework, e.g. :junit, :testng.
375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/buildr/core/test.rb', line 375 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
343 344 345 346 |
# File 'lib/buildr/core/test.rb', line 343 def include(*names) @include += names self end |
#last_successful_run_file ⇒ Object
The path to the file that stores the time stamp of the last successful test run.
399 400 401 |
# File 'lib/buildr/core/test.rb', line 399 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.
394 395 396 |
# File 'lib/buildr/core/test.rb', line 394 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.
256 257 258 |
# File 'lib/buildr/core/test.rb', line 256 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.
266 267 268 |
# File 'lib/buildr/core/test.rb', line 266 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.
275 276 277 |
# File 'lib/buildr/core/test.rb', line 275 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.
404 405 406 |
# File 'lib/buildr/core/test.rb', line 404 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.
315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/buildr/core/test.rb', line 315 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.
284 285 286 287 288 |
# File 'lib/buildr/core/test.rb', line 284 def with(*artifacts) @dependencies |= Buildr.artifacts(artifacts.flatten).uniq compile.with artifacts self end |