Method: Beaker::TestCase#initialize

Defined in:
lib/beaker/test_case.rb

#initialize(these_hosts, logger, options = {}, path = nil) ⇒ TestCase



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
142
143
144
145
146
147
148
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
# File 'lib/beaker/test_case.rb', line 97

def initialize(these_hosts, logger, options={}, path=nil)
  @hosts   = these_hosts
  @logger = logger
  @sublog = ""
  @options = options
  @path    = path
  @usr_home = options[:home]
  @test_status = :pass
  @exception = nil
  @runtime = nil
  @teardown_procs = []
   = {}
  @exports  = []
  set_current_test_filename(@path ? File.basename(@path, '.rb') : nil)


  #
  # We put this on each wrapper (rather than the class) so that methods
  # defined in the tests don't leak out to other tests.
  class << self
    def run_test
      @logger.start_sublog
      @logger.last_result = nil

      set_current_step_name(nil)

      #add arbitrary role methods
      roles = []
      @hosts.each do |host|
        roles << host[:roles]
      end
      add_role_def( roles.flatten.uniq )

      @runtime = Benchmark.realtime do
        begin
          test = File.read(path)
          eval test,nil,path,1
        rescue FailTest, TEST_EXCEPTION_CLASS => e
          log_and_fail_test(e, :fail)
        rescue PassTest
          @test_status = :pass
        rescue PendingTest
          @test_status = :pending
        rescue SkipTest
          @test_status = :skip
        rescue StandardError, ScriptError, SignalException => e
          log_and_fail_test(e)
        ensure
          @logger.info('Begin teardown')
          @teardown_procs.each do |teardown|
            begin
              teardown.call
            rescue StandardError, SignalException, TEST_EXCEPTION_CLASS => e
              log_and_fail_test(e, :teardown_error)
            end
          end
          @logger.info('End teardown')
        end
      end
      @sublog = @logger.get_sublog
      @last_result = @logger.last_result
      return self
    end

    private

    # Log an error and mark the test as failed, passing through an
    # exception so it can be displayed at the end of the total run.
    #
    # We break out the complete exception backtrace and log each line
    # individually as well.
    #
    # @param exception [Exception] exception to fail with
    # @param exception [Symbol] the test status
    def log_and_fail_test(exception, status=:error)
      logger.error("#{exception.class}: #{exception.message}")
      bt = exception.backtrace
      logger.pretty_backtrace(bt).each_line do |line|
        logger.error(line)
      end
      # If the status is already a test failure or error, don't overwrite with the teardown failure.
      unless status == :teardown_error && (@test_status == :error || @test_status == :fail)
        status = :error if status == :teardown_error
        @test_status = status
        @exception   = exception
      end
    end
  end
end