Module: TestLinker::Helpers

Included in:
TestLinker
Defined in:
lib/test_linker/helpers.rb

Overview

This module contains methods that aren’t a part of the TestLink API. They intend to make accessing TestLink database info easier.

Instance Method Summary collapse

Instance Method Details

#add_test_case_to_test_plan_by_name(project_name, plan_name, test_case_id, test_case_version) ⇒ Boolean

TODO:

NEED TO CLEAN THIS UP AND ADD ERROR CHECKING

TODO:

Need to update for having more than one of same test name inside testplan

Creates test in test suite within a test plan within a project.

Parameters:

  • project_name (String)
  • plan_name (String)
  • test_case_id (Fixnum, String)
  • test_case_version (String)

Returns:

  • (Boolean)

    true on success, false on fail



339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/test_linker/helpers.rb', line 339

def add_test_case_to_test_plan_by_name(project_name, plan_name, test_case_id,
    test_case_version)
  test_project_id = project_id(project_name)
  test_plan_id = test_plan_id(project_name, plan_name)

  result = add_test_case_to_test_plan(test_project_id, test_plan_id,
      test_case_id, test_case_version)

  if result.any?
    #Only way to tell if success if with the key "feature_id"
    return result.has_key?(:feature_id) ? true : false
  end
end

#api_versionString

Returns The version of TestLink’s API.

Returns:

  • (String)

    The version of TestLink’s API.



8
9
10
11
12
13
14
15
# File 'lib/test_linker/helpers.rb', line 8

def api_version
  if @api_version
    @api_version
  else
    about =~ /Testlink API Version: (.+) initially/
    @api_version = $1
  end
end

#build_id(project_name, plan_name, build_name) ⇒ Fixnum

Gets the ID for the given build name.

Parameters:

  • project_name (String)

    Name of the project to search for.

  • plan_name (String)

    Name of the plan to search for.

  • build_name (String)

    Name of the build to search for.

Returns:

  • (Fixnum)

    ID of plan matching project_name and plan_name.



58
59
60
61
62
63
64
65
66
67
# File 'lib/test_linker/helpers.rb', line 58

def build_id(project_name, plan_name, build_name)
  plan_id = test_plan_id(project_name, plan_name)
  builds = builds_for_test_plan plan_id

  build = builds.find do |build|
    build[:name] == build_name
  end

  build.nil? ? nil : build[:id].to_i
end

#create_first_level_suite(project_name, suite_name) ⇒ String

Get the ID of a first level suite, creating it if it does not exist.

TODO: This should rescue TestLinker::Error

Parameters:

  • project_name (String)
  • suite_name (String)

Returns:

  • (String)

    ID of the created or existing suite.



162
163
164
165
166
167
168
169
170
# File 'lib/test_linker/helpers.rb', line 162

def create_first_level_suite(project_name, suite_name)
  return first_level_test_suite_id(project_name, suite_name)
rescue RuntimeError

  # Create suite if it doesn't exist.
  project_id = project_id(project_name)

  create_test_suite(project_id, suite_name).first[:id]
end

#create_suite(suite_name, project_name, parent_id) ⇒ String

Get the ID of a suite with the given parent, creating it if it does not exist.

Parameters:

  • suite_name (String)
  • project_name (String)

Returns:

  • (String)

    ID of the created or existing suite.

Raises:



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/test_linker/helpers.rb', line 274

def create_suite(suite_name, project_name, parent_id)
  project_id = project_id(project_name)
  response = test_suites_for_test_suite(parent_id)

  if response.class == Array
    raise TestLinker::Error, response.first[:message]
  elsif response.class == Hash
    return response[:id] if response[:name] == suite_name

    response.each_value do |suite|
      return suite[:id] if suite[:name] == suite_name
    end
  end

  create_test_suite(project_id, suite_name, parent_id).first[:id]
end

#create_test_case_by_name(project_name, plan_name, suite_name, test_case_name, login, summary, steps, expected_results) ⇒ Array

TODO:

