Class: Allure::AllureLifecycle

Inherits:
Object
  • Object
show all
Defined in:
lib/allure_ruby_commons/allure_lifecycle.rb

Overview

Main class for creating and writing allure results

Instance Method Summary collapse

Constructor Details

#initializeAllureLifecycle

Returns a new instance of AllureLifecycle.



6
7
8
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 6

def initialize
  @step_context = []
end

Instance Method Details

#add_attachment(name:, source:, type:, test_case: false) ⇒ void

This method returns an undefined value.

Add attachment to current test or step

Parameters:

  • name (String)

    Attachment name

  • source (File, String)

    File or string to save as attachment

  • type (String)

    attachment type defined in ContentType or any other valid mime type

  • test_case (Boolean) (defaults to: false)

    add attachment to current test case



183
184
185
186
187
188
189
190
191
192
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 183

def add_attachment(name:, source:, type:, test_case: false)
  attachment = prepare_attachment(name, type) || begin
    return logger.error("Can't add attachment, unrecognized mime type: #{type}")
  end
  executable_item = (test_case && @current_test_case) || current_executable
  executable_item&.attachments&.push(attachment) || begin
    return logger.error("Can't add attachment, no test, step or fixture is running")
  end
  write_attachment(source, attachment)
end

#add_test_step(step_result) ⇒ Allure::StepResult

Add step to current fixture|step|test case

Parameters:

Returns:



215
216
217
218
219
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 215

def add_test_step(step_result)
  current_executable.steps.push(step_result)
  @step_context.push(step_result)
  step_result
end

#prepare_attachment(name, type) ⇒ Allure::Attachment

Create attachment object

Parameters:

  • name (String)
  • type (String)

Returns:



198
199
200
201
202
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 198

def prepare_attachment(name, type)
  extension = ContentType.to_extension(type) || return
  file_name = "#{UUID.generate}-attachment.#{extension}"
  Attachment.new(name: name, source: file_name, type: type)
end

#start_fixture(fixture_result) ⇒ Allure::FixtureResult

Start fixture

Parameters:

Returns:



142
143
144
145
146
147
148
149
150
151
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 142

def start_fixture(fixture_result)
  clear_step_context
  unless @current_test_result_container
    logger.error("Could not start fixture, test container is not started")
    return false
  end

  fixture_result.start = ResultUtils.timestamp
  fixture_result.stage = Stage::RUNNING
end

#start_prepare_fixture(fixture_result) ⇒ Allure::FixtureResult

Start prepare fixture

Parameters:

Returns:



124
125
126
127
128
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 124

def start_prepare_fixture(fixture_result)
  start_fixture(fixture_result) || return
  @current_test_result_container.befores.push(fixture_result)
  @current_fixture = fixture_result
end

#start_tear_down_fixture(fixture_result) ⇒ Allure::FixtureResult

Start tear down fixture

Parameters:

Returns:



133
134
135
136
137
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 133

def start_tear_down_fixture(fixture_result)
  start_fixture(fixture_result) || return
  @current_test_result_container.afters.push(fixture_result)
  @current_fixture = fixture_result
end

#start_test_case(test_result) ⇒ Allure::TestResult

Start test case and add to current test container

Parameters:

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 48

def start_test_case(test_result)
  clear_step_context
  unless @current_test_result_container
    return logger.error("Could not start test case, test container is not started")
  end

  test_result.start = ResultUtils.timestamp
  test_result.stage = Stage::RUNNING
  test_result.labels.push(ResultUtils.thread_label, ResultUtils.host_label, ResultUtils.language_label)
  @current_test_result_container.children.push(test_result.uuid)
  @current_test_case = test_result
end

#start_test_container(test_result_container) ⇒ Allure::TestResultContainer

Start test result container

Parameters:

Returns:



13
14
15
16
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 13

def start_test_container(test_result_container)
  test_result_container.start = ResultUtils.timestamp
  @current_test_result_container = test_result_container
end

