Class: Datadog::CI::Contrib::Cucumber::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/contrib/cucumber/formatter.rb

Overview

Defines collection of instrumented Cucumber events

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Formatter

Returns a new instance of Formatter.



15
16
17
18
19
20
21
22
23
# File 'lib/datadog/ci/contrib/cucumber/formatter.rb', line 15

def initialize(config)
  @config = config
  @failed_tests_count = 0

  @current_test_suite = nil
  @failed_tests_in_current_test_suite = 0

  bind_events(config)
end

Instance Method Details

#bind_events(config) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/datadog/ci/contrib/cucumber/formatter.rb', line 25

def bind_events(config)
  config.on_event :test_run_started, &method(:on_test_run_started)
  config.on_event :test_run_finished, &method(:on_test_run_finished)
  config.on_event :test_case_started, &method(:on_test_case_started)
  config.on_event :test_case_finished, &method(:on_test_case_finished)
  config.on_event :test_step_started, &method(:on_test_step_started)
  config.on_event :test_step_finished, &method(:on_test_step_finished)
end

#on_test_case_finished(event) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/datadog/ci/contrib/cucumber/formatter.rb', line 71

def on_test_case_finished(event)
  test_span = CI.active_test
  return if test_span.nil?

  # We need to track overall test failures manually if we are using cucumber < 8.0 because
  # TestRunFinished event does not have a success attribute before 8.0.
  #
  # To track whether test suite failed or passed we need to
  # track the number of failed tests in the current test suite.
  if event.result.failed?
    @failed_tests_count += 1
    @failed_tests_in_current_test_suite += 1
  end

  finish_test(test_span, event.result)
end

#on_test_case_started(event) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/datadog/ci/contrib/cucumber/formatter.rb', line 54

def on_test_case_started(event)
  test_suite_name = event.test_case.location.file

  start_test_suite(test_suite_name) unless same_test_suite_as_current?(test_suite_name)

  CI.start_test(
    event.test_case.name,
    test_suite_name,
    tags: {
      CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
      CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::Cucumber::Integration.version.to_s,
      CI::Ext::Test::TAG_TYPE => CI::Ext::Test::TEST_TYPE
    },
    service: configuration[:service_name]
  )
end

#on_test_run_finished(event) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/datadog/ci/contrib/cucumber/formatter.rb', line 46

def on_test_run_finished(event)
  if event.respond_to?(:success)
    finish_session(event.success)
  else
    finish_session(@failed_tests_count.zero?)
  end
end

#on_test_run_started(event) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/datadog/ci/contrib/cucumber/formatter.rb', line 34

def on_test_run_started(event)
  test_session = CI.start_test_session(
    tags: {
      CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
      CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::Cucumber::Integration.version.to_s,
      CI::Ext::Test::TAG_TYPE => CI::Ext::Test::TEST_TYPE
    },
    service: configuration[:service_name]
  )
  CI.start_test_module(test_session.name)
end

#on_test_step_finished(event) ⇒ Object



92
93
94
95
96
97
# File 'lib/datadog/ci/contrib/cucumber/formatter.rb', line 92

def on_test_step_finished(event)
  current_step_span = CI.active_span(Ext::STEP_SPAN_TYPE)
  return if current_step_span.nil?

  finish_test(current_step_span, event.result)
end

#on_test_step_started(event) ⇒ Object



88
89
90
# File 'lib/datadog/ci/contrib/cucumber/formatter.rb', line 88

def on_test_step_started(event)
  CI.trace(Ext::STEP_SPAN_TYPE, event.test_step.to_s)
end