Module: TestRailOperations

Defined in:
lib/files/testrail_operations.rb,
lib/files/TestCase.rb

Overview

API’s for Test Rail

Defined Under Namespace

Classes: TestCase

Constant Summary collapse

PASSED =
1
BLOCKED =
2
UNTESTED =
3
RETEST =
4
FAILED =
5
PENDING =
6

Class Method Summary collapse

Class Method Details

.add_test_case_to_test_plan(plan_id, entry_id, case_ids) ⇒ Object

Updates a test plan with the given entry_id and array of test case IDSs



228
229
230
231
232
233
234
235
236
237
# File 'lib/files/testrail_operations.rb', line 228

def self.add_test_case_to_test_plan(plan_id, entry_id, case_ids)
  request = "update_plan_entry/#{plan_id}/#{entry_id}"
  data = {
      "suite_id" => self.suite_id,
      "case_ids" => case_ids
  }

  trclient = get_test_rail_client
  trclient.send_post_retry(request, data)
end

.add_testcase_to_test_run(test_run_id, case_ids) ⇒ Object



271
272
273
274
275
276
# File 'lib/files/testrail_operations.rb', line 271

def self.add_testcase_to_test_run(test_run_id, case_ids)
  request = "update_run/#{test_run_id}"
  data = { "case_ids" => case_ids }
  trclient = get_test_rail_client
  trclient.send_post_retry(request, data)
end

.create_test_plan(name, description = 'created by api', entries = []) ⇒ Object



189
190
191
192
193
194
195
196
# File 'lib/files/testrail_operations.rb', line 189

def self.create_test_plan(name, description = 'created by api', entries = [])
  request  = "add_plan/#{self.project_id}"
  data = {
      "name" => name,
      "description" => description,
      "entries"=> entries}
  get_test_rail_client.send_post(request, data)
end

.create_test_plan_entry(plan_id, name, include_all_cases: true, case_ids: []) ⇒ Object

Adds one test run to a test plan Returns hash containing:

  1. The test run ID of the test run.

  2. The entry ID of the test run which is a large Guid like this:

“id”=>“638fd46c-7c3e-4818-9c90-f411a2dec52a”



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/files/testrail_operations.rb', line 209

def self.create_test_plan_entry(plan_id, name, include_all_cases: true, case_ids: [])
  if !include_all_cases && case_ids.count == 0
    return "Error! Must create a test plan with at least one test case"
  end

  request = "add_plan_entry/#{plan_id}"
  data = {
      "suite_id" => self.suite_id,
      "name" => name,
      "include_all" => include_all_cases,
      "case_ids" => case_ids
  }

  trclient = get_test_rail_client
  response = trclient.send_post_retry(request, data)
  { entry_id: response["id"], run_id: response["runs"][0]["id"] }
end

.create_test_plan_entry_with_runs(plan_id, data) ⇒ Object

new method to create plan entry with runs



199
200
201
202
# File 'lib/files/testrail_operations.rb', line 199

def self.create_test_plan_entry_with_runs(plan_id, data)
  request  = "add_plan_entry/#{plan_id}"
  get_test_rail_client.send_post(request, data)
end

.create_test_run(project_id, suite_id, name, test_case_ids) ⇒ Object

Creates a test run on testrail param project_ID - The integer identifier of the project on test rail. param suite_id - The integer identifier of the test suite on test rail. param name - The string name to call the new test run param test_case_ids - The array of numerical integers of test cases to add to the test run returns - The number ID of the test run.



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/files/testrail_operations.rb', line 257

def self.create_test_run(project_id, suite_id, name, test_case_ids)
  request = "add_run/#{project_id}"
  data = {
    "suite_id" => suite_id,
    "name" => name,
    "include_all" => false,
    "case_ids" =>  test_case_ids
  }

  trclient = get_test_rail_client
  response = trclient.send_post_retry(request, data)
  response["id"]
end

.delete_plan_entry(plan_id, days, force_delete = false) ⇒ Object

For a given test plan deletes all runs equal to or older than the days passed Note that testrail stores runs as an entry and requires the entry id to remove the run To delete closer than 7 days, pass in force_delete = true



309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/files/testrail_operations.rb', line 309

