Class: Allure::AllureLifecycle

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb

Overview

Main class for creating and writing allure results

Instance Method Summary collapse

Constructor Details

#initializeAllureLifecycle


11
12
13
14
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 11

def initialize
  @test_context = []
  @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


195
196
197
198
199
200
201
202
203
204
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 195

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


219
220
221
222
223
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 219

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

#clean_results_dirvoid

This method returns an undefined value.

Clean results directory


227
228
229
230
231
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 227

def clean_results_dir
  Allure.configuration.tap do |c|
    FileUtils.rm_f(Dir.glob("#{c.results_directory}/*")) if c.clean_results_directory
  end
end

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

Create attachment object


210
211
212
213
214
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 210

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


154
155
156
157
158
159
160
161
162
163
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 154

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


136
137
138
139
140
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 136

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


145
146
147
148
149
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 145

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


60
61
62
63
64
65
66
67
68
69
70
71
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 60

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


21
22
23
24
25
26
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 21

def start_test_container(test_result_container)
  test_result_container.tap do |container|
    container.start = ResultUtils.timestamp
    @test_context.push(container)
  end
end

#start_test_step(step_result) ⇒ Allure::StepResult

Start test step and add to current test case


101
102
103
104
105
106
107
108
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 101

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


180
181
182
183
184
185
186
187
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 180

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


88
89
90
91
92
93
94
95
96
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 88

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


45
46
47
48
49
50
51
52
53
54
55
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 45

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.tap do |container|
    container.stop = ResultUtils.timestamp
    file_writer.write_test_result_container(container)
    clear_last_test_container
  end
end

#stop_test_stepvoid

This method returns an undefined value.

Stop current test step


125
126
127
128
129
130
131
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 125

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)

172
173
174
175
176
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 172

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)

80
81
82
83
84
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 80

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)

35
36
37
38
39
40
41
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 35

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)

117
118
119
120
121
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 117

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