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.



75
76
77
78
79
# File 'lib/cosmos/tools/test_runner/test.rb', line 75

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

Class Method Details

.abort_on_exceptionObject



81
82
83
# File 'lib/cosmos/tools/test_runner/test.rb', line 81

def self.abort_on_exception
  @@abort_on_exception
end

.abort_on_exception=(value) ⇒ Object



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

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

.get_num_testsObject



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

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



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

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cosmos/tools/test_runner/test.rb', line 89

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



105
106
107
108
109
110
111
# File 'lib/cosmos/tools/test_runner/test.rb', line 105

def name
  if self.class != Test
    self.class.to_s
  else
    'UnnamedTest'
  end
end

#runObject

Run all the test cases



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

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



149
150
151
152
153
154
155
156
157
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
# File 'lib/cosmos/tools/test_runner/test.rb', line 149

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
    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



242
243
244
245
246
247
248
249
# File 'lib/cosmos/tools/test_runner/test.rb', line 242

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



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

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



144
145
146
147
# File 'lib/cosmos/tools/test_runner/test.rb', line 144

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