class Allure::AllureLifecycle
Main class for creating and writing allure results
Attributes
Public Class Methods
Allure
lifecycle instance
@param [Allure::Config] configuration
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 14 def initialize(config = Config.instance) @test_context = [] @step_context = [] @config = config @logger = config.logger @file_writer = FileWriter.new(config.results_directory) end
Public Instance Methods
Add attachment to current test or step @param [String] name Attachment
name @param [File, String] source File or string to save as attachment @param [String] type attachment type defined in {Allure::ContentType} or any other valid mime type @param [Boolean] test_case add attachment to current test case @return [void]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 212 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 step to current fixture|step|test case @param [Allure::StepResult] step_result @return [Allure::StepResult]
# File 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 directory @return [void]
# File 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 @param [Allure::FixtureResult] fixture_result @return [Allure::FixtureResult]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 169 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 @param [Allure::FixtureResult] fixture_result @return [Allure::FixtureResult]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 151 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 @param [Allure::FixtureResult] fixture_result @return [Allure::FixtureResult]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 160 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 and add to current test container @param [Allure::TestResult] test_result @return [Allure::TestResult]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 71 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 result container @param [Allure::TestResultContainer] test_result_container @return [Allure::TestResultContainer]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 29 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 and add to current test case @param [Allure::StepResult] step_result @return [Allure::StepResult]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 114 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 current test fixture @return [void]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 196 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 current test case and write result @return [void]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 100 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 current test container and write result @return [void]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 55 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 current test step @return [void]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 139 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
@example Update current fixture
update_test_container do |fixture| fixture.status = Allure::Status::BROKEN end
@yieldparam [Allure::FixtureResult] current fixture @yieldreturn [void] @return [void]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 188 def update_fixture return logger.error { "Could not update fixture, fixture is not started" } unless @current_fixture yield(@current_fixture) end
@example Update current test case
update_test_container do |test_case| test_case.status = Allure::Status::FAILED end
@yieldparam [Allure::TestResult] current test @yieldreturn [void] @return [void]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 92 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
@example Update current test container
update_test_container do |container| container.stage = Allure::Stage::FINISHED end
@yieldparam [Allure::TestResultContainer] current test result container @yieldreturn [void] @return [void]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 45 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
@example Update current test step
update_test_container do |test_step| test_step.status = Allure::Status::BROKEN end
@yieldparam [Allure::StepResult] current test step @yieldreturn [void] @return [void]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 131 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
Add categories.json
@param [File, Array<Category>] categories @return [void]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 238 def write_categories(categories = config.categories) return unless categories file_writer.write_categories(categories) end
Add environment.properties file
@param [Hash] env @return [void]
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 228 def write_environment(env = config.environment_properties) return unless env file_writer.write_environment(env) end
Private Instance Methods
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 291 def clear_current_fixture @current_fixture = nil end
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 287 def clear_current_test_case @current_test_case = nil end
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 271 def clear_last_test_container @test_context.pop end
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 279 def clear_last_test_step @step_context.pop end
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 283 def clear_step_context @step_context.clear end
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 263 def current_executable current_test_step || @current_fixture || @current_test_case end
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 267 def current_test_result_container @test_context.last end
# File lib/allure_ruby_commons/allure_lifecycle.rb, line 275 def current_test_step @step_context.last end