#!/usr/bin/env python
# encoding: utf-8
'''
TestLink API Examples
Created on Aug 13, 2019
@change: Aug 13, 2019 Hongping Wang: initialization
'''
import logging
# # pip install TestLink-API-Python-client
import testlink
class MyError(Exception):
'''
Generic exception to raise and log different fatal errors.
'''
def __init__(self, msg):
super(MyError).__init__(type(self))
self.msg = msg
def __str__(self):
return self.msg
class TestLinkOp(object):
'''
Operate Testlink by TestLink-API-Python-client
'''
def __init__(self, server_ul, dev_key, project_name='roadDB'):
'''
'''
self._server_url = server_ul
self._dev_key = dev_key
self.tl = testlink.TestlinkAPIClient(tl_url, tl_dev_key)
self._project_name = project_name
self._tl_project_info = self.get_project_info_by_name(self._project_name)
self._plan_id = None
self._project_id = self._tl_project_info['id']
self._project_prefix = self._tl_project_info['prefix']
def get_project_info_by_name(self, name):
'''
@summary: get project information by project name
'''
for project_item in self.tl.getProjects():
if project_item['name'] == name:
return project_item
return {}
def set_plan_id(self, plan_id):
'''
@summary: set plan id
'''
self._plan_id = plan_id
def _get_plan_id(self):
'''
@summary: if not set plan id, raise exception
'''
if not self._plan_id:
raise MyError('not set plan id')
return self._plan_id
def reset_project_name(self, name):
'''
@summary: reset project name
'''
self._project_name = name
self._project_id = self.tl.getProjectIDByName(self._project_name)
def create_test_plan(self, plan_name, note='CI creation', active=1, public=1):
'''
@summary: create test plan
'''
try:
return self.tl.createTestPlan(plan_name, self._project_name, note=note, active=active, public=public)
except testlink.testlinkerrors.TLResponseError as e:
if e.code == 3034:
logging.warn('%s', e.message)
else:
logging.exception('%s: %s', e.code, e.message)
return {}
def get_test_plan(self, plan_name):
'''
@summary: return like {'name': 'RDB-4.0.4.X', 'notes': u'<p>\u8ba1\u5212\u5f00\u59cb\u65e5\u671f\uff1a2019-08-16\uff0c\u8ba1\u5212\u7ed3\u675f\u65e5\u671f\uff1a 2019-08-26</p>', 'active': '1', 'is_public': '1', 'testproject_id': '262', 'id': '328'}
'''
projects = self.tl.getProjects()
for item in projects:
if item['name'] == self._project_name:
plans = self.tl.getProjectTestPlans(item['id'])
for sub_item in plans:
if sub_item['name'] == plan_name:
return sub_item
return {}
def create_test_build(self, name, buildnotes='CI creation', active=1, public_build=1):
'''
@summary: create test build
'''
return self.tl.createBuild(testplanid=self._get_plan_id(), buildname=name, buildnotes=buildnotes, active=active, open=public_build)
def get_build_by_name(self, name):
'''
@summary: get build information by build name
'''
all_build = self.tl.getBuildsForTestPlan(self._get_plan_id())
for item in all_build:
if item['name'] == name:
return item
return {}
def add_test_case_to_plan(self, case_id, testplanid, version=None):
'''
@summary: add test case to test plan
'''
testcaseexternalid = '{0}-{1}'.format(self._project_prefix, case_id)
if not version:
version = int(self.tl.getTestCase(None, testcaseexternalid=testcaseexternalid)[0]['version'])
return self.tl.addTestCaseToTestPlan(testcaseexternalid=testcaseexternalid,
testprojectid=self._project_id, testplanid=testplanid, version=version)
def