Skip to content

Commit f86d877

Browse files
author
Luiko Czub
committed
suggestion for a generic testlink api lczub#7
uses decorators
1 parent 65a145e commit f86d877

File tree

6 files changed

+618
-65
lines changed

6 files changed

+618
-65
lines changed

example/TestLinkExampleGenericApi.py

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#! /usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
4+
# Copyright 2013 Luiko Czub, TestLink-API-Python-client developers
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# ------------------------------------------------------------------------
19+
20+
21+
"""
22+
23+
Shows how to use the TestLinkAPIGeneric.
24+
- does same steps as Example TestLinkAPI in TestLinkExample.py
25+
26+
=> Counts and lists the Projects
27+
=> Create a new Project with the following structure:
28+
29+
30+
NewProject
31+
|
32+
----NewTestPlan
33+
|
34+
------ Test Suite A
35+
| |
36+
| ------- Test Suite AA
37+
| |
38+
| --------- Test Case AA
39+
| |
40+
------ Test Suite B --- 5 manual test steps
41+
|
42+
--------- Test Case B
43+
|
44+
--- 5 automated test steps
45+
"""
46+
from testlink import TestlinkAPIGeneric, TestLinkHelper
47+
import sys
48+
49+
# precondition a)
50+
# SERVER_URL and KEY are defined in environment
51+
# TESTLINK_API_PYTHON_SERVER_URL=http://YOURSERVER/testlink/lib/api/xmlrpc.php
52+
# TESTLINK_API_PYTHON_DEVKEY=7ec252ab966ce88fd92c25d08635672b
53+
#
54+
# alternative precondition b)
55+
# SERVEUR_URL and KEY are defined as command line arguments
56+
# python TestLinkExample.py --server_url http://YOURSERVER/testlink/lib/api/xmlrpc.php
57+
# --devKey 7ec252ab966ce88fd92c25d08635672b
58+
#
59+
# ATTENTION: With TestLink 1.9.7, cause of the new REST API, the SERVER_URL
60+
# has changed from
61+
# (old) http://YOURSERVER/testlink/lib/api/xmlrpc.php
62+
# to
63+
# (new) http://YOURSERVER/testlink/lib/api/xmlrpc/v1/xmlrpc.php
64+
tl_helper = TestLinkHelper()
65+
tl_helper.setParamsFromArgs('''Shows how to use the TestLinkAPI.
66+
=> Counts and lists the Projects
67+
=> Create a new Project with the following structure:''')
68+
myTestLink = tl_helper.connect(TestlinkAPIGeneric)
69+
70+
71+
NEWPROJECT="NEW_PROJECT_API_GENERIC"
72+
NEWTESTPLAN="TestPlan_API_GENERIC"
73+
NEWTESTSUITE_A="A - First Level"
74+
NEWTESTSUITE_B="B - First Level"
75+
NEWTESTSUITE_AA="AA - Second Level"
76+
NEWTESTCASE_AA="TESTCASE_AA"
77+
NEWTESTCASE_B="TESTCASE_B"
78+
79+
80+
if myTestLink.checkDevKey() != True:
81+
print "Error with the devKey."
82+
sys.exit(-1)
83+
84+
# NOT GENERIC
85+
#print "Number of Projects in TestLink: %s " % (myTestLink.countProjects(),)
86+
# print ""
87+
# myTestLink.listProjects()
88+
# print ""
89+
# GENERIC
90+
print "Number of Projects in TestLink: %i " % len(myTestLink.getProjects())
91+
print ""
92+
for project in myTestLink.getProjects():
93+
print "Name: %(name)s ID: %(id)s " % project
94+
print ""
95+
96+
97+
# # Creates the project
98+
# NOT GENERIC
99+
# newProject = myTestLink.createTestProject(NEWPROJECT, "NPROAPI",
100+
# "notes=This is a Project created with the API", "active=1", "public=1",
101+
# "options=requirementsEnabled:0,testPriorityEnabled:1,automationEnabled:1,inventoryEnabled:0")
102+
# GENERIC
103+
newProject = myTestLink.createTestProject(
104+
testprojectname=NEWPROJECT, testcaseprefix='GPROAPI',
105+
notes='This is a Project created with the Generic API', active=1, public=1,
106+
requirementsEnabled=0, testPriorityEnabled=1, automationEnabled=1,
107+
inventoryEnabled=0)
108+
print(newProject)
109+
isOk = newProject[0]['message']
110+
if isOk=="Success!":
111+
newProjectID = newProject[0]['id']
112+
print "New Project '%s' - id: %s" % (NEWPROJECT,newProjectID)
113+
else:
114+
print "Error creating the project '%s': %s " % (NEWPROJECT,isOk)
115+
sys.exit(-1)
116+
117+
# # Creates the test plan
118+
# newTestPlan = myTestLink.createTestPlan(NEWTESTPLAN, NEWPROJECT,
119+
# "notes=New TestPlan created with the API","active=1", "public=1")
120+
# isOk = newTestPlan[0]['message']
121+
# if isOk=="Success!":
122+
# newTestPlanID = newTestPlan[0]['id']
123+
# print "New Test Plan '%s' - id: %s" % (NEWTESTPLAN,newTestPlanID)
124+
# else:
125+
# print "Error creating the Test Plan '%s': %s " % (NEWTESTPLAN, isOk)
126+
# sys.exit(-1)
127+
#
128+
# #Creates the test Suite A
129+
# newTestSuite = myTestLink.createTestSuite(newProjectID, NEWTESTSUITE_A,
130+
# "Details of the Test Suite A")
131+
# isOk = newTestSuite[0]['message']
132+
# if isOk=="ok":
133+
# newTestSuiteID = newTestSuite[0]['id']
134+
# print "New Test Suite '%s' - id: %s" % (NEWTESTSUITE_A, newTestSuiteID)
135+
# else:
136+
# print "Error creating the Test Suite '%s': %s " % (NEWTESTSUITE_A, isOk)
137+
# sys.exit(-1)
138+
#
139+
# FirstLevelID = newTestSuiteID
140+
#
141+
# #Creates the test Suite B
142+
# newTestSuite = myTestLink.createTestSuite(newProjectID, NEWTESTSUITE_B,
143+
# "Details of the Test Suite B")
144+
# isOk = newTestSuite[0]['message']
145+
# if isOk=="ok":
146+
# TestSuiteID_B = newTestSuite[0]['id']
147+
# print "New Test Suite '%s' - id: %s" % (NEWTESTSUITE_B, TestSuiteID_B)
148+
# else:
149+
# print "Error creating the Test Suite '%s': %s " % (NEWTESTSUITE_B, isOk)
150+
# sys.exit(-1)
151+
#
152+
# #Creates the test Suite AA
153+
# newTestSuite = myTestLink.createTestSuite(newProjectID, NEWTESTSUITE_AA,
154+
# "Details of the Test Suite AA","parentid="+FirstLevelID)
155+
# isOk = newTestSuite[0]['message']
156+
# if isOk=="ok":
157+
# TestSuiteID_AA = newTestSuite[0]['id']
158+
# print "New Test Suite '%s' - id: %s" % (NEWTESTSUITE_AA, TestSuiteID_AA)
159+
# else:
160+
# print "Error creating the Test Suite '%s': %s " % (NEWTESTSUITE_AA, isOk)
161+
# sys.exit(-1)
162+
#
163+
# MANUAL = 1
164+
# AUTOMATED = 2
165+
#
166+
# #Creates the test case TC_AA
167+
# myTestLink.initStep("Step action 1", "Step result 1", MANUAL)
168+
# myTestLink.appendStep("Step action 2", "Step result 2", MANUAL)
169+
# myTestLink.appendStep("Step action 3", "Step result 3", MANUAL)
170+
# myTestLink.appendStep("Step action 4", "Step result 4", MANUAL)
171+
# myTestLink.appendStep("Step action 5", "Step result 5", MANUAL)
172+
#
173+
# newTestCase = myTestLink.createTestCase(NEWTESTCASE_AA, TestSuiteID_AA,
174+
# newProjectID, "admin", "This is the summary of the Test Case AA",
175+
# "preconditions=these are the preconditions")
176+
# isOk = newTestCase[0]['message']
177+
# if isOk=="Success!":
178+
# newTestCaseID = newTestCase[0]['id']
179+
# print "New Test Case '%s' - id: %s" % (NEWTESTCASE_AA, newTestCaseID)
180+
# else:
181+
# print "Error creating the Test Case '%s': %s " % (NEWTESTCASE_AA, isOk)
182+
# sys.exit(-1)
183+
#
184+
# #Creates the test case TC_B
185+
# myTestLink.initStep("Step action 1", "Step result 1", AUTOMATED)
186+
# myTestLink.appendStep("Step action 2", "Step result 2", AUTOMATED)
187+
# myTestLink.appendStep("Step action 3", "Step result 3", AUTOMATED)
188+
# myTestLink.appendStep("Step action 4", "Step result 4", AUTOMATED)
189+
# myTestLink.appendStep("Step action 5", "Step result 5", AUTOMATED)
190+
#
191+
# newTestCase = myTestLink.createTestCase(NEWTESTCASE_B, TestSuiteID_B,
192+
# newProjectID, "admin", "This is the summary of the Test Case B",
193+
# "preconditions=these are the preconditions",
194+
# "executiontype=%i" % AUTOMATED)
195+
# isOk = newTestCase[0]['message']
196+
# if isOk=="Success!":
197+
# newTestCaseID = newTestCase[0]['id']
198+
# print "New Test Case '%s' - id: %s" % (NEWTESTCASE_B, newTestCaseID)
199+
# else:
200+
# print "Error creating the Test Case '%s': %s " % (NEWTESTCASE_B, isOk)
201+
# sys.exit(-1)
202+
#
203+
print ""
204+
# NOT GENERIC
205+
# print "Number of Projects in TestLink: %s " % (myTestLink.countProjects(),)
206+
# print ""
207+
# myTestLink.listProjects()
208+
# GENERIC
209+
print "Number of Projects in TestLink: %i " % len(myTestLink.getProjects())
210+
print ""
211+
for project in myTestLink.getProjects():
212+
print "Name: %(name)s ID: %(id)s " % project
213+
print ""
214+
#
215+
#
216+
#
217+
#

