Class: Datadog::CI::Test

Inherits:
Span
  • Object
show all
Defined in:
lib/datadog/ci/test.rb

Overview

Represents a single part of a test run.

Instance Attribute Summary collapse

Attributes inherited from Span

#tracer_span

Instance Method Summary collapse

Methods inherited from Span

#base_commit_sha, #clear_tag, #failed?, #get_metric, #get_tag, #git_branch, #git_commit_message, #git_commit_sha, #git_repository_url, #git_tag, #id, #initialize, #original_git_commit_message, #original_git_commit_sha, #os_architecture, #os_platform, #os_version, #passed?, #runtime_name, #runtime_version, #service, #set_default_tags, #set_environment_runtime_tags, #set_metric, #set_tag, #set_tags, #skipped?, #source_file, #status, #to_s, #trace_id, #type, #undefined?

Constructor Details

This class inherits a constructor from Datadog::CI::Span

Instance Attribute Details

#context_idsArray<String>

Context IDs for this test (used for TIA context coverage merging). Contains list of context identifiers from outermost to innermost.

Returns:

  • (Array<String>)

    list of context IDs



18
19
20
# File 'lib/datadog/ci/test.rb', line 18

def context_ids
  @context_ids
end

Instance Method Details

#all_executions_failed?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/datadog/ci/test.rb', line 213

def all_executions_failed?
  !!test_suite&.all_executions_failed?(datadog_test_id)
end

#all_executions_passed?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/datadog/ci/test.rb', line 218

def all_executions_passed?
  !!test_suite&.all_executions_passed?(datadog_test_id)
end

#any_retry_passed?Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/datadog/ci/test.rb', line 208

def any_retry_passed?
  !!test_suite&.any_test_retry_passed?(datadog_test_id)
end

#attempt_to_fix?Boolean

Returns "true" if this flaky test has fixing attempts (determined by Datadog backend).

Returns:

  • (Boolean)

    true if this test is attempted to be fixed.



128
129
130
# File 'lib/datadog/ci/test.rb', line 128

def attempt_to_fix?
  get_tag(Ext::Test::TAG_IS_ATTEMPT_TO_FIX) == "true"
end

#datadog_skip_reasonObject



223
224
225
226
227
228
229
# File 'lib/datadog/ci/test.rb', line 223

def datadog_skip_reason
  if skipped_by_test_impact_analysis?
    Ext::Test::SkipReason::TEST_IMPACT_ANALYSIS
  elsif disabled? || quarantined?
    Ext::Test::SkipReason::TEST_MANAGEMENT_DISABLED
  end
end

#datadog_test_idString

Returns the test id according to Datadog's test impact analysis.

Returns:

  • (String)

    the test id according to Datadog's test impact analysis.



26
27
28
# File 'lib/datadog/ci/test.rb', line 26

def datadog_test_id
  @datadog_test_id ||= Utils::TestRun.datadog_test_id(name, test_suite_name, parameters)
end

#disabled?Boolean

Returns "true" if this test is disabled by Datadog test management.

Returns:

  • (Boolean)

    true if this test is disabled, false otherwise.



122
123
124
# File 'lib/datadog/ci/test.rb', line 122

def disabled?
  get_tag(Ext::Test::TAG_IS_TEST_DISABLED) == "true"
end

#end_lineInteger?

Returns the ending line number of the test in the source file.

Returns:

  • (Integer)

    the ending line number

  • (nil)

    if the ending line is not available



91
92
93
94
# File 'lib/datadog/ci/test.rb', line 91

def end_line
  line = get_tag(Ext::Test::TAG_SOURCE_END)
  line&.to_i
end

#failed!(exception: nil) ⇒ void

This method returns an undefined value.

Sets the status of the span to "fail".

Parameters:

  • exception (Exception) (defaults to: nil)

    the exception that caused the test to fail.



171
172
173
174
175
# File 'lib/datadog/ci/test.rb', line 171

def failed!(exception: nil)
  super

  record_test_result(Ext::Test::Status::FAIL)
end

#finishvoid

This method returns an undefined value.

Finishes the current test.



32
33
34
35
36
37
38
39
40
# File 'lib/datadog/ci/test.rb', line 32