Need to update for having more than one of same test name inside test plan.

Creates test in test suite within a test plan within a project.

Parameters:

  • project_name (String)
  • plan_name (String)
  • suite_name (String)
  • test_case_name (String)
  • login (String)
  • summary (String)
  • steps (String)
  • expected_results (String)

Returns:

  • (Array)

    array-> array=test case id, array=test case version



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/test_linker/helpers.rb', line 303

def create_test_case_by_name(project_name, plan_name, suite_name,
    test_case_name, , summary, steps, expected_results)

  test_project_id = self.project_id(project_name)
  test_suite_id = self.suite_info(project_name, plan_name, suite_name)

  result = create_test_case(test_project_id, test_suite_id, test_case_name,
      summary, steps, expected_results, )

  if result.any?
    result.each do |result_ptr|
      if result_ptr[:message].eql? "Success!"
        if result_ptr.has_key? "additionalInfo"
          result_info = result_ptr.fetch("additionalInfo")
          if result_info[:msg].eql? "ok"
            test_case_id = result_info[:id]
            test_case_version = result_info[:version_number]
            return [test_case_id, test_case_version]
          else
            return -1
          end
        end
      end
    end
  end
end

#find_open_cases_for_plan(project_name, plan_name, options = {}) ⇒ Array<Hash> Also known as: find_open_cases_for_plans

Returns all open (not-run) test cases for a given plan within project. Extra options for now are :build, which will match a given build rather than all open builds by default.

