Class: Buildr::Scala::ScalaTest

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

Overview

ScalaTest framework, the default test framework for Scala tests.

Support the following options:

  • :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.

Constant Summary collapse

VERSION =
'1.3'

Instance Attribute Summary collapse

Attributes inherited from TestFramework::Base

#options, #task

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TestFramework::Base

#dependencies, to_sym

Constructor Details

#initialize(test_task, options) ⇒ ScalaTest

Returns a new instance of ScalaTest.



93
94
95
96
97
# File 'lib/buildr/scala/tests.rb', line 93

def initialize(test_task, options)
  super
  @group_includes = []
  @group_excludes = []
end

Instance Attribute Details

#group_excludesObject

annotation-based group exclusion



91
92
93
# File 'lib/buildr/scala/tests.rb', line 91

def group_excludes
  @group_excludes
end

#group_includesObject

annotation-based group inclusion



88
89
90
# File 'lib/buildr/scala/tests.rb', line 88

def group_includes
  @group_includes
end

Class Method Details

.applies_to?(project) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


75
76
77
# File 'lib/buildr/scala/tests.rb', line 75

def applies_to?(project) #:nodoc:
  !Dir[project.path_to(:source, :test, :scala, '**/*.scala')].empty?
end

.dependenciesObject



70
71
72
73
# File 'lib/buildr/scala/tests.rb', line 70

def dependencies
  ["org.scalatest:scalatest:jar:#{version}"] + Check.dependencies +
    JMock.dependencies + JUnit.dependencies
end

.versionObject



66
67
68
# File 'lib/buildr/scala/tests.rb', line 66

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

Instance Method Details

#run(scalatest, dependencies) ⇒ Object

:nodoc:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/buildr/scala/tests.rb', line 103

def run(scalatest, dependencies) #:nodoc:
  mkpath task.report_to.to_s
  success = []

  reporter_options = 'TFGBSAR' # testSucceeded, testFailed, testIgnored, suiteAborted, runStopped, runAborted, runCompleted
  scalatest.each do |suite|
    info "ScalaTest #{suite.inspect}"
    # Use Ant to execute the ScalaTest task, gives us performance and reporting.
    reportDir = task.report_to.to_s
    reportFile = File.join(reportDir, "TEST-#{suite}.txt")
    taskdef = Buildr.artifacts(self.class.dependencies).each(&:invoke).map(&:to_s)
    Buildr.ant('scalatest') do |ant|
      # ScalaTestTask was deprecated in 1.2, in favor of ScalaTestAntTask
      classname = (ScalaTest.version =~ /1\.[01]/) ? \
        'org.scalatest.tools.ScalaTestTask' : 'org.scalatest.tools.ScalaTestAntTask'
      ant.taskdef :name=>'scalatest', :classname=>'org.scalatest.tools.ScalaTestTask',
        :classpath=>taskdef.join(File::PATH_SEPARATOR)
      ant.scalatest :runpath=>dependencies.join(File::PATH_SEPARATOR) do
        ant.suite    :classname=>suite
        ant.reporter :type=>'stdout', :config=>reporter_options
        ant.reporter :type=>'file', :filename=> reportFile, :config=>reporter_options
        ant.reporter :type=>(ScalaTest.version == "1.0") ? "xml" : "junitxml",
                     :directory=> reportDir, :config=>reporter_options
        # TODO: This should be name=>value pairs!
        #ant.includes group_includes.join(" ") if group_includes
        #ant.excludes group_excludes.join(" ") if group_excludes
        (options[:properties] || []).each { |name, value| ant.config :name=>name, :value=>value }
      end
    end

    # Parse for failures, errors, etc.
    # This is a bit of a pain right now because ScalaTest doesn't flush its
    # output synchronously before the Ant test finishes so we have to loop
    # and wait for an indication that the test run was completed.
    failed = false
    completed = false
    wait = 0
    while (!completed) do
      File.open(reportFile, "r") do |input|
        while (line = input.gets) do
          failed = (line =~ /(TESTS? FAILED)|(RUN STOPPED)|(RUN ABORTED)/) unless failed
          completed |= (line =~ /Run completed/)
          break if (failed)
        end
      end
      wait += 1
      break if (failed || wait > 10)
      unless completed
        sleep(1)
      end
    end
    success << suite if (completed && !failed)
  end

  success
end

#tests(dependencies) ⇒ Object

:nodoc:



99
100
101
# File 'lib/buildr/scala/tests.rb', line 99

def tests(dependencies) #:nodoc:
  filter_classes(dependencies, :interfaces => %w{org.scalatest.Suite})
end