class AllureRspec::RSpecFormatter

Main rspec formatter class translating rspec events to allure lifecycle

Constants

ALLURE_STATUS

@return [Hash] allure statuses mapping

Attributes

allure_config[R]
lifecycle[R]

Public Class Methods

new(output) click to toggle source
Calls superclass method
# File lib/allure_rspec/formatter.rb, line 37
def initialize(output)
  super

  @allure_config = AllureRspec.configuration
  Allure.lifecycle = @lifecycle = Allure::AllureLifecycle.new(@allure_config)
end

Public Instance Methods

example_finished(example_notification) click to toggle source

Finishes example @param [RSpec::Core::Notifications::ExampleNotification] example_notification @return [void]

# File lib/allure_rspec/formatter.rb, line 83
def example_finished(example_notification)
  lifecycle.update_test_case(&update_test_proc(example_notification.example.execution_result))
  lifecycle.stop_test_case
end
example_group_finished(_example_group_notification) click to toggle source

Starts example group @param [RSpec::Core::Notifications::GroupNotification] _example_group_notification @return [void]

# File lib/allure_rspec/formatter.rb, line 91
def example_group_finished(_example_group_notification)
  lifecycle.stop_test_container
end
example_group_started(example_group_notification) click to toggle source

Starts example group @param [RSpec::Core::Notifications::GroupNotification] example_group_notification @return [void]

# File lib/allure_rspec/formatter.rb, line 66
def example_group_started(example_group_notification)
  description = example_group_notification.group.description.then do |desc|
    desc.empty? ? "Anonymous" : desc
  end
  lifecycle.start_test_container(Allure::TestResultContainer.new(name: description))
end
example_started(example_notification) click to toggle source

Starts example @param [RSpec::Core::Notifications::ExampleNotification] example_notification @return [void]

# File lib/allure_rspec/formatter.rb, line 76
def example_started(example_notification)
  lifecycle.start_test_case(test_result(example_notification.example))
end
start(_start_notification) click to toggle source

Start test run @param [RSpec::Core::Notifications::StartNotification] _start_notification @return [void]

# File lib/allure_rspec/formatter.rb, line 47
def start(_start_notification)
  lifecycle.clean_results_dir
  lifecycle.write_categories

  RSpec::Core::Example.class_eval do
    include Allure
  end
end
stop(_stop_notification) click to toggle source

Start test run @param [RSpec::Core::Notifications::StopNotification] _stop_notification @return [void]

# File lib/allure_rspec/formatter.rb, line 59
def stop(_stop_notification)
  lifecycle.write_environment
end

Private Instance Methods

status(result) click to toggle source

Get allure status from result @param [RSpec::Core::Example::ExecutionResult] result @return [Symbol]

# File lib/allure_rspec/formatter.rb, line 135
def status(result)
  return Allure::ResultUtils.status(result.exception) if result.status == :failed

  ALLURE_STATUS[result.status]
end
test_result(example) click to toggle source

Transform example to <Allure::TestResult> @param [RSpec::Core::Example] example @return [Allure::TestResult]

# File lib/allure_rspec/formatter.rb, line 102
def test_result(example)
  parser = RspecMetadataParser.new(example, allure_config)

  Allure::TestResult.new(
    name: example.description,
    description: "Location - #{strip_relative(parser.location)}",
    description_html: "Location - #{strip_relative(parser.location)}",
    history_id: example.id,
    full_name: example.full_description,
    labels: parser.labels,
    links: parser.links,
    status_details: parser.status_details,
    environment: allure_config.environment
  )
end
update_test_proc(result) click to toggle source

Update test status on finish @param [RSpec::Core::Example::ExecutionResult] result @return [Proc]

# File lib/allure_rspec/formatter.rb, line 121
def update_test_proc(result)
  Allure::ResultUtils.status_details(result.exception).then do |status_detail|
    proc do |test_case|
      test_case.stage = Allure::Stage::FINISHED
      test_case.status = status(result)
      test_case.status_details.message = status_detail.message
      test_case.status_details.trace = status_detail.trace&.gsub(/\e\[(\d+)(?:;\d+)*m/, "")
    end
  end
end