Class: Moto::Test::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/test/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

._pathObject

Returns the value of attribute _path.



18
19
20
# File 'lib/test/base.rb', line 18

def _path
  @_path
end

Instance Attribute Details

#metadataMoto::Test::Metadata

Contains information specified by user in Test headers, marked with appropriate tags



15
16
17
# File 'lib/test/base.rb', line 15

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/test/base.rb', line 8

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/test/base.rb', line 9

def params
  @params
end

#static_pathObject

Returns the value of attribute static_path.



10
11
12
# File 'lib/test/base.rb', line 10

def static_path
  @static_path
end

#statusObject

Returns the value of attribute status.



11
12
13
# File 'lib/test/base.rb', line 11

def status
  @status
end

Class Method Details

.inherited(k) ⇒ Object



21
22
23
# File 'lib/test/base.rb', line 21

def self.inherited(k)
  k._path = caller.first.match(/(.+):\d+:in/)[1]
end

Instance Method Details

#afterObject



96
97
98
# File 'lib/test/base.rb', line 96

def after
  # abstract
end

#assert_equal(value1, value2, failure_message = "Arguments should be equal: $1 != $2") ⇒ Object

Checks for equality of both values using operator ==

Parameters:

  • value1 (Object)
  • value2 (Object)
  • failure_message (String) (defaults to: "Arguments should be equal: $1 != $2")

    Will be logged/displayed when assertion fails. Substrings ‘$1’ and ‘$2’ (without quotes) found in string will be replaced, respectively, with value1.to_s and value2.to_s



129
130
131
132
133
# File 'lib/test/base.rb', line 129

def assert_equal(value1, value2, failure_message = "Arguments should be equal: $1 != $2")
  if value1 != value2
    report_failed_assertion(failure_message.gsub('$1', value1.to_s).gsub('$2', value2.to_s))
  end
end

#assert_false(value, failure_message = "Logical condition not met, expecting false, given $1") ⇒ Object

Checks if passed value is equal to False

Parameters:

  • value (Object)
  • failure_message (String) (defaults to: "Logical condition not met, expecting false, given $1")

    Will be logged/displayed when assertion fails. Substring ‘$1’ (without quotes) found in string will be replaced with value.to_s



152
153
154
155
156
# File 'lib/test/base.rb', line 152

def assert_false(value, failure_message = "Logical condition not met, expecting false, given $1")
  if value
    report_failed_assertion(failure_message.gsub('$1', value.to_s))
  end
end

#assert_true(value, failure_message = "Logical condition not met, expecting true, given $1") ⇒ Object Also known as: assert

Checks if passed value is equal to True

Parameters:

  • value (Object)
  • failure_message (String) (defaults to: "Logical condition not met, expecting true, given $1")

    Will be logged/displayed when assertion fails. Substring ‘$1’ (without quotes) found in string will be replaced with value.to_s



140
141
142
143
144
# File 'lib/test/base.rb', line 140

def assert_true(value, failure_message = "Logical condition not met, expecting true, given $1")
  if !value
    report_failed_assertion(failure_message.gsub('$1', value.to_s))
  end
end

#beforeObject



92
93
94
# File 'lib/test/base.rb', line 92

def before
  # abstract
end

#envHash

Returns Configuration for selected environment + current thread combination.

Returns:

  • (Hash)

    Configuration for selected environment + current thread combination



165
166
167
# File 'lib/test/base.rb', line 165

def env
  Moto::Config::Manager.config_environment
end

#fail(msg = nil) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/test/base.rb', line 104

def fail(msg = nil)
  if msg.nil?
    msg = 'Test forcibly failed with no reason given.'
  else
    msg = "Forced failure: #{msg}"
  end
  raise Exceptions::TestForcedFailure.new msg
end

#init(params_path) ⇒ Object

Initializes test to be executed with specified params and environment



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/test/base.rb', line 30

def init(params_path)
  @params = []
  @params_path = params_path

  @name = self.class.to_s.demodulize
  @name += "_#{@params_path.split('/')[-1].chomp('.param')}" if @params_path

  @status = Moto::Test::Status.new
  @status.name = @name
  @status.test_class_name = self.class.name
  @status.display_name = @status.test_class_name.split('::')[2..-2].join(' > ')
  @status.display_name += "_#{@params_path.split('/')[-1].chomp('.param')}" if @params_path
  @status.display_name += "(#{.test_repeat})" if .test_repeat > 1
end

#log_pathString

Returns string with the path to the test’s log.

Returns:

  • (String)

    string with the path to the test’s log



55
56
57
# File 'lib/test/base.rb', line 55

def log_path
  @log_path
end

#log_path=(param) ⇒ Object

Setter for :log_path



46
47
48
49
50
51
52
# File 'lib/test/base.rb', line 46

def log_path=(param)
  @log_path = param

  # I hate myself for doing this, but I have no other idea for now how to pass log to Listeners that
  # make use of it (for example WebUI)
  @status.log_path = param
end

#loggerLogger

Returns:

  • (Logger)


170
171
172
# File 'lib/test/base.rb', line 170

def logger
  Thread.current['logger']
end

#pass(msg = nil) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/test/base.rb', line 113

def pass(msg = nil)
  if msg.nil?
    msg = 'Test forcibly passed with no reason given.'
  else
    msg = "Forced passed: #{msg}"
  end
  raise Exceptions::TestForcedPassed.new msg
end

#pathObject



25
26
27
# File 'lib/test/base.rb', line 25

def path
  self.class._path
end

#report_failed_assertion(failure_message) ⇒ Object



158
159
160
161
162
# File 'lib/test/base.rb', line 158

def report_failed_assertion(failure_message)
  line_number = caller.select { |l| l.match(/#{static_path}:\d*:in `run'/) }.first[/\d+/].to_i
  status.log_failure("ASSERTION FAILED in line #{line_number}: #{failure_message}")
  logger.error(failure_message)
end

#runObject

Only to be overwritten by final test execution Use :run_test in order to run test



88
89
90
# File 'lib/test/base.rb', line 88

def run
  # abstract
end

#run_testObject

Use this to run test Initializes status, runs test, handles exceptions, finalizes status after run completion



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/test/base.rb', line 61

def run_test
  status.initialize_run

  #TODO Formatting/optimization
  begin
    @params = eval(File.read(@params_path)) if File.exists?(@params_path.to_s)
    @status.params = @params
  rescue Exception => exception
    status.log_exception(exception)
    raise "ERROR: Invalid parameters file: #{@params_path}.\n\tMESSAGE: #{exception.message}"
  ensure
    status.finalize_run
  end

  begin
    run
  rescue Exception => exception
    status.log_exception(exception)
    raise
  ensure
    status.finalize_run
  end

end

#skip(msg = nil) ⇒ Object



100
101
102
# File 'lib/test/base.rb', line 100

def skip(msg = nil)
  raise Exceptions::TestSkipped.new(msg.nil? ? 'Test skipped with no reason given.' : "Skipped: #{msg}")
end