Class: TestRail::API

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

Constant Summary collapse

TESTRAIL =
'testrail.com'
HEADERS =
{ "Content-Type" => "application/json" }
STATUSES =
{
    :passed   => 1,
    :pass     => 1,
    :blocked  => 2,
    :untested => 3,
    :retest   => 4,
    :failed   => 5,
    :fail     => 5
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ API

Initialize a new tesrail interface TestRail.new( :user => ‘[email protected]’,

:password  => 'passw0rd',
:namespace => 'yourteam' )


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/test_rail/api.rb', line 23

def initialize(args)
  raise "Need to provide arguments to constructor" if !args
  @user = args[:user] or raise "Missing username (:user => '[email protected]')"
  @password = args[:password] or raise "Missing password (:password => 'abc123')"
  @namespace = args[:namespace] or raise "Missing namespace (:namespace => 'testteam')"

  @server = @namespace + "." + TESTRAIL
  @host   = URI('https://' + @server)
  @api    = "/index.php?/api/v2/"
  @port   = 443

end

Instance Attribute Details

#serverObject

Returns the value of attribute server.



3
4
5
# File 'lib/test_rail/api.rb', line 3

def server
  @server
end

Instance Method Details

#add_case(args) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/test_rail/api.rb', line 213

def add_case(args)
  section_id = args[:section_id] or raise 'Missing section id (:section_id => 1)'
  title = args[:title] or raise 'Missing title (:title => "Use logs in")'
  steps = args[:steps]
  type_id = args[:type_id]
  priority_id = args[:priority_id]

  test_case = post('add_case', [section_id],
                     { :title => title, :custom_steps => steps,
                       :type_id => type_id, :priority_id => priority_id })
  
  TestRail::TestCase.new(test_case.merge({ :api => self }))
end

#add_plan(args) ⇒ Object

Add a new plan (If plan already exists, a new one is created with the same name) api.add_plan( :project_id => 1, :name => ‘My new Plan’, :description => ‘Test Plan’ ) Returns the plan object



76
77
78
79
80
81
82
83
# File 'lib/test_rail/api.rb', line 76

def add_plan(args)
  project_id = args[:project_id] or raise 'Missing project id (:project_id => 1)'
  name = args[:name] or raise 'Missing plan name (:name => "My Test Plan")'
  description = args[:description]

  plan = post('add_plan', [project_id], { :name => name, :description => description })
  TestRail::Plan.new(plan.merge({ :api => self }))
end

#add_result_for_case(args) ⇒ Object

Results API



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/test_rail/api.rb', line 245

def add_result_for_case(args)
  run_id = args[:run_id] or raise "Missing test run id (:run_id => 1)"
  case_id = args[:case_id] or raise "Missing test case id (:case_id => 1)"

  status_id = args[:status_id]
  if !status_id
    status_id = STATUSES[args[:status].to_sym]
    raise "Couldn't determine result status '#{args[:status]}'" if !status_id
  end

  result = {
      :status_id => status_id,
      :comment   => args[:comment],
      :version   => args[:version],
      :elapsed   => args[:elapsed]
  }

  test_rail_result = post('add_result_for_case', [run_id, case_id], result)
  #TODO new this into a relevant TestRails object
end

#add_run(args) ⇒ Object

Creates a new run api.add_run( :project_id => project_id, :suite_id => suite_id ) Optional parameters: name: Name to give the run description: Description of the run



279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/test_rail/api.rb', line 279

def add_run(args)
  project_id = args[:project_id] or raise "Missing project id ( :project_id => 1)"
  suite_id = args[:suite_id] or raise "Missing suite id ( :suite_id => 1)"

  params = { 
             :suite_id => suite_id,
             :name     => args[:name],
             :description => args[:description] 
           }

  result = post('add_run', [project_id], params)
  TestRail::Run.new(result.merge({ :api => self }))
end

#add_run_in_plan(args) ⇒ Object

Add a new run in test plan api.add_run_in_plan( :project_id => project_id, :plan_id => plan_id, :suite_id => suite_id )



295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/test_rail/api.rb', line 295

def add_run_in_plan(args)
  project_id = args[:project_id] or raise "Missing project id ( :project_id => 1)"
  plan_id = args[:plan_id] or raise "Missing project id ( :project_id => 1)"
  suite_id = args[:suite_id] or raise "Missing suite id ( :suite_id => 1)"

  params = { 
             :project_id => project_id,
             :suite_id => suite_id,
             :name     => args[:name],
             :description => args[:description] 
           }
  result = post('add_plan_entry', [plan_id], params)["runs"][0]
  TestRail::Run.new(result.merge({ :api => self }))
end

#add_section(args) ⇒ Object

Section API Calls



164
165
166
167
168
169
170
171
172
# File 'lib/test_rail/api.rb', line 164

def add_section(args)
  project_id = args[:project_id] or raise 'Missing project id (:project_id => 1)'
  name = args[:name] or raise 'Missing name (:name => "UI features")'
  suite_id = args[:suite_id] or raise 'Missing suite id (:suite_id => 1)'
  parent_id = args[:parent_id]

  section = post('add_section', [project_id], { :suite_id => suite_id, :name => name, :parent_id => parent_id })
  TestRail::Section.new(section.merge({ :api => self, :project_id => project_id, :suite_id => suite_id }))
end

#add_suite(args) ⇒ Object

Add a new suite (If suite already exists, a new one is created with the same name) api.add_suite( :project_id => 1, :name => ‘Cucumber features’, :description => ‘BDD Tests’ ) Returns the suite object



123
124
125
126
127
128
129
130
# File 'lib/test_rail/api.rb', line 123

def add_suite(args)
  project_id = args[:project_id] or raise 'Missing project id (:project_id => 1)'
  name = args[:name] or raise 'Missing name (:name => "Cucumber features")'
  description = args[:description]

  suite = post('add_suite', [project_id], { :name => name, :description => description })
  TestRail::Suite.new(suite.merge({ :api => self }))
end

#find_project(args) ⇒ Object

Search for a project by name testrail.find_project(:name => ‘Amazing project’)



58
59
60
61
62
63
64
65
66
# File 'lib/test_rail/api.rb', line 58

def find_project(args)
  name = args[:name] or raise "Missing name (:name => 'Amazing Project')"
  projects      = get_projects
  projectexists = projects.select { |p| p.name == name }.first
  if (!projectexists)
    raise "Project Not Found."
  end
  return projectexists
end

#get_case(args) ⇒ Object



207
208
209
210
211
# File 'lib/test_rail/api.rb', line 207

def get_case(args)
  case_id = args[:case_id] or raise 'Missing test case id (:case_id => 1)'
  testcase = get('get_case', [case_id])
  TestRail::TestCase.new(testcase.merge({ :api => self }))
end

#get_case_typesObject

Get all the case types defined for this testrail instance



320
321
322
323
324
325
326
# File 'lib/test_rail/api.rb', line 320

def get_case_types
  result = get('get_case_types')

  result.collect do |type|
    TestRail::CaseType.new(type.merge({ :api => self  }))
  end
end

#get_cases(args) ⇒ Object

Testcase API



197
198
199
200
201
202
203
204
205
# File 'lib/test_rail/api.rb', line 197

def get_cases(args)
  project_id = args[:project_id] or raise 'Missing project id (:project_id => 1)'
  suite_id = args[:suite_id] or raise 'Missing suite id (:suite_id => 1)'
  section_id = args[:section_id]
  list       = get('get_cases', [project_id], { :suite_id => suite_id, :section_id => section_id })
  list.collect do |item|
    TestRail::TestCase.new(item.merge({ :api => self }))
  end
end

#get_plan(args) ⇒ Object

Given a plan id, returns a plan (and populates internal run objects)



97
98
99
100
101
102
103
104
# File 'lib/test_rail/api.rb', line 97

def get_plan(args)
  plan_id = args[:plan_id] or raise "Missing plan id (:plan_id => 1)"
  result = get('get_plan', [plan_id])
  raw_runs = result['entries'].collect { |e| e['runs'] }.flatten
  runs     = raw_runs.each.collect { |r| TestRail::Run.new(r.merge({ :api => self })) }
  plan = TestRail::Plan.new(result.merge({ :api => self, :runs => runs }))
  plan
end

#get_plans(args) ⇒ Object

Get a list of plans for a project api.get_plans( :project_id => 1 ) Returns a list of plan objects



88
89
90
91
92
93
94
# File 'lib/test_rail/api.rb', line 88

def get_plans(args)
  id = args[:project_id] or raise "Missing project id (:project_id => 1)"
  list = get('get_plans', [id])
  list.collect do |item|
    TestRail::Plan.new(item.merge({ :api => self }))
  end
end

#get_prioritiesObject

Get all the priorities defined for this testrail instance



329
330
331
332
333
334
335
# File 'lib/test_rail/api.rb', line 329

def get_priorities
  result = get('get_priorities')

  result.collect do |priority|
    TestRail::Priority.new(priority.merge({ :api => self  }))
  end
end

#get_project(args) ⇒ Object

Return a specific project by id api.get_project( :id => 1 )



50
51
52
53
54
# File 'lib/test_rail/api.rb', line 50

def get_project(args)
  id = args[:id] or raise "Missing id (:id => 1)"
  project = get('get_project', [id])
  TestRail::Project.new(project.merge({ :api => self }))
end

#get_projectsObject

Return a list of projects



41
42
43
44
45
46
# File 'lib/test_rail/api.rb', line 41

def get_projects
  list = get('get_projects')
  list.collect do |item|
    TestRail::Project.new(item.merge({ :api => self }))
  end
end

#get_run(args) ⇒ Object

Given a run id, retrieve a plan



267
268
269
270
271
272
# File 'lib/test_rail/api.rb', line 267

def get_run(args)
  run_id = args[:run_id] or raise "Missing run id ( :run_id => 1)"

  result = get('get_run', [run_id])
  TestRail::Run.new(result.merge({ :api => self }))
end

#get_sections(args) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/test_rail/api.rb', line 174

def get_sections(args)
  project_id = args[:project_id] or raise 'Missing project id (:project_id => 1)'
  suite_id = args[:suite_id] or raise 'Missing suite id (:suite_id => 1)'
  list = get('get_sections', [project_id], { :suite_id => suite_id })
  list.collect do |item|
    TestRail::Section.new(item.merge({ :api => self, :project_id => project_id }))
  end
end

#get_suite(args) ⇒ Object

Get an existing suite by id testrail.get_suite( :id => 1 ) Returns the suite object



146
147
148
149
150
# File 'lib/test_rail/api.rb', line 146

def get_suite(args)
  id = args[:id] or raise "Missing suite id (:id => 1)"
  suite = get('get_suite', [id])
  TestRail::Suite.new(suite.merge({ :api => self }))
end

#get_suites(args) ⇒ Object

Get a list of suites for a project api.get_suites( :project_id => 1 ) Returns a list of suite objects



135
136
137
138
139
140
141
# File 'lib/test_rail/api.rb', line 135

def get_suites(args)
  id = args[:project_id] or raise "Missing project id (:project_id => 1)"
  list = get('get_suites', [id])
  list.collect do |item|
    TestRail::Suite.new(item.merge({ :api => self }))
  end
end

#get_tests(args) ⇒ Object



310
311
312
313
314
315
316
317
# File 'lib/test_rail/api.rb', line 310

def get_tests(args)
  run_id = args[:run_id] or raise "Missing run id (:run_id => 1)"
  list = get('get_tests', [run_id])

  list.collect do |item|
    TestRail::Test.new(item.merge({ :api => self }))
  end
end

#update_case(args) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/test_rail/api.rb', line 227

def update_case(args)
  case_id = args[:case_id] or raise "Missing case id (:case_id => 1)"
  
  new_values = {}

  new_values[:title] = args[:title] if args[:title]
  new_values[:custom_steps] = args[:steps] if args[:steps]
  new_values[:priority_id] = args[:priority_id] if args[:priority_id] 
  new_values[:type_id] = args[:type_id] if args[:type_id]

  test_case = post('update_case', [case_id], new_values )
  
  TestRail::TestCase.new(test_case.merge({ :api => self }))
end

#update_plan(args) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/test_rail/api.rb', line 106

def update_plan(args)
  id = args[:id] or raise "Missing plan id (:id => 1)"
  name        = args[:name]
  description = args[:description]

  plan = post('update_plan', [id], { :name => name, :description => description })
  TestRail::Plan.new(plan.merge({ :api => self }))
end

#update_section(args) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/test_rail/api.rb', line 183

def update_section(args)
  section_id = args[:section_id] or raise 'Missing section id (:section_id => 1)'
  project_id = args[:project_id] or raise 'Missing project id (:project_id => 1)'
  name = args[:name] or raise 'Missing name (:name => "UI features")'
  suite_id = args[:suite_id] or raise 'Missing suite id (:suite_id => 1)'
  parent_id = args[:parent_id]

  section = post('update_section', [section_id], { :suite_id => suite_id, :name => name, :parent_id => parent_id })
  TestRail::Section.new(section.merge({ :api => self, :project_id => project_id }))
end

#update_suite(args) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/test_rail/api.rb', line 152

def update_suite(args)
  id = args[:id] or raise "Missing suite id (:id => 1)"
  name        = args[:name]
  description = args[:description]

  suite = post('update_suite', [id], { :name => name, :description => description })
  TestRail::Suite.new(suite.merge({ :api => self }))
end