Class: Trainer::XCResult::TestSuite

Inherits:
Object
  • Object
show all
Defined in:
trainer/lib/trainer/xcresult/test_suite.rb

Overview

Represents a test suite, including its test cases and sub-suites

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, identifier:, type:, result:, tags: [], test_cases: [], sub_suites: []) ⇒ TestSuite

Returns a new instance of TestSuite.



15
16
17
18
19
20
21
22
23
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 15

def initialize(name:, identifier:, type:, result:, tags: [], test_cases: [], sub_suites: [])
  @name = name
  @identifier = identifier
  @type = type
  @result = result
  @tags = tags
  @test_cases = test_cases
  @sub_suites = sub_suites
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



8
9
10
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 8

def identifier
  @identifier
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 7

def name
  @name
end

#resultObject (readonly)

Returns the value of attribute result.



10
11
12
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 10

def result
  @result
end

#sub_suitesObject (readonly)

Returns the value of attribute sub_suites.



12
13
14
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 12

def sub_suites
  @sub_suites
end

#tagsObject (readonly)

Returns the value of attribute tags.



13
14
15
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 13

def tags
  @tags
end

#test_casesObject (readonly)

Returns the value of attribute test_cases.



11
12
13
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 11

def test_cases
  @test_cases
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 9

def type
  @type
end

Class Method Details

.from_json(node:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 25

def self.from_json(node:)
  # Create initial TestSuite with basic attributes
  test_suite = new(
    name: node['name'],
    identifier: node['nodeIdentifier'],
    type: node['nodeType'],
    result: node['result'],
    tags: node['tags'] || []
  )

  # Process children to populate test_cases and sub_suites
  test_suite.process_children(node['children'] || [])

  test_suite
end

Instance Method Details

#durationObject



53
54
55
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 53

def duration
  @duration ||= @test_cases.sum(&:duration) + @sub_suites.sum(&:duration)
end

#failed?Boolean

Returns:



45
46
47
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 45

def failed?
  @result == 'Failed'
end

#failures_count(output_remove_retry_attempts: false) ⇒ Object

When output_remove_retry_attempts is true, counts reflect each test case's final attempt only (matching the trimmed JUnit output), so a test that failed then was skipped on retry is no longer counted as a failure.



64
65
66
67
68
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 64

def failures_count(output_remove_retry_attempts: false)
  predicate = output_remove_retry_attempts ? :final_failed? : :failed?
  @test_cases.count(&predicate) +
    @sub_suites.sum { |suite| suite.failures_count(output_remove_retry_attempts: output_remove_retry_attempts) }
end

#passed?Boolean

Returns:



41
42
43
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 41

def passed?
  @result == 'Passed'
end

#process_children(children) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 133

def process_children(children)
  children.each do |child|
    case child['nodeType']
    when 'Test Case'
      # Use from_json to generate multiple test cases if needed
      @test_cases.concat(TestCase.from_json(node: child))
    when 'Test Suite', 'Unit test bundle', 'UI test bundle'
      @sub_suites << TestSuite.from_json(node: child)
    end
  end
end

#skipped?Boolean

Returns:



49
50
51
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 49

def skipped?
  @result == 'Skipped'
end

#skipped_count(output_remove_retry_attempts: false) ⇒ Object



70
71
72
73
74
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 70

def skipped_count(output_remove_retry_attempts: false)
  predicate = output_remove_retry_attempts ? :final_skipped? : :skipped?
  @test_cases.count(&predicate) +
    @sub_suites.sum { |suite| suite.skipped_count(output_remove_retry_attempts: output_remove_retry_attempts) }
end

#test_cases_countObject



57
58
59
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 57

def test_cases_count
  @test_cases_count ||= @test_cases.count + @sub_suites.sum(&:test_cases_count)
end

#to_hash(output_remove_retry_attempts: false) ⇒ Object

Hash representation used by TestParser to collect test results



94
95
96
97
98
99
100
101
102
103
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 94

def to_hash(output_remove_retry_attempts: false)
  {
    number_of_tests: total_tests_count(output_remove_retry_attempts: output_remove_retry_attempts),
    number_of_failures: total_failures_count(output_remove_retry_attempts: output_remove_retry_attempts),
    number_of_tests_excluding_retries: test_cases_count,
    number_of_failures_excluding_retries: failures_count(output_remove_retry_attempts: output_remove_retry_attempts),
    number_of_retries: total_retries_count(output_remove_retry_attempts: output_remove_retry_attempts),
    number_of_skipped: skipped_count(output_remove_retry_attempts: output_remove_retry_attempts)
  }
end

#to_xml(output_remove_retry_attempts: false) ⇒ Object

Generates a JUnit-compatible XML representation of the test suite See https://github.com/testmoapp/junitxml/



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 107

def to_xml(output_remove_retry_attempts: false)
  testsuite = Helper.create_xml_element('testsuite',
    name: @name,
    time: duration.to_s,
    tests: test_cases_count.to_s,
    failures: failures_count(output_remove_retry_attempts: output_remove_retry_attempts).to_s,
    skipped: skipped_count(output_remove_retry_attempts: output_remove_retry_attempts).to_s)

  # Add test cases
  @test_cases.each do |test_case|
    runs = test_case.to_xml_nodes
    # Remove retry attempts: keep only the final run (whether it passes, fails, or is skipped)
    if output_remove_retry_attempts
      runs = runs.last(1)
    end
    runs.each { |node| testsuite.add_element(node) }
  end

  # Add sub-suites
  @sub_suites.each do |sub_suite|
    testsuite.add_element(sub_suite.to_xml(output_remove_retry_attempts: output_remove_retry_attempts))
  end

  testsuite
end

#total_failures_count(output_remove_retry_attempts: false) ⇒ Object



81
82
83
84
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 81

def total_failures_count(output_remove_retry_attempts: false)
  @test_cases.sum { |test_case| test_case.total_failures_count(output_remove_retry_attempts: output_remove_retry_attempts) } +
    @sub_suites.sum { |suite| suite.total_failures_count(output_remove_retry_attempts: output_remove_retry_attempts) }
end

#total_retries_count(output_remove_retry_attempts: false) ⇒ Object



86
87
88
89
90
91
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 86

def total_retries_count(output_remove_retry_attempts: false)
  return 0 if output_remove_retry_attempts

  @test_cases.sum(&:retries_count) +
    @sub_suites.sum(&:total_retries_count)
end

#total_tests_count(output_remove_retry_attempts: false) ⇒ Object



76
77
78
79
# File 'trainer/lib/trainer/xcresult/test_suite.rb', line 76

def total_tests_count(output_remove_retry_attempts: false)
  @test_cases.sum { |test_case| test_case.total_tests_count(output_remove_retry_attempts: output_remove_retry_attempts) } +
    @sub_suites.sum { |suite| suite.total_tests_count(output_remove_retry_attempts: output_remove_retry_attempts) }
end