def self.delete_plan_entry(plan_id, days, force_delete = false)
  if days >= 7 || force_delete == true
    clean_date = (Date.today - days).to_time.to_i
    trclient = get_test_rail_client
    request = "get_plan/#{plan_id}"
    response = trclient.send_get(request)
    response["entries"].each do |entry|
      if entry["runs"][0]["created_on"] <= clean_date
        uri = "delete_plan_entry/#{plan_id}/#{entry["id"]}"
        trclient.send_post_retry(uri, "")
      end
    end
  end
end

.get_test_rail_cases(suite_id = nil) ⇒ Object

Gets all the test cases for a particular test suite. The keys are the numeric(integer) test rail case ID’s. The Values are the instance of TestCase. Each TestCase instance corresponds to a test case in test rail. return - A hash of TestCase instances



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/files/testrail_operations.rb', line 109

def self.get_test_rail_cases(suite_id = nil)
  trclient        = get_test_rail_client
  screen_sizes    = get_test_rail_screen_size_codes
  priorities      = get_test_rail_priority_codes
  test_cases      = {}

  # retrieve test cases
  id_suite = suite_id ? suite_id : self.suite_id
  testcases_url = "get_cases/#{self.project_id}&suite_id=#{id_suite}"
  response = trclient.send_get(testcases_url)
  response.each do |test_case|
    id = test_case["id"]
    size = test_case["custom_screen_size"]
    screen_size_description = screen_sizes[size]
    priority = test_case["priority_id"]
    priority_description = priorities[priority]
    if priority_description
      priority_code = priority_description[:user_friendly_priority]
      automated = test_case["custom_automated"]
      run_once = test_case["custom_run_once"]
      automatable = test_case["custom_to_be_automated"]
      references = test_case["refs"]
      tc = TestCase.new(
        id.to_s, test_case["title"], priority_code, automated,
        screen_size_description, automatable, references, run_once
      )
      tc.file = test_case["custom_spec_location"]
      test_cases[id] = tc
    end
  end

  test_cases
end

.get_test_rail_cases_for_all_suites(suite_ids) ⇒ Object

Gets all the test cases for all the suites that we have for Bridge in Testrail suite_ids - An array of integers for each test suite under a testrail project



145
146
147
148
149
150
151
# File 'lib/files/testrail_operations.rb', line 145

def self.get_test_rail_cases_for_all_suites(suite_ids)
  result = {}
  suite_ids.each do |id|
    result.merge!(get_test_rail_cases(id))
  end
  result
end

.get_test_rail_clientObject



35
36
37
38
39
40
41
# File 'lib/files/testrail_operations.rb', line 35

def self.get_test_rail_client
  url = ENV["TESTRAIL_URL"] || "https://canvas.testrail.com"
  trclient = TestRail::APIClient.new(url)
  trclient.user = ENV["TESTRAIL_USER"]
  trclient.password = ENV["TESTRAIL_PASSWORD"]
  trclient
end

.get_test_rail_plan(plan_id) ⇒ Object

Gets JSON data about an existing test plan docs.gurock.com/testrail-api2/reference-plans#get_plan



183
184
185
186
187
# File 'lib/files/testrail_operations.rb', line 183

def self.get_test_rail_plan(plan_id)
  trclient = get_test_rail_client
  request  = "get_plan/#{plan_id}"
  trclient.send_get(request)
end

.get_test_rail_plansObject

Returns a list of test plans for a project. docs.gurock.com/testrail-api2/reference-plans#get_plans



175
176
177
178
179
# File 'lib/files/testrail_operations.rb', line 175

def self.get_test_rail_plans
  trclient = get_test_rail_client
  request  = "get_plans/#{self.project_id}"
  trclient.send_get(request)
end

.get_test_rail_priority_codesObject

Gets the definition of the priority codes that are assigned to our test rail cases. The priority codes on testrail do not match our nice 1,2,3 numbers. Their codes are different from ours. i.e.

- Low Priority”, 3=>“2 - Test If Time”, 4=>“1 - Must Test”, 6=>“Smoke Test”

Therefore this function creates a more bridge friendly translation.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/files/testrail_operations.rb', line 78

