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.



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

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

Instance Attribute Details

#plansObject (readonly)

Returns the value of attribute plans.



321
322
323
# File 'lib/cosmos/tools/test_runner/test.rb', line 321

def plans
  @plans
end

#testsObject (readonly)

Returns the value of attribute tests.



320
321
322
# File 'lib/cosmos/tools/test_runner/test.rb', line 320

def tests
  @tests
end

Instance Method Details

#<=>(other_test_suite) ⇒ Object



328
329
330
# File 'lib/cosmos/tools/test_runner/test.rb', line 328

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

#add_test(test_class) ⇒ Object

Add a test to the suite



342
343
344
345
346
# File 'lib/cosmos/tools/test_runner/test.rb', line 342

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



349
350
351
352
353
# File 'lib/cosmos/tools/test_runner/test.rb', line 349

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



356
357
358
359
360
# File 'lib/cosmos/tools/test_runner/test.rb', line 356

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



363
364
365
366
367
# File 'lib/cosmos/tools/test_runner/test.rb', line 363

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



523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/cosmos/tools/test_runner/test.rb', line 523

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



333
334
335
336
337
338
339
# File 'lib/cosmos/tools/test_runner/test.rb', line 333

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

#run(&block) ⇒ Object

Run all the tests



370
371
372
373
374
375
376
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
# File 'lib/cosmos/tools/test_runner/test.rb', line 370

def run(&block)
  TestResult.test_suite = name()
  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

  TestResult.test_suite = nil
  results
end

#run_setup(internal = false) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
# File 'lib/cosmos/tools/test_runner/test.rb', line 483

def run_setup(internal = false)
  TestResult.test_suite = name() unless internal
  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
  TestResult.test_suite = nil unless internal
  result
end

#run_teardown(internal = false) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
# File 'lib/cosmos/tools/test_runner/test.rb', line 495

def run_teardown(internal = false)
  TestResult.test_suite = name() unless internal
  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
  TestResult.test_suite = nil unless internal
  result
end

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

Run a specific test



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/cosmos/tools/test_runner/test.rb', line 420

def run_test(test_class, internal = false, &block)
  TestResult.test_suite = name() unless internal

  # 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

  TestResult.test_suite = nil unless internal
  return results
end

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

Run a specific test case



475
476
477
478
479
480
481
# File 'lib/cosmos/tools/test_runner/test.rb', line 475

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

#run_test_setup(test_class, internal = false) ⇒ Object



507
508
509
510
511
512
513
# File 'lib/cosmos/tools/test_runner/test.rb', line 507

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

#run_test_teardown(test_class, internal = false) ⇒ Object



515
516
517
518
519
520
521
# File 'lib/cosmos/tools/test_runner/test.rb', line 515

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