Class: Trainer::XCResult::TestCase

Inherits:
Object
  • Object
show all
Includes:
TestCaseAttributes
Defined in:
trainer/lib/trainer/xcresult/test_case.rb

Overview

Represents a test case, including its retries (aka repetitions)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TestCaseAttributes

#failed?, included, #passed?, #skipped?

Constructor Details

#initialize(name:, identifier:, duration:, result:, classname:, argument: nil, tags: [], retries: nil, failure_messages: [], source_references: [], attachments: []) ⇒ TestCase

Returns a new instance of TestCase.


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

def initialize(
  name:, identifier:, duration:, result:, classname:, argument: nil, tags: [], retries: nil,
  failure_messages: [], source_references: [], attachments: []
)
  @name = name
  @identifier = identifier
  @duration = duration
  @result = result
  @classname = classname
  @argument = argument
  @tags = tags
  @retries = retries
  @failure_messages = failure_messages
  @source_references = source_references
  @attachments = attachments
end

Instance Attribute Details

#argumentObject (readonly)

Returns the value of attribute argument.


16
17
18
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 16

def argument
  @argument
end

#attachmentsObject (readonly)

Returns the value of attribute attachments.


22
23
24
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 22

def attachments
  @attachments
end

#classnameObject (readonly)

Returns the value of attribute classname.


15
16
17
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 15

def classname
  @classname
end

#durationObject (readonly)

Returns the value of attribute duration.


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

def duration
  @duration
end

#failure_messagesObject (readonly)

Returns the value of attribute failure_messages.


20
21
22
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 20

def failure_messages
  @failure_messages
end

#identifierObject (readonly)

Returns the value of attribute identifier.


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

def identifier
  @identifier
end

#nameObject (readonly)

Returns the value of attribute name.


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

def name
  @name
end

#resultObject (readonly)

Returns the value of attribute result.


14
15
16
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 14

def result
  @result
end

#retriesArray<Repetition> (readonly)

This will be ‘nil` if the test case was not run multiple times, but will contain all repetitions if it was run more than once.

Returns:

  • (Array<Repetition>)

    Array of retry attempts for this test case, **including the initial attempt**


19
20
21
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 19

def retries
  @retries
end

#source_referencesObject (readonly)

Returns the value of attribute source_references.


21
22
23
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 21

def source_references
  @source_references
end

#tagsObject (readonly)

Returns the value of attribute tags.


23
24
25
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 23

def tags
  @tags
end

Class Method Details

.from_json(node:) ⇒ Object


42
43
44
45
46
47
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_case.rb', line 42

def self.from_json(node:)
  # Handle test case arguments
  argument_nodes = Helper.find_json_children(node, 'Arguments')
  argument_nodes = [nil] if argument_nodes.empty?

  # Generate test cases for each argument
  argument_nodes.map do |arg_node|
    # For repetition nodes, failure messages, source refs, attachments and result attributes,
    # Search them as children of the argument child node if present, of the test case node otherwise.
    node_for_attributes = arg_node || node

    retries = Helper.find_json_children(node_for_attributes, 'Repetition', 'Test Case Run')
                   &.map { |rep_node| Repetition.from_json(node: rep_node) } || []

    failure_messages = if retries.empty?
                         extract_failure_messages(node_for_attributes)
                       else
                         retries.flat_map(&:failure_messages)
                       end

    source_references = if retries.empty?
                          extract_source_references(node_for_attributes)
                        else
                          retries.flat_map(&:source_references)
                        end

    attachments = if retries.empty?
                    extract_attachments(node_for_attributes)
                  else
                    retries.flat_map(&:attachments)
                  end

    new(
      name: node['name'],
      identifier: node['nodeIdentifier'],
      duration: parse_duration(node['duration']),
      result: node_for_attributes['result'],
      classname: extract_classname(node),
      argument: arg_node&.[]('name'), # Only set if there is an argument
      tags: node['tags'] || [],
      retries: retries,
      failure_messages: failure_messages,
      source_references: source_references,
      attachments: attachments
    )
  end
end

Instance Method Details

#retries_countObject


114
115
116
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 114

def retries_count
  @retries&.count || 0
end

#to_xml_nodesArray<REXML::Element>

Generates XML nodes for the test case

  • If no retries, the array contains a single <testcase> element

  • If retries, the array contains one <testcase> element per retry

Returns:

  • (Array<REXML::Element>)

    An array of XML <testcase> elements


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 96

def to_xml_nodes
  runs = @retries.nil? || @retries.empty? ? [nil] : @retries

  runs.map do |run|
    Helper.create_xml_element('testcase',
      name: if @argument.nil?
              @name
            else
              @name.match?(/(\(.*\))/) ? @name.gsub(/(\(.*\))/, "(#{@argument})") : "#{@name} (#{@argument})"
            end,
      classname: @classname,
      time: (run || self).duration.to_s).tap do |testcase|
      add_xml_result_elements(testcase, run || self)
      add_properties_to_xml(testcase, repetition_name: run&.name)
    end
  end
end

#total_failures_countObject


122
123
124
125
126
127
128
129
130
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 122

def total_failures_count
  if retries_count > 0
    @retries.count(&:failed?)
  elsif failed?
    1
  else
    0
  end
end

#total_tests_countObject


118
119
120
# File 'trainer/lib/trainer/xcresult/test_case.rb', line 118

def total_tests_count
  retries_count > 0 ? retries_count : 1
end