def self.get_test_rail_priority_codes
  trclient = get_test_rail_client
  # retreive priority information
  # http://docs.gurock.com/testrail-api2/reference-priorities
  priority_response = trclient.send_get("get_priorities")

  results = {}
  priority_response.each do |priority|
    # find the numeric priority
    name = priority["name"]
    splits = name.split("-")
    key = priority["id"].to_i
    if splits.size == 2 && splits[0].to_i
      val = { name: name, user_friendly_priority: splits[0].to_i }
      results[key] = val
    elsif name == "Smoke Test"
      val = { name: "Smoke", user_friendly_priority: 0 }
      results[key] = val
    elsif name == "STUB"
      val = { name: "Stub", user_friendly_priority: 7 }
      results[key] = val
    end
  end
  results
end

.get_test_rail_runsObject

Gets JSON data about the test runs on testrail for the given project and suite



167
168
169
170
171
# File 'lib/files/testrail_operations.rb', line 167

def self.get_test_rail_runs
  trclient = get_test_rail_client
  request  = "get_runs/#{self.project_id}"
  trclient.send_get(request)
end

.get_test_rail_screen_size_codesObject

Gets the definition of device types that are assigned to our test rail cases. It looks like this: 2=>“Tablet”, 3=>“Phone”



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/files/testrail_operations.rb', line 46

def self.get_test_rail_screen_size_codes
  trclient = get_test_rail_client
  case_fields = trclient.send_get("get_case_fields")

  results = {}
  case_fields.each do |case_field|
    if (case_field["name"] == "screen_size")
      config_array = case_field["configs"] # The array usually has a size of 1, but sometimes 0
      if config_array.size > 0
        config = config_array[0]
        # Some configs have items, like screen size
        items = config["options"]["items"]
        if items
          devices = items.split("\n")
          devices.each do |device|
            key, value = split_by_comma(device)
            results[key.to_i] = value
          end
        end
      end
    end
  end
  results
end

.get_test_rail_user_by_email(email) ⇒ Object

Given an email address, gets the user json data



389
390
391
392
393
394
395
396
397
398
# File 'lib/files/testrail_operations.rb', line 389

def self.get_test_rail_user_by_email(email)
  # The response looks like this:
  # {
  #    "email": "[email protected]",
  #    "id": 1,
  #    "is_active": true,
  #    "name": "Alexis Gonzalez"
  # }
  get_test_rail_client.send_get("get_user_by_email&email=#{email}")
end

.get_test_rail_usersObject



374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/files/testrail_operations.rb', line 374

def self.get_test_rail_users
  # Returns an array of user hashes. Like this:
  # [
  # {
  #    "email": "[email protected]",
  #    "id": 1,
  #    "is_active": true,
  #    "name": "Alexis Gonzalez"
  # },
  # ....
  # ]
  get_test_rail_client.send_get("get_users")
end

.get_test_run_cases(run_id) ⇒ Object

When a test run is created, the test cases have new, sort of temporary ID’s. These are completely different from the permanent static ID’s of the test cases. Given a test run ID, this gets the test cases that are assigned to that test run. It returns a hash where the key is the integer permanent ID, and the value is a TestCase instance.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/files/testrail_operations.rb', line 328

def self.get_test_run_cases(run_id)
  result = {}
  trclient = get_test_rail_client
  response = trclient.send_get("get_tests/#{run_id}")
  response.each do |r|
    # Each response hash for a test case in a run looks like this:
    #
    # {"id"=>481664, "case_id"=>152222, "status_id"=>5, "assignedto_id"=>49, "run_id"=>2641,
    # "title"=>"The my Learning page has all my courses listed. Overdue, Next Up, Optional,
    # Completed", "type_id"=>2, "priority_id"=>3, "estimate"=>nil, "estimate_forecast"=>nil,
    # "refs"=>nil, "milestone_id"=>nil, "custom_sprint_date"=>nil, "custom_commit_url"=>nil,
    # "custom_reviewed"=>false, "custom_automated"=>true,
    # "custom_spec_location"=>"regression_spec/learner_management/mylearning_spec.rb",
    # "custom_test_order"=>nil, "custom_screen_size"=>3, "custom_preconds"=>nil,
    # "custom_steps_separated"=>nil, "custom_browser_skip"=>[]}
    #

    permanent_id = r["case_id"].to_i
    tc = TestCase.new(
      permanent_id, r["title"], r["priority_id"].to_i,
      r["custom_automated"], r["custom_screen_size"].to_i,
      r["custom_to_be_automated"], r["refs"], r["custom_run_once"]
    )
    tc.temp_id = r["id"].to_i
    tc.assigned_to = r["assignedto_id"].to_i
    tc.set_status(status_testrail_to_rspec(r["status_id"].to_i), nil)

    value = tc
    key = permanent_id
    result[key] = value
  end
  result
