Class: Cosmos::Test

Inherits:
Object show all
Defined in:
lib/cosmos/tools/test_runner/test.rb

Overview

TestGroup class

Constant Summary collapse

@@abort_on_exception =
false
@@current_test_result =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTest

Returns a new instance of Test.



84
85
86
87
88
# File 'lib/cosmos/tools/test_runner/test.rb', line 84

def initialize
  @output_io = StringIO.new('', 'r+')
  $stdout = Stdout.instance
  $stderr = Stderr.instance
end

Class Method Details

.abort_on_exceptionObject



90
91
92
# File 'lib/cosmos/tools/test_runner/test.rb', line 90

def self.abort_on_exception
  @@abort_on_exception
end

.abort_on_exception=(value) ⇒ Object



94
95
96
# File 'lib/cosmos/tools/test_runner/test.rb', line 94

def self.abort_on_exception=(value)
  @@abort_on_exception = value
end

.current_testObject



302
303
304
305
306
307
308
# File 'lib/cosmos/tools/test_runner/test.rb', line 302

def self.current_test
  if @@current_test_result
    @@current_test_result.test
  else
    nil
  end
end

.current_test_caseObject



310
311
312
313
314
315
316
# File 'lib/cosmos/tools/test_runner/test.rb', line 310

def self.current_test_case
  if @@current_test_result
    @@current_test_result.test_case
  else
    nil
  end
end

.current_test_groupObject



294
295
296
297
298
299
300
# File 'lib/cosmos/tools/test_runner/test.rb', line 294

def self.current_test_group
  if @@current_test_result
    @@current_test_result.test
  else
    nil
  end
end

.current_test_suiteObject



286
287
288
289
290
291
292
# File 'lib/cosmos/tools/test_runner/test.rb', line 286

def self.current_test_suite
  if @@current_test_result
    @@current_test_result.test_suite
  else
    nil
  end
end

.get_num_testsObject



269
270
271
272
273
274
275
# File 'lib/cosmos/tools/test_runner/test.rb', line 269

def self.get_num_tests
  num_tests = 0
  num_tests += 1 if self.method_defined?(:setup)
  num_tests += 1 if self.method_defined?(:teardown)
  num_tests += self.test_cases.length
  num_tests
end

.puts(string) ⇒ Object



277
278
279
280
281
282
283
284
# File 'lib/cosmos/tools/test_runner/test.rb', line 277

def self.puts(string)
  $stdout.puts string
  if @@current_test_result
    @@current_test_result.message ||= ''
    @@current_test_result.message << string.chomp
    @@current_test_result.message << "\n"
  end
end

.test_casesObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cosmos/tools/test_runner/test.rb', line 98

def self.test_cases
  # Find all the test cases
  test_cases = []
  self.instance_methods.each do |method_name|
    if method_name.to_s =~ /^test/
      test_cases << method_name.to_s
    end
  end

  # Sort by name
  test_cases.sort!

  test_cases
end

Instance Method Details

#nameObject

Name of the test



114
115
116
117
118
119
120
# File 'lib/cosmos/tools/test_runner/test.rb', line 114

def name
  if self.class != Test
    self.class.to_s.split('::')[-1]
  else
    'UnnamedTest'
  end
end

#runObject

Run all the test cases



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
# File 'lib/cosmos/tools/test_runner/test.rb', line 123

def run
  results = []

  # Setup the test
  result = run_setup()
  if result
    results << result
    yield result if block_given?
    raise StopScript if result.stopped
  end

  # Run each test case
  self.class.test_cases.each do |test_case|
    results << run_test_case(test_case)
    yield results[-1] if block_given?
    raise StopScript if (results[-1].exceptions and @@abort_on_exception) or results[-1].stopped
  end

  # Teardown the test
  result = run_teardown()
  if result
    results << result
    yield result if block_given?
    raise StopScript if result.stopped
  end

  results
end

#run_function(object, method_name) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
# File 'lib/cosmos/tools/test_runner/test.rb', line 158

def run_function(object, method_name)
  # Convert to a symbol to use as a method_name
  method_name = method_name.to_s.intern unless method_name.class == Symbol

  # Create the test result
  result = TestResult.new
  @@current_test_result = result

  # Verify test case exists
  if object.class.method_defined?(method_name)
    # Capture STDOUT and STDERR
    $stdout.add_stream(@output_io)
    $stderr.add_stream(@output_io)

    result.test = object.class.to_s.split('::')[-1]
    result.test_case = method_name.to_s
    begin
      if defined? ScriptRunnerFrame
        if ScriptRunnerFrame.instance
          ScriptRunnerFrame.instance.select_tab_and_destroy_tabs_after_index(0)
        end
      end

      object.send(method_name)
      result.result = :PASS

      if defined? ScriptRunnerFrame
        if ScriptRunnerFrame.instance and ScriptRunnerFrame.instance.exceptions
          result.exceptions = ScriptRunnerFrame.instance.exceptions
          result.result     = :FAIL
          ScriptRunnerFrame.instance.exceptions = nil
        end
      end
    rescue StandardError, SyntaxError => error
      if error.class == StopScript
        result.stopped = true
        result.result  = :STOP
      end
      if error.class == SkipTestCase
        result.result  = :SKIP
        result.message ||= ''
        result.message << error.message
      else
        if defined? ScriptRunnerFrame
          if error.class != StopScript and
            (not ScriptRunnerFrame.instance or
             not ScriptRunnerFrame.instance.exceptions or
             not ScriptRunnerFrame.instance.exceptions.include? error)
            result.exceptions ||= []
            result.exceptions << error
            puts "*** Exception in Control Statement:"
            error.formatted.each_line do |line|
              break if line =~ /test_runner.test.rb/
              puts '  ' + line
            end
          end
          if ScriptRunnerFrame.instance and ScriptRunnerFrame.instance.exceptions
            result.exceptions ||= []
            result.exceptions.concat(ScriptRunnerFrame.instance.exceptions)
            ScriptRunnerFrame.instance.exceptions = nil
          end
        elsif error.class != StopScript
          result.exceptions ||= []
          result.exceptions << error
        end

        result.result = :FAIL if result.exceptions
      end
    ensure
      result.output     = @output_io.string
      @output_io.string = ''
      $stdout.remove_stream(@output_io)
      $stderr.remove_stream(@output_io)

      case result.result
      when :FAIL
        TestStatus.instance.fail_count += 1
      when :SKIP
        TestStatus.instance.skip_count += 1
      when :PASS
        TestStatus.instance.pass_count += 1
      end
    end

  else
    @@current_test_result = nil
    raise "Unknown method #{method_name} for #{object.class}"
  end

  @@current_test_result = nil
  result
end

#run_setupObject



251
252
253
254
255
256
257
258
# File 'lib/cosmos/tools/test_runner/test.rb', line 251

def run_setup
  result = nil
  if self.class.method_defined?(:setup)
    TestStatus.instance.status = "#{self.class} : setup"
    result = run_test_case(:setup)
  end
  result
end

#run_teardownObject



260
261
262
263
264
265
266
267
# File 'lib/cosmos/tools/test_runner/test.rb', line 260

def run_teardown
  result = nil
  if self.class.method_defined?(:teardown)
    TestStatus.instance.status = "#{self.class} : teardown"
    result = run_test_case(:teardown)
  end
  result
end

#run_test_case(test_case) ⇒ Object

Run a specific test case



153
154
155
156
# File 'lib/cosmos/tools/test_runner/test.rb', line 153

def run_test_case(test_case)
  TestStatus.instance.status = "#{self.class} : #{test_case}"
  run_function(self, test_case)
end