Parameters:

  • project_name (String)
  • plan_name (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • build (String)

    Name of the build to match.

Returns:

  • (Array<Hash>)

    Array of matching testcase hashes.

See Also:



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/test_linker/helpers.rb', line 193

def find_open_cases_for_plan(project_name, plan_name, options={})
  test_case_array = []
  plan_id = test_plan_id(project_name, plan_name)

  if plan_id.nil?
    return []
  else
    builds = builds_for_test_plan(plan_id)
  end

  builds.each do |build|
    if options[:build]
      if build[:name] =~ options[:build]
        test_cases = test_cases_for_test_plan(build[:testplan_id],
            { "buildid" => build[:id] })
      end
    elsif build[:is_open] == 1
      test_cases = test_cases_for_test_plan(build[:testplan_id],
          { "buildid" => build[:id] })
    end

    unless test_cases.nil?
      test_cases.each_value do |test_case|
        test_case_array << test_case if test_not_run?(test_case)
      end
    end
  end

  test_case_array
end

#find_projects(regex, match_attribute = :name) ⇒ Array

Finds a list of projects whose attribute (given via the match_attribute parameter) matches regex.

Parameters:

  • regex (Regexp)

    The expression to match project names on.

  • match_attribute (Symbol) (defaults to: :name)

    Attribute of the projects to match on.

Returns:

  • (Array)

    An array of projects that match the Regexp.



75
76
77
78
79
80
81
# File 'lib/test_linker/helpers.rb', line 75

def find_projects(regex, match_attribute=:name)
  project_list = projects

  project_list.find_all do |project|
    project[match_attribute] =~ regex
  end
end

#find_test_plans(project_id, regex, match_attribute = :name) ⇒ Array

Finds a list of test plans whose attribute (given via the match_attribute parameter) matches regex.

Parameters:

  • project_id (Fixnum, String)

    ID of the project that the test plans belong to.

  • regex (Regexp)

    The expression to match test plan names on.

Returns:

  • (Array)

    An array of test plans that match the Regexp.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/test_linker/helpers.rb', line 90

def find_test_plans(project_id, regex, match_attribute=:name)
  test_plan_list = test_plans(project_id)
  matched_list = []

  if @version >= "1.0"
    matched_list = test_plan_list.find_all do |plan|
      plan[match_attribute] =~ regex
    end
  elsif @version < "1.0"
    test_plan_list.first.each_value do |plan|
      matched_list << plan if plan[match_attribute] =~ regex
    end
  end

  matched_list
end

#first_level_test_suite_id(project_name, suite_name) ⇒ Fixnum

Returns ID of the requested test suite. nil if not found.

Parameters:

  • project_name (String)
  • suite_name (String)

Returns:

  • (Fixnum)

    ID of the requested test suite. nil if not found.



110
111
112
113
114
115
116
117
118
# File 'lib/test_linker/helpers.rb', line 110

def first_level_test_suite_id(project_name, suite_name)
  test_suites = first_level_test_suites_for_project(project_id(project_name))

  test_suite = test_suites.find do |test_suite|
    test_suite[:name] == suite_name
  end

  test_suite.nil? ? nil : test_suite[:id].to_i
end

#project_id(project_name) ⇒ Fixnum

Gets ID of project matching the given project_name.

Parameters:

  • project_name (String)

    Name of the project to search for.

Returns:

  • (Fixnum)

    ID of project matching project_name.



21
22
23
24
25
26
27
28
29
# File 'lib/test_linker/helpers.rb', line 21

def project_id project_name
  if @version < "1.0"
    project = projects.find { |project| project[:name] == project_name }
  else
    project = project_by_name(project_name).first
  end

  project.nil? ? nil : project[:id].to_i
end

#suite_info(project_name, plan_name, suite_name) ⇒ Hash

TODO:

Need to update for having more than one of same test name inside test plan.

Gets info about test suite within a test plan within a project.

Parameters:

  • project_name (String)
  • plan_name (String)
  • suite_name (String)

Returns:

  • (Hash)

    The name and ID of the test suite.



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/test_linker/helpers.rb', line 143

def suite_info(project_name, plan_name, suite_name)
  test_plan_id = test_plan_id(project_name, plan_name)
  test_suites = test_suites_for_test_plan(test_plan_id)

  if test_suites.empty?
    return nil
  end

  test_suites.find do |test_suite|
    test_suite[:name].include? suite_name
  end
end

#test_info(project_name, plan_name, test_case_name) ⇒ Hash

TODO:

Need to update for having more than one of same test name inside test plan.

Gets info about test case within a test plan within a project.

Parameters:

  • project_name (String)

    Name of the project to search for.

  • plan_name (String)

    Name of the plan to search for.

  • test_case_name (String)

    Name of the test case to search for.

Returns:

  • (Hash)

    Info on the first matching test case.



127
128
129
130
131
132
133
134
# File 'lib/test_linker/helpers.rb', line 127

def test_info(project_name, plan_name, test_case_name)
  test_plan_id = test_plan_id(project_name, plan_name)
  test_cases = test_cases_for_test_plan(test_plan_id)

  test_cases.values.find do |test_case|
    test_case[:name] == test_case_name
  end
end

#test_not_run?(test_case) ⇒ Boolean

Checks if the test case has been executed.

Todo:

  • versioning support (make_call = :exec_status vs ‘exec_status’)‘

  • Also accept TC_ID instead?

Parameters:

  • test_case (Hash)

    A testcase Hash (AKA ‘testcase info’)

Returns:

  • (Boolean)

    true if the test hasn’t yet been executed.



179
180
181
# File 'lib/test_linker/helpers.rb', line 179

def test_not_run?(test_case)
  test_case[:exec_status] == 'n'
end

#test_plan_id(project_name, plan_name) ⇒ Fixnum

Gets info about test plans within a project

Parameters:

  • project_name (String)

    Name of the project to search for.

  • plan_name (String)

    Name of the plan to search for.

Returns:

  • (Fixnum)

    ID of plan matching project_name and plan_name. nil if the test plan wasn’t found.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/test_linker/helpers.rb', line 37

def test_plan_id(project_name, plan_name)
  if @version < "1.0"
    project_id = project_id project_name
    test_plans = test_plans(project_id)

    test_plan = test_plans.first.values.find do |project_test_plan|
      project_test_plan[:name] == plan_name
    end
  else
    test_plan = test_plan_by_name(project_name, plan_name).first
  end

  test_plan.nil? ? nil : test_plan[:id].to_i
end