Module: Itriagetestrail::TestResults

Included in:
TestRailInterface
Defined in:
lib/itriagetestrail/testrail_objects/test_results.rb

Instance Method Summary collapse

Instance Method Details

#read_batch_fileObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/itriagetestrail/testrail_objects/test_results.rb', line 51

def read_batch_file
  return unless File.exist?('./tmp/batch')
  run_id = nil
  results = []
  File.open("./tmp/batch").each do |line|
    content = JSON.parse(line, symbolize_names: true)
    unless run_id
      run_id = content[:runId]
    end
      content[:results].each { |result| results << result }
  end
  {runId: run_id, results: results}
end

#send_batch_results_to_testrailObject

Use this method to read bulk records of results stored in the temp file, and send entire set as a post execution batch.



40
41
42
43
44
45
46
47
48
49
# File 'lib/itriagetestrail/testrail_objects/test_results.rb', line 40

def send_batch_results_to_testrail
  content = read_batch_file
  begin
    send = { results: content[:results] }
    @client.send_post("add_results_for_cases/#{content[:runId]}", send)
    clear_results
  rescue StandardError => e
    raise e
  end
end

#send_results_to_testrailObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/itriagetestrail/testrail_objects/test_results.rb', line 5

def send_results_to_testrail
  return if @results[:results].empty?
  # copy what is in the results
  @submitted[:results] << @results[:results]

  begin
    send = { results: @results[:results] }
    @client.send_post("add_results_for_cases/#{@run_id}", send)
    clear_results
  rescue StandardError => e
    raise e
  end
end

#store_result(scenario_title, external_id, scenario_steps, status_id, comment) ⇒ Object

Called during after_teardown, this saves the result of the test method or scenario into local memory for later processing



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/itriagetestrail/testrail_objects/test_results.rb', line 88

def store_result(scenario_title, external_id, scenario_steps, status_id, comment)
  update_test_suite scenario_title, external_id, scenario_steps

  case_id = testrail_test_case_id(external_id)
  @results[:results] << {
    case_id: case_id,
    scenario_title: scenario_title,
    external_id: external_id,
    scenario_steps: scenario_steps,
    status_id: status_id,
    comment: comment,
    custom_branch_name: @testrail_config[:origin]
  }

  # Pull Tag IDs
  unless @scenario_tags.nil?

    # https://github.com/cucumber/cucumber-ruby/blob/master/lib/cucumber/runtime.rb
    # Test has to actually run in order to populate testrail.

    testrail_tag_object = @testrail_case_fields.select { |field| field['system_name'] == 'custom_tags'}[0]
    testrail_tag_items = testrail_tag_object['configs'][0]['options']['items'].split("\n")
    scenario_tag_ids = []
    @scenario_tags.each do |scenario_tag|
      testrail_tag_items.each do | tag_item |
        system_tag = tag_item.split(',')
        if scenario_tag == system_tag[1]
          scenario_tag_ids << system_tag[0].to_i
        end
      end
    end

    current_case = @test_cases.select { |item| item['id'] == case_id }[0]

    testrail_case_tag_ids = current_case['custom_tags']
    testrail_case_steps = current_case['custom_steps']

    if testrail_case_tag_ids == scenario_tag_ids || testrail_case_steps == @scenario_steps
      @client.send_post("update_case/#{case_id}", { 'custom_tags': scenario_tag_ids, 'custom_steps': @scenario_steps})
    end
  end
end

#store_results_to_batch_fileObject

In implementations where there is not an at_exit, parallel test threads can store results of tests from the completed test class into a batch file, so final processing sends all results at the very end of the job while avoiding rate limiting issues. Each row is a separate json.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/itriagetestrail/testrail_objects/test_results.rb', line 22

def store_results_to_batch_file
  return if @results[:results].empty?
  entry = {runId: @run_id, results: @results[:results]}.to_json

  Dir.mkdir('./tmp') unless File.exist?('./tmp')
  file = File.open("./tmp/batch", 'a')
  file.write("#{entry}\n")
  file.close

  # keep track of what is submitted
  @submitted[:results] << @results[:results]

  # clear the buffer for next class
  @results[:results] = []
end

#update_test_suite(scenario_title, external_id, scenario_steps) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/itriagetestrail/testrail_objects/test_results.rb', line 65

def update_test_suite(scenario_title, external_id, scenario_steps)
  feature = external_id.split(';')[0].split('#')[0]

  # if the testrail case does not exist, update cases
  if testrail_test_case_id(external_id) == -1

    section_id = testrail_section_id(feature)

    # if the testrail section does not exist, update sections
    section_id = add_testrail_section(feature) if section_id == -1

    add_testrail_test_case(scenario_title, external_id, scenario_steps, section_id)
    sleep 1

    # Update Test Run
    extend_testrail_run
  end
  # store the case id associated with the external_id
  associate_result(external_id)
end