end

.get_test_run_name(run_id) ⇒ Object

Gets the string name of the test run, given the test run id as an integer if the test id is not found, it returns an empty string



364
365
366
367
368
369
370
371
372
# File 'lib/files/testrail_operations.rb', line 364

def self.get_test_run_name(run_id)
  runs = get_test_rail_runs(self.project_id)
  runs.each do |run|
    if run_id == run["id"].to_i
      return run["name"]
    end
  end
  ""
end

.keep_only(plan_id, entry_id, case_ids) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/files/testrail_operations.rb', line 239

def self.keep_only(plan_id, entry_id, case_ids)
  request = "update_plan_entry/#{plan_id}/#{entry_id}"
  data = {
      "suite_id" => self.suite_id,
      "include_all" => false,
      "case_ids" => case_ids
  }

  trclient = get_test_rail_client
  trclient.send_post_retry(request, data)
end

.post_run_results(run_id, data) ⇒ Object

Sends test result data back to testrail to update cases in a test run param run_id - integer value designating the test run to update param data - A hash containing an array of hashes with test results



300
301
302
303
304
# File 'lib/files/testrail_operations.rb', line 300

def self.post_run_results(run_id, data)
  trclient = get_test_rail_client
  uri = "add_results/#{run_id}"
  trclient.send_post_retry(uri, "results" => data)
end

.project_idObject



17
18
19
# File 'lib/files/testrail_operations.rb', line 17

def self.project_id
  @@testrail_project_id
end

.set_testrail_ids(pid, sid) ⇒ Object



12
13
14
15
# File 'lib/files/testrail_operations.rb', line 12

def self.set_testrail_ids(pid, sid)
  @@testrail_project_id = pid
  @@testrail_suite_id = sid
end

.split_by_comma(device_string) ⇒ Object

Splits a string by a command and returns an array.



26
27
28
29
30
31
# File 'lib/files/testrail_operations.rb', line 26

def self.split_by_comma(device_string)
  splits = device_string.split(",")
  key = splits[0].chomp
  val = splits[1].lstrip
  [key, val]
end

.status_rspec_to_testrail(result_symbol) ⇒ Object

Converts an rspec test result (a symbol) to an integer that TestRail understands.



288
289
290
# File 'lib/files/testrail_operations.rb', line 288

def self.status_rspec_to_testrail(result_symbol)
  @rspec_to_testrail_status_map[result_symbol]
end

.status_testrail_to_rspec(int_id) ⇒ Object

Converts the a TestRail integer result status into a symbol (that rspec uses) that is human readable



293
294
295
# File 'lib/files/testrail_operations.rb', line 293

def self.status_testrail_to_rspec(int_id)
  @rspec_to_testrail_status_map.key(int_id)
end

.suite_idObject



21
22
23
# File 'lib/files/testrail_operations.rb', line 21

def self.suite_id
  @@testrail_suite_id
end

.update_references(testrail_id, reference) ⇒ Object

Updates a testcase that corresponds to the provided testrail_id. The testcase’s reference field will be linked to the provided Jira ticket, given in the format of PROJECT-ID (ex. “BR-1000”) param - testrail_id. The integer ID of the testcase param - reference. The string of the JIRA ticket(s)



159
160
161
162
163
164
# File 'lib/files/testrail_operations.rb', line 159

def self.update_references(testrail_id, reference)
  puts "id: #{testrail_id} => refs: #{reference}"
  url = "update_case/#{testrail_id}"
  data = { "refs" => reference }
  TestRailOperations.get_test_rail_client.send_post_retry(url, data)
end