Class: Trainer::XCResult::TestPlan

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
trainer/lib/trainer/xcresult/test_plan.rb

Overview

Represents a collection of test suites + the configuration, and device used to run them

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_suites:, configurations: [], devices: [], output_remove_retry_attempts: false) ⇒ TestPlan

Returns a new instance of TestPlan.



10
11
12
13
14
15
# File 'trainer/lib/trainer/xcresult/test_plan.rb', line 10

def initialize(test_suites:, configurations: [], devices: [], output_remove_retry_attempts: false)
  @test_suites = test_suites
  @configurations = configurations
  @devices = devices
  @output_remove_retry_attempts = output_remove_retry_attempts
end

Instance Attribute Details

#configurationsObject (readonly)

Returns the value of attribute configurations.



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

def configurations
  @configurations
end

#devicesObject (readonly)

Returns the value of attribute devices.



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

def devices
  @devices
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#output_remove_retry_attemptsObject

Returns the value of attribute output_remove_retry_attempts.



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

def output_remove_retry_attempts
  @output_remove_retry_attempts
end

#test_suitesObject (readonly)

Returns the value of attribute test_suites.



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

def test_suites
  @test_suites
end

Class Method Details

.from_json(json:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'trainer/lib/trainer/xcresult/test_plan.rb', line 17

def self.from_json(json:)
  # Extract configurations and devices
  configurations = json['testPlanConfigurations'] || []
  devices = json['devices'] || []

  # Find the test plan node (root of test results)
  test_plan_node = json['testNodes']&.find { |node| node['nodeType'] == 'Test Plan' }
  return new(test_suites: []) if test_plan_node.nil?

  # Convert test plan node's children (test bundles) to TestSuite objects
  test_suites = test_plan_node['children']&.map do |test_bundle|
    TestSuite.from_json(
      node: test_bundle
    )
  end || []

  new(
    test_suites: test_suites,
    configurations: configurations,
    devices: devices
  )
end

Instance Method Details

#each(&block) ⇒ Object



42
43
44
# File 'trainer/lib/trainer/xcresult/test_plan.rb', line 42

def each(&block)
  test_suites.map(&:to_hash).each(&block)
end

#to_xmlObject

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'trainer/lib/trainer/xcresult/test_plan.rb', line 48

def to_xml
  # Create the root testsuites element with calculated summary attributes
  testsuites = Helper.create_xml_element('testsuites',
    tests: test_suites.sum(&:test_cases_count).to_s,
    failures: test_suites.sum(&:failures_count).to_s,
    skipped: test_suites.sum(&:skipped_count).to_s,
    time: test_suites.sum(&:duration).to_s)

  # Create <properties> node for configuration and device, to be applied to each suite node
  properties = Helper.create_xml_element('properties').tap do |node|
    @configurations.each do |config|
      config_prop = Helper.create_xml_element('property', name: 'Configuration', value: config['configurationName'])
      node.add_element(config_prop)
    end

    @devices.each do |device|
      device_prop = Helper.create_xml_element('property', name: 'device', value: "#{device.fetch('modelName', 'Unknown Device')} (#{device.fetch('osVersion', 'Unknown OS Version')})")
      node.add_element(device_prop)
    end
  end

  # Add each test suite to the root
  test_suites.each do |suite|
    suite_node = suite.to_xml(output_remove_retry_attempts: output_remove_retry_attempts)
    # In JUnit conventions, the <testsuites> root element can't have properties
    # So we add the <properties> node to each child <testsuite> node instead
    suite_node.add_element(properties.dup) if properties.elements.any?
    testsuites.add_element(suite_node)
  end

  # Convert to XML string with prologue
  doc = REXML::Document.new
  doc << REXML::XMLDecl.new('1.0', 'UTF-8')

  doc.add(testsuites)

  formatter = REXML::Formatters::Pretty.new
  output = String.new
  formatter.write(doc, output)
  output
end