def finish
  if is_retry? && retry_reason.nil?
    set_tag(Ext::Test::TAG_RETRY_REASON, Ext::Test::RetryReason::RETRY_EXTERNAL)
  end

  test_tracing.deactivate_test

  super
end

#is_new?Boolean

Returns "true" if this span represents a test that wasn't known to Datadog before.

Returns:

  • (Boolean)

    true if this test is a new one, false otherwise.



110
111
112
# File 'lib/datadog/ci/test.rb', line 110

def is_new?
  get_tag(Ext::Test::TAG_IS_NEW) == "true"
end

#is_retry?Boolean

Returns "true" if test span represents a retry.

Returns:

  • (Boolean)

    true if this test is a retry, false otherwise.



98
99
100
# File 'lib/datadog/ci/test.rb', line 98

def is_retry?
  get_tag(Ext::Test::TAG_IS_RETRY) == "true"
end

#itr_unskippable!void

This method returns an undefined value.

Marks this test as unskippable by the Test Impact Analysis. This must be done before the test execution starts.

Examples of tests that should be unskippable:

  • tests that read files from disk
  • tests that make network requests
  • tests that call external processes
  • tests that use forking


148
149
150
151
152
153
154
155
156
157
158
# File 'lib/datadog/ci/test.rb', line 148

def itr_unskippable!
  TestImpactAnalysis::Telemetry.itr_unskippable
  set_tag(Ext::Test::TAG_ITR_UNSKIPPABLE, "true")

  if skipped_by_test_impact_analysis?
    clear_tag(Ext::Test::TAG_ITR_SKIPPED_BY_ITR)

    TestImpactAnalysis::Telemetry.itr_forced_run
    set_tag(Ext::Test::TAG_ITR_FORCED_RUN, "true")
  end
end

#modified?Boolean

Returns "true" if this test is marked as modified (e.g., impacted by code changes).

Returns:

  • (Boolean)

    true if this test is modified, false otherwise.



134
135
136
# File 'lib/datadog/ci/test.rb', line 134

def modified?
  get_tag(Ext::Test::TAG_TEST_IS_MODIFIED) == "true"
end

#nameString

Returns the name of the test.

Returns:

  • (String)

    the name of the test.



21
22
23
# File 'lib/datadog/ci/test.rb', line 21

def name
  get_tag(Ext::Test::TAG_NAME)
end

#parametersString?

Gets the parameters for this test (e.g. Cucumber example or RSpec specs) as a serialized JSON.

Returns:

  • (String)

    the serialized JSON of the parameters

  • (nil)

    if this test does not have parameters



203
204
205
# File 'lib/datadog/ci/test.rb', line 203

def parameters
  get_tag(Ext::Test::TAG_PARAMETERS)
end

#passed!void

This method returns an undefined value.

Sets the status of the span to "pass".



162
163
164
165
166
# File 'lib/datadog/ci/test.rb', line 162

def passed!
  super

  record_test_result(Ext::Test::Status::PASS)
end

#peek_durationObject



260
261
262
263
264
265
266
# File 'lib/datadog/ci/test.rb', line 260

def peek_duration
  end_time = Core::Utils::Time.now.utc
  start_time = tracer_span.start_time

  return 0.0 if start_time.nil? || end_time.nil?
  end_time - start_time
end

#quarantined?Boolean

Returns "true" if this test is quarantined by Datadog test management.

Returns:

  • (Boolean)

    true if this test is quarantined, false otherwise.



116
117
118
# File 'lib/datadog/ci/test.rb', line 116

def quarantined?
  get_tag(Ext::Test::TAG_IS_QUARANTINED) == "true"
end

#record_final_statusObject



250
251
252
253
254
255
256
257
# File 'lib/datadog/ci/test.rb', line 250

def record_final_status
  status = get_tag(Ext::Test::TAG_STATUS)
  return if status.nil?

  final_status = compute_final_status(status)
  set_tag(Ext::Test::TAG_FINAL_STATUS, final_status)
  test_suite&.record_test_final_status(datadog_test_id, final_status)
end

#retry_reasonString

Returns string with a reason why test was retried

