Method: Beaker::TestCase#initialize

Defined in:
lib/beaker/test_case.rb

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

Returns a new instance of TestCase.

Parameters:

  • these_hosts (Hosts, Array<Host>)

    The hosts to execute this test against/on.

  • logger (Logger)

    A logger that implements Logger‘s interface.

  • options (Hash{Symbol=>String}) (defaults to: {})

    Parsed command line options.

  • path (String) (defaults to: nil)

    The local path to a test file to be executed.



93
94
95
96
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
# File 'lib/beaker/test_case.rb', line 93

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 = []
   = {}
  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
          @test_status = :fail
          @exception   = e
        rescue PendingTest
          @test_status = :pending
        rescue SkipTest
          @test_status = :skip
        rescue StandardError, ScriptError, SignalException => e
          log_and_fail_test(e)
        ensure
          @teardown_procs.each do |teardown|
            begin
              teardown.call
            rescue StandardError, SignalException, TEST_EXCEPTION_CLASS => e
              log_and_fail_test(e)
            end
          end
        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
    def log_and_fail_test(exception)
      logger.error("#{exception.class}: #{exception.message}")
      bt = exception.backtrace
      logger.pretty_backtrace(bt).each_line do |line|
        logger.error(line)
      end
      @test_status = :error
      @exception   = exception
    end
  end
end