Class: Cosmos::TestSuite

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

Overview

class Test

Direct Known Subclasses

UnassignedTestSuite

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTestSuite

Returns a new instance of TestSuite.



282
283
284
285
# File 'lib/cosmos/tools/test_runner/test.rb', line 282

def initialize
  @tests = {}
  @plans = []
end

Instance Attribute Details

#plansObject (readonly)

Returns the value of attribute plans.



280
281
282
# File 'lib/cosmos/tools/test_runner/test.rb', line 280

def plans
  @plans
end

#testsObject (readonly)

Returns the value of attribute tests.



279
280
281
# File 'lib/cosmos/tools/test_runner/test.rb', line 279

def tests
  @tests
end

Instance Method Details

#<=>(other_test_suite) ⇒ Object



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

def <=> (other_test_suite)
  self.name <=> other_test_suite.name
end

#add_test(test_class) ⇒ Object

Add a test to the suite



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

def add_test (test_class)
  test_class = Object.const_get(test_class.to_s.intern) unless test_class.class == Class
  @tests[test_class] = test_class.new unless @tests[test_class]
  @plans << [:TEST, test_class, nil]
end

#add_test_case(test_class, test_case) ⇒ Object

Add a test case to the suite



308
309
310
311
312
# File 'lib/cosmos/tools/test_runner/test.rb', line 308

def add_test_case (test_class, test_case)
  test_class = Object.const_get(test_class.to_s.intern) unless test_class.class == Class
  @tests[test_class] = test_class.new unless @tests[test_class]
  @plans << [:TEST_CASE, test_class, test_case]
end

#add_test_setup(test_class) ⇒ Object

Add a test setup to the suite



315
316
317
318
319
# File 'lib/cosmos/tools/test_runner/test.rb', line 315

def add_test_setup (test_class)
  test_class = Object.const_get(test_class.to_s.intern) unless test_class.class == Class
  @tests[test_class] = test_class.new unless @tests[test_class]
  @plans << [:TEST_SETUP, test_class, nil]
end

#add_test_teardown(test_class) ⇒ Object

Add a test teardown to the suite



322
323
324
325
326
# File 'lib/cosmos/tools/test_runner/test.rb', line 322

def add_test_teardown (test_class)
  test_class = Object.const_get(test_class.to_s.intern) unless test_class.class == Class
  @tests[test_class] = test_class.new unless @tests[test_class]
  @plans << [:TEST_TEARDOWN, test_class, nil]
end

#get_num_testsObject



464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/cosmos/tools/test_runner/test.rb', line 464

def get_num_tests
  num_tests = 0
  @plans.each do |test_type, test_class, test_case|
    case test_type
    when :TEST
      num_tests += test_class.get_num_tests
    when :TEST_CASE, :TEST_SETUP, :TEST_TEARDOWN
      num_tests += 1
    end
  end
  num_tests += 1 if self.class.method_defined?(:setup)
  num_tests += 1 if self.class.method_defined?(:teardown)
  num_tests
end

#nameObject

Name of the test suite



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

def name
  if self.class != TestSuite
    self.class.to_s
  else
    'UnassignedTestSuite'
  end
end

#run(&block) ⇒ Object

Run all the tests



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/cosmos/tools/test_runner/test.rb', line 329

def run (&block)
  TestStatus.instance.total = get_num_tests()
  results = []

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

  # Run each test
  @plans.each do |test_type, test_class, test_case|
    case test_type
    when :TEST
      results.concat(run_test(test_class, true, &block))
    when :TEST_CASE
      result = run_test_case(test_class, test_case, true)
      results << result
      yield result if block_given?
    when :TEST_SETUP
      result = run_test_setup(test_class, true)
      if result
        results << result
        yield result if block_given?
      end
    when :TEST_TEARDOWN
      result = run_test_teardown(test_class, true)
      if result
        results << result
        yield result if block_given?
      end
    end
  end

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

  results
end

#run_setup(internal = false) ⇒ Object



434
435
436
437
438
439
440
441
442
# File 'lib/cosmos/tools/test_runner/test.rb', line 434

def run_setup (internal = false)
  result = nil
  if self.class.method_defined?(:setup) and @tests.length > 0
    TestStatus.instance.total = 1 unless internal
    TestStatus.instance.status = "#{self.class} : setup"
    result = @tests[@tests.keys[0]].run_function(self, :setup)
  end
  result
end

#run_teardown(internal = false) ⇒ Object



444
445
446
447
448
449
450
451
452
# File 'lib/cosmos/tools/test_runner/test.rb', line 444

def run_teardown (internal = false)
  result = nil
  if self.class.method_defined?(:teardown) and @tests.length > 0
    TestStatus.instance.total = 1 unless internal
    TestStatus.instance.status = "#{self.class} : teardown"
    result = @tests[@tests.keys[0]].run_function(self, :teardown)
  end
  result
end

#run_test(test_class, internal = false, &block) ⇒ Object

Run a specific test



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/cosmos/tools/test_runner/test.rb', line 377

def run_test (test_class, internal = false, &block)
  # Determine if this test_class is in the plan and the number of tests associated with this test_class
  in_plan = false
  num_tests = 0
  @plans.each do |plan_test_type, plan_test_class, plan_test_case|
    if plan_test_type == :TEST and test_class == plan_test_class
      in_plan = true
    end
    if (plan_test_type == :TEST_SETUP and test_class == plan_test_class) or
       (plan_test_type == :TEST_TEARDOWN and test_class == plan_test_class) or
       (plan_test_case and test_class == plan_test_class)
      num_tests += 1
    end
  end

  if in_plan
    TestStatus.instance.total = test_class.get_num_tests() unless internal
    results = @tests[test_class].run(&block)
  else
    results = []
    TestStatus.instance.total = num_tests unless internal

    # Run each setup, teardown, or test_case associated with this test_class in the order
    # defined in the plan
    @plans.each do |plan_test_type, plan_test_class, plan_test_case|
      if plan_test_class == test_class
        case plan_test_type
        when :TEST_CASE
          result = run_test_case(plan_test_class, plan_test_case, true)
          results << result
          yield result if block_given?
        when :TEST_SETUP
          result = run_test_setup(plan_test_class, true)
          if result
            results << result
            yield result if block_given?
          end
        when :TEST_TEARDOWN
          result = run_test_teardown(plan_test_class, true)
          if result
            results << result
            yield result if block_given?
          end
        end
      end
    end
  end

  return results
end

#run_test_case(test_class, test_case, internal = false) ⇒ Object

Run a specific test case



429
430
431
432
# File 'lib/cosmos/tools/test_runner/test.rb', line 429

def run_test_case (test_class, test_case, internal = false)
  TestStatus.instance.total = 1 unless internal
  @tests[test_class].run_test_case(test_case)
end

#run_test_setup(test_class, internal = false) ⇒ Object



454
455
456
457
# File 'lib/cosmos/tools/test_runner/test.rb', line 454

def run_test_setup (test_class, internal = false)
  TestStatus.instance.total = 1 unless internal
  @tests[test_class].run_setup
end

#run_test_teardown(test_class, internal = false) ⇒ Object



459
460
461
462
# File 'lib/cosmos/tools/test_runner/test.rb', line 459

def run_test_teardown (test_class, internal = false)
  TestStatus.instance.total = 1 unless internal
  @tests[test_class].run_teardown
end