src/testlink/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

2121
from .testlinkerrors import TestLinkError
22+
from .testlinkapigeneric import TestlinkAPIGeneric
2223
from .testlinkapi import TestlinkAPIClient
2324
from .testlink import TestLink
2425
from .testlinkhelper import TestLinkHelper

src/testlink/testlinkapi.py

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /usr/bin/python
22
# -*- coding: UTF-8 -*-
33

4-
# Copyright 2011-2012 Olivier Renault, James Stock, TestLink-API-Python-client developers
4+
# Copyright 2011-2013 Olivier Renault, James Stock, TestLink-API-Python-client developers
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -17,66 +17,36 @@
1717
#
1818
# ------------------------------------------------------------------------
1919

20-
import xmlrpclib
20+
#import xmlrpclib
2121

22-
from testlinkhelper import TestLinkHelper, VERSION
23-
import testlinkerrors
22+
from testlinkapigeneric import TestlinkAPIGeneric, TestLinkHelper
23+
#import testlinkerrors
2424

2525

26-
class TestlinkAPIClient(object):
26+
class TestlinkAPIClient(TestlinkAPIGeneric):
2727

28-
__slots__ = ['server', 'devKey', 'stepsList', '_server_url']
29-
30-
__VERSION__ = VERSION
31-
32-
def __init__(self, server_url, devKey):
33-
self.server = xmlrpclib.Server(server_url)
34-
self.devKey = devKey
35-
self.stepsList = []
36-
self._server_url = server_url
37-
38-
def _callServer(self, methodAPI, argsAPI=None):
39-
""" call server method METHODAPI with error handling and returns the
40-
responds """
41-
42-
response = None
43-
try:
44-
if argsAPI is None:
45-
response = getattr(self.server.tl, methodAPI)()
46-
else:
47-
response = getattr(self.server.tl, methodAPI)(argsAPI)
48-
except (IOError, xmlrpclib.ProtocolError), msg:
49-
new_msg = 'problems connecting the TestLink Server %s\n%s' %\
50-
(self._server_url, msg)
51-
raise testlinkerrors.TLConnectionError(new_msg)
52-
except xmlrpclib.Fault, msg:
53-
new_msg = 'problems calling the API method %s\n%s' %\
54-
(methodAPI, msg)
55-
raise testlinkerrors.TLAPIError(new_msg)
56-
57-
return response
5828

