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.4'

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:



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

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

.dependenciesObject



185
186
187
# File 'lib/buildr/java/tests.rb', line 185

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'


177
178
179
# File 'lib/buildr/java/tests.rb', line 177

def report
  @report ||= Report.new
end

.versionObject



181
182
183
# File 'lib/buildr/java/tests.rb', line 181

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

Instance Method Details

#run(tests, dependencies) ⇒ Object

:nodoc:



208
209
210
211
212
213
214
215
216
217
218
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
# File 'lib/buildr/java/tests.rb', line 208

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:



201
202
203
204
205
206
# File 'lib/buildr/java/tests.rb', line 201

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