Class: TestRail::TestCaseResult

Inherits:
Object
  • Object
show all
Defined in:
lib/test_rail_integration/generator/test_case_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scenario) ⇒ TestCaseResult

Returns a new instance of TestCaseResult.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 31

def initialize(scenario)
  self.test_case_id = scenario.source_tag_names.find { |e| e.match(TEST_RAIL_ID_REGEX) }[2..-1]
  self.scenario = scenario
  if scenario.kind_of? Cucumber::Ast::OutlineTable::ExampleRow
    self.title = scenario.scenario_outline.title
    self.exception_message = scenario.scenario_exception unless defined?(scenario.scenario_exception).nil?
  else
    self.title = scenario.title
    self.exception_message = scenario.steps.exception unless defined?(scenario.steps).nil?
  end
  self.test_results = TestRail::Connection.get_test_results(self.test_case_id)
  self.previous_comment = get_last_failed_comment unless get_indexes_of_fails.empty?
end

Instance Attribute Details

#assign_toObject

Returns the value of attribute assign_to.



7
8
9
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 7

def assign_to
  @assign_to
end

#commentObject

Returns the value of attribute comment.



7
8
9
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 7

def comment
  @comment
end

#exception_messageObject

Returns the value of attribute exception_message.



7
8
9
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 7

def exception_message
  @exception_message
end

#previous_commentObject

Returns the value of attribute previous_comment.



7
8
9
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 7

def previous_comment
  @previous_comment
end

#scenarioObject

Returns the value of attribute scenario.



7
8
9
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 7

def scenario
  @scenario
end

#test_case_idObject

Returns the value of attribute test_case_id.



7
8
9
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 7

def test_case_id
  @test_case_id
end

#test_resultsObject

Returns the value of attribute test_results.



7
8
9
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 7

def test_results
  @test_results
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 7

def title
  @title
end

Instance Method Details

#failed?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 45

def failed?
  !scenario.passed?
end

#get_indexes_of_failsObject

Get indexes of failed results



60
61
62
63
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 60

def get_indexes_of_fails
  indexes = self.test_results.map.with_index { |result, index| result["status_id"] == COMMENT[:fail][:status] ? index : nil }
  indexes.compact
end

#get_indexes_of_passesObject



65
66
67
68
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 65

def get_indexes_of_passes
  indexes = self.test_results.map.with_index { |result, index| result["status_id"] == COMMENT[:pass][:status] ? index : nil }
  indexes.compact
end

#get_last_failed_commentObject

Get last failed comment for test case



83
84
85
86
87
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 83

def get_last_failed_comment
  comments = get_previous_comments
  index = get_indexes_of_fails.first
  comments[index]
end

#get_previous_commentsObject

Parse results and returns previous comment.



73
74
75
76
77
78
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 73

def get_previous_comments
  test_comment = self.test_results.map { |hash| hash["comment"] }
  comment = test_comment
  comment ||= []
  comment
end

#get_previous_test_resultObject

Parse results and returns Failed if this test was marked as failed.



92
93
94
95
96
97
98
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 92

def get_previous_test_result
  test_results = self.test_results.map { |status_hash| status_hash["status_id"] }
  status = FAILED if test_results.include?(FAILED)
  status ||= PASS if test_results.first == PASS
  status ||= NEW
  status
end

#passed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 49

def passed?
  scenario.passed? && get_previous_test_result != FAILED
end

#to_test_rail_apiObject

Send status to TestRail

1, comment: “Test passed”



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 122

def to_test_rail_api
  comment_message = "#{self.comment[:comment]} \"#{self.title}\""
  comment_message = "**[#{Fixtures.instance['venture']}]** #{self.comment[:comment]} for \"#{self.title}\"" unless defined?(Fixtures.instance['venture']).nil?
  comment_message += "\n Exception : #{self.exception_message}" unless self.exception_message.nil?
  comment_message += "\n #{self.previous_comment}" if self.comment[:status] == COMMENT[:fail][:status] || self.comment[:status] == COMMENT[:unchanged_pass][:status]
  if self.comment[:status] == COMMENT_STATUS
    {comment: comment_message}
  else
    {status_id: self.comment[:status], comment: comment_message}
  end
end

#unchanged_pass?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 53

def unchanged_pass?
  scenario.passed? && get_previous_test_result == FAILED
end

#updateObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/test_rail_integration/generator/test_case_result.rb', line 100

def update
  self.comment = COMMENT[:pass] if passed?
  self.comment ||= COMMENT[:unchanged_pass] if unchanged_pass?

  if failed?
    self.comment ||= COMMENT[:fail]
    self.exception_message = self.exception_message rescue nil
    self.assign_to = ASSIGN_TO
  end

  raise("Invalid test case result : scenario.passed? #{self.scenario.passed?}, prev_result? #{get_previous_test_result}, run_result? #{self.scenario.passed?}") if self.comment.nil?

  TestRail::Connection.commit_test_result(self)

  self
end