Class: Kankankan::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/kankankan.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_id, user, password, base_url = nil) ⇒ Project

Returns a new instance of Project.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kankankan.rb', line 101

def initialize(project_id, user, password, base_url = nil)
  @project_id = project_id
  @client = APIClient.new(base_url.nil? ? 'https://testrail.com':base_url)
  @client.user = user
  @client.password = password
  @project_info = @client.send_get("get_project/#{@project_id}")

  @test_plan_id = ENV['TEST_PLAN_ID'] if ENV.key?('TEST_PLAN_ID')
  @test_run_name = 'Debug Run'
  use_test_run_name(ENV['RUN_NAME']) if ENV.key?('RUN_NAME')
  use_test_run_id(ENV['RUN_ID']) if ENV.key?('RUN_ID')
end

Instance Attribute Details

#test_plan_idObject

Returns the value of attribute test_plan_id.



98
99
100
# File 'lib/kankankan.rb', line 98

def test_plan_id
  @test_plan_id
end

#test_suite_idObject

Returns the value of attribute test_suite_id.



99
100
101
# File 'lib/kankankan.rb', line 99

def test_suite_id
  @test_suite_id
end

Instance Method Details

#_get_test_run_entry_id(test_plan_id) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/kankankan.rb', line 132

def _get_test_run_entry_id(test_plan_id)
  runs = []
  test_run_entry_id = nil

  if @project_info['suite_mode'].to_i == 1
    runs = @client.send_get("get_runs/#{@project_id}")
  else
    if @test_plan_id
      @test_plan = @client.send_get("get_plan/#{test_plan_id}")

      @test_plan['entries'].each do |entry|
        runs += entry['runs']
      end
    end
  end
  runs.each do |run|
    if @test_run_name && @test_run_name != ''
      next unless run['name'].eql?(@test_run_name)
      test_run_entry_id = run['entry_id']
      break
    else
      next unless run['id'].to_s.eql?(@test_run_id)
      test_run_entry_id = run['entry_id']
      break
    end
  end

  test_run_entry_id
end

#_get_test_run_id(test_plan_id) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/kankankan.rb', line 162

def _get_test_run_id(test_plan_id)
  runs = []
  test_run_id = nil

  if @project_info['suite_mode'].to_i == 1
    runs = @client.send_get("get_runs/#{@project_id}")
  else
    if @test_plan_id
      @test_plan = @client.send_get("get_plan/#{test_plan_id}")

      @test_plan['entries'].each do |entry|
        runs += entry['runs']
      end
    end
  end

  runs.each do |run|
    if @test_run_name && @test_run_name != ''
      next unless run['name'].eql?(@test_run_name)
      test_run_id = run['id']
      break
    else
      next unless run['id'].to_s.eql?(@test_run_id)
      test_run_id = run['id']
      break
    end
  end

  test_run_id
end

#_get_test_run_id_from_name(_test_run_name) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/kankankan.rb', line 193

def _get_test_run_id_from_name(_test_run_name)
  runs = []
  test_run_id = nil

  if @project_info['suite_mode'].to_i == 1
    runs = @client.send_get("get_runs/#{@project_id}")
  else
    if @test_plan_id
      @test_plan = @client.send_get("get_plan/#{test_plan_id}")

      @test_plan['entries'].each do |entry|
        runs += entry['runs']
      end
    end
  end

  runs.each do |run|
    next unless run['name'].eql?(@test_run_name)
    test_run_id = run['id']
    break
  end

  test_run_id
end

#_prepare_test_run(test_run_id, case_id) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/kankankan.rb', line 235

def _prepare_test_run(test_run_id, case_id)
  existing_test_run_id = _test_run_id_exists?(test_run_id)

  if existing_test_run_id
    test_run_id = _get_test_run_id(@test_plan_id)
  end

  if @project_info['suite_mode'].to_i == 1
    new_run = @client.send_post("add_run/#{@project_id}", name: @test_run_name, include_all: false) unless existing_test_run_id
  else
    if @test_plan_id
      run_entry_id = _get_test_run_entry_id(@test_plan_id)

      unless run_entry_id
        newPlanEntry = @client.send_post("add_plan_entry/#{@test_plan_id}",
                                         suite_id: @test_suite_id,
                                         include_all: false,
                                         name: @test_run_name,
                                         case_ids:case_id.to_s.split(',').map(&:to_i)
        )
        new_run = newPlanEntry['runs'][0]
        run_entry_id = newPlanEntry['id']
      end

    else
      new_run = @client.send_post("add_run/#{@project_id}", name: @test_run_name, include_all: false) unless existing_test_run_id
    end
  end
  new_run = @client.send_get("get_run/#{test_run_id}") unless new_run

  # extract all tests in the run
  allTestsForRun = @client.send_get("get_tests/#{new_run['id']}")
  included_tests = []
  included_tests << case_id.to_i # append our new case
  allTestsForRun.each do |testInRun|
    included_tests << testInRun['case_id'].to_i
  end

  if @test_plan_id
    @client.send_post("update_plan_entry/#{@test_plan_id}/#{run_entry_id}",
                      include_all: false, case_ids: included_tests
                      )
  else
    @client.send_post("update_run/#{new_run['id']}", case_ids: included_tests)
  end

  new_run
end

#_test_run_id_exists?(test_run_id, test_plan_id = nil) ⇒ Boolean

Returns:

  • (Boolean)


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/kankankan.rb', line 218

def _test_run_id_exists?(test_run_id, test_plan_id = nil)
  if test_run_id.nil? || test_run_id.empty?
    if test_plan_id.nil?
      return true if _get_test_run_id_from_name(@test_run_name)
    end

    return false
  end

  if @project_info['suite_mode'].to_i == 1 || test_plan_id.nil?
    run = @client.send_get("get_run/#{test_run_id}")
    return !run.nil?
  else
    return !_get_test_run_entry_id(test_plan_id).nil?
  end
end

#add_result(case_id, ok, comment) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/kankankan.rb', line 284

def add_result(case_id, ok, comment)
  if case_id.to_i
    test_run = _prepare_test_run(@test_run_id, case_id)
    # puts test_run
    # puts "case id #{case_id}"
    # send the result to TestRail
    @client.send_post("add_result_for_case/#{test_run['id']}/#{case_id}",
                      status_id: ok ? 1 : 5,
                      comment: comment
                     )
  else
    puts 'Skipping testrail update due to empty case_id'
  end
end

#dumpObject



128
129
130
# File 'lib/kankankan.rb', line 128

def dump
  puts @project_info
end

#testsuitesObject



114
115
116
# File 'lib/kankankan.rb', line 114

def testsuites
  @test_suites = @client.send_get("get_suites/#{@project_id}") unless @test_suites
end

#use_test_run_id(test_run_id) ⇒ Object



118
119
120
121
# File 'lib/kankankan.rb', line 118

def use_test_run_id(test_run_id)
  @test_run_name = ''
  @test_run_id = test_run_id
end

#use_test_run_name(test_run_name) ⇒ Object



123
124
125
126
# File 'lib/kankankan.rb', line 123

def use_test_run_name(test_run_name)
  @test_run_name = test_run_name
  @test_run_id = ''
end