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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Config.instance) ⇒ AllureLifecycle

Allure lifecycle instance

Parameters:



14
15
16
17
18
19
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 14

def initialize(config = Config.instance)
  @test_context = []
  @step_context = []
  @config = config
  @logger = config.logger
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
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



211
212
213
214
215
216
217
218
219
220
221
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 211

def add_attachment(name:, source:, type:, test_case: false)
  attachment = ResultUtils.prepare_attachment(name, type)
  return logger.error { "Can't add attachment, unrecognized mime type: #{type}" } unless attachment

  executable_item = test_case ? @current_test_case : current_executable
  return logger.error { "Can't add attachment, no test, step or fixture is running" } unless executable_item

  executable_item.attachments.push(attachment)
  logger.debug { "Adding attachment '#{name}' to '#{executable_item.name}'" }
  write_attachment(source, attachment)
end

#add_test_step(step_result) ⇒ Allure::StepResult

Add step to current fixture|step|test case

Parameters:

Returns:



247
248
249
250
251
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 247

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



255
256
257
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 255

def clean_results_dir
  FileUtils.rm_f(Dir.glob("#{config.results_directory}/**/*")) if config.clean_results_directory
end

#start_fixture(fixture_result) ⇒ Allure::FixtureResult

Start fixture

Parameters:

Returns:



168
169
170
171
172
173
174
175
176
177
178
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 168

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

  logger.debug { "Starting fixture: #{fixture_result.name}" }
  fixture_result.start = ResultUtils.timestamp
  fixture_result.stage = Stage::RUNNING
end

#start_prepare_fixture(fixture_result) ⇒ Allure::FixtureResult

Start prepare fixture

Parameters:

Returns:



150
151
152
153
154
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 150

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:



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

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:



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 70

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

  logger.debug("Starting test case: #{test_result.name}")
  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:



28
29
30
31
32
33
34
35
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 28

def start_test_container(test_result_container)
  test_result_container.tap do |container|
    logger.debug { "Starting test container: #{container.name}" }

    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

Parameters:

Returns:



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

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

  logger.debug { "Starting test step: #{step_result.name}" }
  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



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

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

  logger.debug { "Stopping fixture: #{@current_fixture.name}" }
  @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



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

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

  logger.debug { "Stopping test case: #{@current_test_case.name}" }
  @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



54
55
56
57
58
59
60
61
62
63
64
65
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 54

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|
    logger.debug { "Stopping container: #{container.name}" }
    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



138
139
140
141
142
143
144
145
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 138

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

  logger.debug { "Stopping test step: #{current_test_step.name}" }
  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)


187
188
189
190
191
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 187

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)


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

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)


44
45
46
47
48
49
50
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 44

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)


130
131
132
133
134
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 130

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_categories(categories = config.categories) ⇒ void

This method returns an undefined value.

Add categories.json

Parameters:

  • categories (File, Array<Category>) (defaults to: config.categories)


238
239
240
241
242
# File 'allure-ruby-commons/lib/allure_ruby_commons/allure_lifecycle.rb', line 238

def write_categories(categories = config.categories)
  return unless categories

  file_writer.write_categories(categories)
end

#write_environment(env = config.environment_properties) ⇒ void

This method returns an undefined value.

Add environment.properties file

Parameters:

  • env (Hash, Proc) (defaults to: config.environment_properties)


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

def write_environment(env = config.environment_properties)
  return unless env

  env_properties = env.respond_to?(:call) ? env.call : env
  file_writer.write_environment(env_properties)
end