class Beaker::TestCase

using the DSL.
See {Beaker::DSL} for more information about writing tests
the test case is running within.
the test case access to various details of the environment and suite
the status of the TestCase is saved. Instance readers/accessors provide
through {Beaker::DSL::Structure#teardown}) and once completed
They are executed in order (save for any teardown procs registered
contained all in one file though may have multiple dependent examples.
This class represents a single test case. A test case is necessarily

def initialize(these_hosts, logger, options = {}, path = nil)

Parameters:
  • path (String) -- The local path to a test file to be executed.
  • options (Hash{Symbol=>String}) -- Parsed command line options.
  • logger (Logger) -- A logger that implements
  • these_hosts (Hosts, Array) -- The hosts to execute this test
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 = []
  @metadata = {}
  @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.
      return if status == :teardown_error && (@test_status == :error || @test_status == :fail)
      status = :error if status == :teardown_error
      @test_status = status
      @exception   = exception
    end
  end
end

def log_and_fail_test(exception, status = :error)

Parameters:
  • exception (Symbol) -- the test status
  • exception (Exception) -- exception to fail with
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.
  return if status == :teardown_error && (@test_status == :error || @test_status == :fail)
  status = :error if status == :teardown_error
  @test_status = status
  @exception   = exception
end

def run_test

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

def to_hash

Returns:
  • (Hash) - A Hash representation of this test.

Other tags:
    Note: - The visibility and semantics of this method are valid, but the

Other tags:
    Api: - public
def to_hash
  hash = {}
  hash['HOSTS'] = {}
  @hosts.each do |host|
    hash['HOSTS'][host.name] = host.overrides
  end
  hash
end