#start_test_step(step_result) ⇒ Allure::StepResult

Start test step and add to current test case

Parameters:

Returns:



89
90
91
92
93
94
95
96
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 89

def start_test_step(step_result)
  return logger.error("Could not start test step, no test case is running") unless @current_test_case

  step_result.start = ResultUtils.timestamp
  step_result.stage = Stage::RUNNING
  add_test_step(step_result)
  step_result
end

#stop_fixturevoid

This method returns an undefined value.

Stop current test fixture



168
169
170
171
172
173
174
175
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 168

def stop_fixture
  return logger.error("Could not stop fixture, fixture is not started") unless @current_fixture

  @current_fixture.stop = ResultUtils.timestamp
  @current_fixture.stage = Stage::FINISHED
  clear_current_fixture
  clear_step_context
end

#stop_test_casevoid

This method returns an undefined value.

Stop current test case and write result



76
77
78
79
80
81
82
83
84
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 76

def stop_test_case
  return logger.error("Could not stop test case, no test case is running") unless @current_test_case

  @current_test_case.stop = ResultUtils.timestamp
  @current_test_case.stage = Stage::FINISHED
  file_writer.write_test_result(@current_test_case)
  clear_current_test_case
  clear_step_context
end

#stop_test_containervoid

This method returns an undefined value.

Stop current test container and write result



35
36
37
38
39
40
41
42
43
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 35

def stop_test_container
  unless @current_test_result_container
    return logger.error("Could not stop test container, no container is running.")
  end

  @current_test_result_container.stop = ResultUtils.timestamp
  file_writer.write_test_result_container(@current_test_result_container)
  clear_current_test_container
end

#stop_test_stepvoid

This method returns an undefined value.

Stop current test step



113
114
115
116
117
118
119
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 113

def stop_test_step
  return logger.error("Could not stop test step, no step is running") unless current_test_step

  current_test_step.stop = ResultUtils.timestamp
  current_test_step.stage = Stage::FINISHED
  clear_last_test_step
end

#update_fixture {|current| ... } ⇒ void

This method returns an undefined value.

Examples:

Update current fixture

update_test_container do |fixture|
  fixture.status = Allure::Status::BROKEN
end

Yield Parameters:

Yield Returns:

  • (void)


160
161
162
163
164
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 160

def update_fixture
  return logger.error("Could not update fixture, fixture is not started") unless @current_fixture

  yield(@current_fixture)
end

#update_test_case {|current| ... } ⇒ void

This method returns an undefined value.

Examples:

Update current test case

update_test_container do |test_case|
  test_case.status = Allure::Status::FAILED
end

Yield Parameters:

Yield Returns:

  • (void)


68
69
70
71
72
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 68

def update_test_case
  return logger.error("Could not update test case, no test case running") unless @current_test_case

  yield(@current_test_case)
end

#update_test_container {|current| ... } ⇒ void

This method returns an undefined value.

Examples:

Update current test container

update_test_container do |container|
  container.stage = Allure::Stage::FINISHED
end

Yield Parameters:

Yield Returns:

  • (void)


25
26
27
28
29
30
31
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 25

def update_test_container
  unless @current_test_result_container
    return logger.error("Could not update test container, no container is running.")
  end

  yield(@current_test_result_container)
end

#update_test_step {|current| ... } ⇒ void

This method returns an undefined value.

Examples:

Update current test step

update_test_container do |test_step|
  test_step.status = Allure::Status::BROKEN
end

Yield Parameters:

Yield Returns:

  • (void)


105
106
107
108
109
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 105

def update_test_step
  return logger.error("Could not update test step, no step is running") unless current_test_step

  yield(current_test_step)
end

#write_attachment(source, attachment) ⇒ void

This method returns an undefined value.

Write attachment file

Parameters:



208
209
210
# File 'lib/allure_ruby_commons/allure_lifecycle.rb', line 208

def write_attachment(source, attachment)
  file_writer.write_attachment(source, attachment)
end