Class: Buildr::JUnit

Inherits:
TestFramework::Java show all
Defined in:
lib/buildr/java/tests.rb

Overview

JUnit test framework, the default test framework for Java tests.

Support the following options:

  • :fork – If true/:once (default), fork for each test class. If :each, fork for each individual

    test case.  If false, run all tests in the same VM (fast, but dangerous).
    
  • :clonevm – If true clone the VM each time it is forked.

  • :properties – Hash of system properties available to the test case.

  • :environment – Hash of environment variables available to the test case.

  • :java_args – Arguments passed as is to the JVM.

Defined Under Namespace

Classes: Report

Constant Summary collapse

VERSION =

JUnit version number.

'4.7'

Instance Attribute Summary

Attributes inherited from TestFramework::Base

#options, #task

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TestFramework::Java

applies_to?

Methods inherited from TestFramework::Base

applies_to?, #dependencies, #initialize, to_sym

Constructor Details

This class inherits a constructor from Buildr::TestFramework::Base

Class Method Details

.ant_taskdefObject

:nodoc:



200
201
202
# File 'lib/buildr/java/tests.rb', line 200

def ant_taskdef #:nodoc:
  "org.apache.ant:ant-junit:jar:#{Ant.version}"
end

.dependenciesObject



196
197
198
# File 'lib/buildr/java/tests.rb', line 196

def dependencies
  @dependencies ||= ["junit:junit:jar:#{version}"]+ JMock.dependencies
end

.reportObject

:call-seq:

report()

Returns the Report object used by the junit:report task. You can use this object to set various options that affect your report, for example:

JUnit.report.frames = false
JUnit.report.params['title'] = 'My App'


188
189
190
# File 'lib/buildr/java/tests.rb', line 188

def report
  @report ||= Report.new
end

.versionObject



192
193
194
# File 'lib/buildr/java/tests.rb', line 192

def version
  Buildr.settings.build['junit'] || VERSION
end

Instance Method Details

#run(tests, dependencies) ⇒ Object

:nodoc:



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/buildr/java/tests.rb', line 219

def run(tests, dependencies) #:nodoc:
  # Use Ant to execute the Junit tasks, gives us performance and reporting.
  Buildr.ant('junit') do |ant|
    case options[:fork]
    when false
      forking = {}
    when :each
      forking = { :fork=>true, :forkmode=>'perTest' }
    when true, :once
      forking = { :fork=>true, :forkmode=>'once' }
    else
      fail 'Option fork must be :once, :each or false.'
    end
    mkpath task.report_to.to_s

    taskdef = Buildr.artifact(JUnit.ant_taskdef)
    taskdef.invoke
    ant.taskdef :name=>'junit', :classname=>'org.apache.tools.ant.taskdefs.optional.junit.JUnitTask', :classpath=>taskdef.to_s

    ant.junit forking.merge(:clonevm=>options[:clonevm] || false, :dir=>task.send(:project).path_to) do
      ant.classpath :path=>dependencies.join(File::PATH_SEPARATOR)
      (options[:properties] || []).each { |key, value| ant.sysproperty :key=>key, :value=>value }
      (options[:environment] || []).each { |key, value| ant.env :key=>key, :value=>value }
      Array(options[:java_args]).each { |value| ant.jvmarg :value=>value }
      ant.formatter :type=>'plain'
      ant.formatter :type=>'plain', :usefile=>false # log test
      ant.formatter :type=>'xml'
      ant.batchtest :todir=>task.report_to.to_s, :failureproperty=>'failed' do
        ant.fileset :dir=>task.compile.target.to_s do
          tests.each { |test| ant.include :name=>File.join(*test.split('.')).ext('class') }
        end
      end
    end
    return tests unless ant.project.getProperty('failed')
  end
  # But Ant doesn't tell us what went kaput, so we'll have to parse the test files.
  tests.inject([]) do |passed, test|
    report_file = File.join(task.report_to.to_s, "TEST-#{test}.txt")
    if File.exist?(report_file)
      report = File.read(report_file)
      # The second line (if exists) is the status line and we scan it for its values.
      status = (report.split("\n")[1] || '').scan(/(run|failures|errors):\s*(\d+)/i).
        inject(Hash.new(0)) { |hash, pair| hash[pair[0].downcase.to_sym] = pair[1].to_i ; hash }
      passed << test if status[:failures] == 0 && status[:errors] == 0
    end
    passed
  end
end

#tests(dependencies) ⇒ Object

:nodoc:



212
213
214
215
216
217
# File 'lib/buildr/java/tests.rb', line 212

def tests(dependencies) #:nodoc:
  filter_classes(dependencies,
                 :interfaces => %w{junit.framework.TestCase},
                 :class_annotations => %w{org.junit.runner.RunWith},
                 :method_annotations => %w{org.junit.Test})
end