5929
#
6030
# BUILT-IN API CALLS
6131
#
6232

63-
def checkDevKey(self):
64-
""" checkDevKey :
65-
check if Developer Key exists
66-
"""
67-
argsAPI = {'devKey' : self.devKey}
68-
return self._callServer('checkDevKey', argsAPI)
33+
# def checkDevKey(self):
34+
# """ checkDevKey :
35+
# check if Developer Key exists
36+
# """
37+
# argsAPI = {'devKey' : self.devKey}
38+
# return self._callServer('checkDevKey', argsAPI)
6939

70-
def about(self):
71-
""" about :
72-
Gives basic information about the API
73-
"""
74-
return self._callServer('about')
40+
# def about(self):
41+
# """ about :
42+
# Gives basic information about the API
43+
# """
44+
# return self._callServer('about')
7545

76-
def ping(self):
77-
""" ping :
78-
"""
79-
return self._callServer('ping')
46+
# def ping(self):
47+
# """ ping :
48+
# """
49+
# return self._callServer('ping')
8050

8151
def echo(self, message):
8252
return self._callServer('repeat', {'str': message})
@@ -133,12 +103,12 @@ def getLatestBuildForTestPlan(self, testplanid):
133103
'testplanid':str(testplanid)}
134104
return self._callServer('getLatestBuildForTestPlan', argsAPI)
135105

136-
def getProjects(self):
137-
""" getProjects:
138-
Gets a list of all projects
139-
"""
140-
argsAPI = {'devKey' : self.devKey}
141-
return self._callServer('getProjects', argsAPI)
106+
# def getProjects(self):
107+
# """ getProjects:
108+
# Gets a list of all projects
109+
# """
110+
# argsAPI = {'devKey' : self.devKey}
111+
# return self._callServer('getProjects', argsAPI)
142112

143113
def getProjectTestPlans(self, testprojectid):
144114
""" getLastExecutionResult :
@@ -564,13 +534,6 @@ def getProjectIDByName(self, projectName):
564534
break
565535
return result
566536

567-
def __str__(self):
568-
message = """
569-
TestlinkAPIClient - class %s - version %s
570-
@author: Olivier Renault, James Stock, TestLink-API-Python-client developers
571-
"""
572-
return message % (self.__class__.__name__, self.__VERSION__)
573-
574537

575538
if __name__ == "__main__":
576539
tl_helper = TestLinkHelper()

0 commit comments

Comments
 (0)