Class: Beaker::TestSuite

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/test_suite.rb

Overview

This Class is in need of some cleaning up beyond what can be quickly done. Things to keep in mind:

* Global State Change
* File Creation Relative to CWD  -- Should be a config option
* Better Method Documentation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, hosts, options, config, fail_mode = nil) ⇒ TestSuite



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/beaker/test_suite.rb', line 21

def initialize(name, hosts, options, config, fail_mode = nil)
  @name      = name.gsub(/\s+/, '-')
  @hosts     = hosts
  @run       = false
  @options   = options
  @config    = config
  @fail_mode = @options[:fail_mode] || fail_mode
  @logger    = options[:logger]

  @test_cases = []
  @test_files = []

  options[:tests].each do |root|
    if File.file? root then
      @test_files << root
    else
      @test_files += Dir.glob(
        File.join(root, "**/*.rb")
      ).select { |f| File.file?(f) }
    end
  end
  report_and_raise(@logger, RuntimeError.new("#{@name}: no test files found..."), "TestSuite: initialize") if @test_files.empty?

  @test_files = @test_files.sort
rescue => e
  report_and_raise(@logger, e, "TestSuite: initialize")
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/beaker/test_suite.rb', line 19

def config
  @config
end

#fail_modeObject (readonly)

Returns the value of attribute fail_mode.



19
20
21
# File 'lib/beaker/test_suite.rb', line 19

def fail_mode
  @fail_mode
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/beaker/test_suite.rb', line 19

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/beaker/test_suite.rb', line 19

def options
  @options
end

Instance Method Details

#errored_testsObject



123
124
125
# File 'lib/beaker/test_suite.rb', line 123

def errored_tests
  @errored_tests ||= @test_cases.select { |c| c.test_status == :error }.length
end

#fail_without_test_runObject



102
103
104
# File 'lib/beaker/test_suite.rb', line 102

def fail_without_test_run
  report_and_raise(@logger, RuntimeError.new("#{@name}: you have not run the tests yet"), "TestSuite: fail_without_test_run") unless @run
end

#failed?Boolean



111
112
113
# File 'lib/beaker/test_suite.rb', line 111

def failed?
  !success?
end

#failed_testsObject



127
128
129
# File 'lib/beaker/test_suite.rb', line 127

def failed_tests
  @failed_tests ||= @test_cases.select { |c| c.test_status == :fail }.length
end

#passed_testsObject



119
120
121
# File 'lib/beaker/test_suite.rb', line 119

def passed_tests
  @passed_tests ||= @test_cases.select { |c| c.test_status == :pass }.length
end

#pending_testsObject



135
136
137
# File 'lib/beaker/test_suite.rb', line 135

def pending_tests
  @pending_tests ||= @test_cases.select {|c| c.test_status == :pending}.length
end

#runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/beaker/test_suite.rb', line 49

def run
  @run = true
  @start_time = Time.now

  configure_logging 

  @test_files.each do |test_file|
    @logger.notify
    @logger.notify "Begin #{test_file}"
    start = Time.now
    test_case = TestCase.new(@hosts, @logger, config, options, test_file).run_test
    duration = Time.now - start
    @test_cases << test_case

    state = test_case.test_status == :skip ? 'skipp' : test_case.test_status
    msg = "#{test_file} #{state}ed in %.2f seconds" % duration.to_f
    case test_case.test_status
    when :pass
      @logger.success msg
    when :skip
      @logger.debug msg
    when :fail
      @logger.error msg
      break if fail_mode #all failure modes cause us to kick out early on failure
    when :error
      @logger.warn msg
      break if fail_mode #all failure modes cause use to kick out early on error
    end
  end

  # REVISIT: This changes global state, breaking logging in any future runs
  # of the suite – or, at least, making them highly confusing for anyone who
  # has not studied the implementation in detail. --daniel 2011-03-14
  summarize
  write_junit_xml if options[:xml]

  # Allow chaining operations...
  return self
end

#run_and_raise_on_failureObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/beaker/test_suite.rb', line 89

def run_and_raise_on_failure
  begin
    run
    return self if success?
  rescue => e
    #failed during run
    report_and_raise(@logger, e, "TestSuite :run_and_raise_on_failure")
  else
    #failed during test
    report_and_raise(@logger, RuntimeError.new("Failed while running the #{name} suite"), "TestSuite: report_and_raise_on_failure")
  end
end

#skipped_testsObject



131
132
133
# File 'lib/beaker/test_suite.rb', line 131

def skipped_tests
  @skipped_tests ||= @test_cases.select { |c| c.test_status == :skip }.length
end

#success?Boolean



106
107
108
109
# File 'lib/beaker/test_suite.rb', line 106

def success?
  fail_without_test_run
  sum_failed == 0
end

#test_countObject



115
116
117
# File 'lib/beaker/test_suite.rb', line 115

def test_count
  @test_count ||= @test_cases.length
end