Returns:

  • (String)

    retry reason



104
105
106
# File 'lib/datadog/ci/test.rb', line 104

def retry_reason
  get_tag(Ext::Test::TAG_RETRY_REASON)
end

#set_parameters(arguments, metadata = {}) ⇒ void

This method returns an undefined value.

Sets the parameters for this test (e.g. Cucumber example or RSpec specs). Parameters are needed to compute test fingerprint to distinguish between different tests having same names.

Parameters:

  • arguments (Hash)

    the arguments that test accepts as key-value hash

  • metadata (Hash) (defaults to: {})

    optional metadata



193
194
195
196
197
# File 'lib/datadog/ci/test.rb', line 193

def set_parameters(arguments,  = {})
  return if arguments.nil?

  set_tag(Ext::Test::TAG_PARAMETERS, Utils::TestRun.test_parameters(arguments: arguments, metadata: ))
end

#should_ignore_failures?Boolean

Returns:

  • (Boolean)


237
238
239
240
241
242
# File 'lib/datadog/ci/test.rb', line 237

def should_ignore_failures?
  return true if quarantined? || disabled?
  return false if attempt_to_fix?

  any_retry_passed?
end

#should_skip?Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/datadog/ci/test.rb', line 232

def should_skip?
  skipped_by_test_impact_analysis? || (disabled? && !attempt_to_fix?)
end

#skipped!(exception: nil, reason: nil) ⇒ void

This method returns an undefined value.

Sets the status of the span to "skip".

Parameters:

  • exception (Exception) (defaults to: nil)

    the exception that caused the test to fail.

  • reason (String) (defaults to: nil)

    the reason why the test was skipped.



181
182
183
184
185
# File 'lib/datadog/ci/test.rb', line 181

def skipped!(exception: nil, reason: nil)
  super

  record_test_result(Ext::Test::Status::SKIP)
end

#skipped_by_test_impact_analysis?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'lib/datadog/ci/test.rb', line 245

def skipped_by_test_impact_analysis?
  get_tag(Ext::Test::TAG_ITR_SKIPPED_BY_ITR) == "true"
end

#start_lineInteger?

Returns the starting line number of the test in the source file.

Returns:

  • (Integer)

    the starting line number

  • (nil)

    if the starting line is not available



83
84
85
86
# File 'lib/datadog/ci/test.rb', line 83

def start_line
  line = get_tag(Ext::Test::TAG_SOURCE_START)
  line&.to_i
end

#test_module_idString

Span id of the running test module this test belongs to.

Returns:

  • (String)

    the span id of the test module.



64
65
66
# File 'lib/datadog/ci/test.rb', line 64

def test_module_id
  get_tag(Ext::Test::TAG_TEST_MODULE_ID)
end

#test_module_nameString

Name of the running test module this test belongs to.

Returns:

  • (String)

    the name of the test module.



70
71
72
# File 'lib/datadog/ci/test.rb', line 70

def test_module_name
  get_tag(Ext::Test::TAG_MODULE)
end

#test_session_idString

Span id of the running test session this test belongs to.

Returns:

  • (String)

    the span id of the test session.



76
77
78
# File 'lib/datadog/ci/test.rb', line 76

def test_session_id
  get_tag(Ext::Test::TAG_TEST_SESSION_ID)
end

#test_suiteDatadog::CI::TestSuite?

Running test suite that this test is part of (if any).

Returns:



45
46
47
48
# File 'lib/datadog/ci/test.rb', line 45

def test_suite
  suite_name = test_suite_name
  CI.active_test_suite(suite_name) if suite_name
end

#test_suite_idString

Span id of the running test suite this test belongs to.

Returns:

  • (String)

    the span id of the test suite.



52
53
54
# File 'lib/datadog/ci/test.rb', line 52

def test_suite_id
  get_tag(Ext::Test::TAG_TEST_SUITE_ID)
end

#test_suite_nameString

Name of the running test suite this test belongs to.

Returns:

  • (String)

    the name of the test suite.



58
59
60
# File 'lib/datadog/ci/test.rb', line 58

def test_suite_name
  get_tag(Ext::Test::TAG_SUITE)
end