Skip to content

Commit 0762c60

Browse files
author
Patrick Dassier
committed
prepare library
1 parent f970bd7 commit 0762c60

File tree

2 files changed

+147
-32
lines changed

2 files changed

+147
-32
lines changed

test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
'''
4+
Source file
5+
File name: test.py
6+
Creation date: 04-10-2012
7+
Author: dassier
8+
9+
'''
10+
11+
'''
12+
Fichier de test pour le module "TestLinkAPI.py"
13+
'''
14+
15+
import re
16+
import TestLinkAPI
17+
from nose.tools import *
18+
19+
class TestClass():
20+
def setUp(self):
21+
"""Initialisation
22+
"""
23+
24+
SERVEUR_URL = "http://localhost/testlink/lib/api/xmlrpc.php"
25+
KEY = "7ec252ab966ce88fd92c25d08635672b"
26+
self.client = TestLinkAPI.TestLinkAPIClient(server_url=SERVEUR_URL, devKey=KEY)
27+
28+
def test_getTestCaseIDByName(self):
29+
""" getTestCaseIDByName test
30+
"""
31+
(val, message) = self.client.getTestCaseIDByName("Fin de programme", "Séquence 2", "Test 2")
32+
assert_equal(message, None )
33+
(val, message) = self.client.getTestCaseIDByName("Initialisation", "Séquence 1", "Test 2")
34+
assert_equal(message, "(getTestCaseIDByName) - Several case test found. Suite name must not be duplicate for the same project")
35+
36+
def test___str__(self):
37+
""" __str__ test
38+
Check that return is a string that contains the version number
39+
"""
40+
41+
message = self.client.__str__()
42+
assert_not_equal(re.search(self.client.__VERSION__, message), None)
43+
44+

TestLinkAPI.py renamed to testlink.py

Lines changed: 103 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
@author: kereval.com
77
Initialy based on the James Stock testlink-api-python-client R7.
88
9-
Updated by Kereval to support testCase Reporting and File attachment to test execution
10-
9+
Update by pade to provide a user friendly library, with more robustness and error management
1110
"""
1211
import xmlrpclib
12+
import sys
13+
14+
class TestLinkAPIClient:
15+
16+
__VERSION__ = "0.3"
1317

14-
class TestlinkAPIClient:
15-
1618
def __init__(self, server_url, devKey):
1719
self.server = xmlrpclib.Server(server_url)
1820
self.devKey = devKey
@@ -139,14 +141,52 @@ def getTestCaseCustomFieldDesignValue(self, testcaseexternalid, version,
139141
'details' : str(details)}
140142
return self.server.tl.getTestCaseCustomFieldDesignValue(argsAPI)
141143

142-
def getTestCaseIDByName(self, testCaseName):
143-
""" getTestCaseIDByName :
144-
Find a test case by its name
145-
"""
144+
def __getTestCaseIDByName(self, testCaseName, testSuiteName=None, testProjectName=None):
145+
""" __getTestCaseIDByName :
146+
Internal function
147+
Find a test case by its name
148+
testSuiteName and testProjectName are optionals arguments
149+
This function return a list of tests cases
150+
"""
146151
argsAPI = {'devKey' : self.devKey,
147152
'testcasename':str(testCaseName)}
148-
return self.server.tl.getTestCaseIDByName(argsAPI)
153+
154+
if testSuiteName is not None:
155+
argsAPI.update({'testsuitename':str(testSuiteName)})
156+
157+
if testProjectName is not None:
158+
argsAPI.update({'testprojectname':str(testProjectName)})
159+
160+
# Server return can be a list or a dictionnary !
161+
# This function always return a list
162+
ret_srv = self.server.tl.getTestCaseIDByName(argsAPI)
163+
if type(ret_srv) == dict:
164+
retval = []
165+
for value in ret_srv.values():
166+
retval.append(value)
167+
return retval
168+
else:
169+
return ret_srv
170+
171+
172+
def getTestCaseIDByName(self, testCaseName, testSuiteName, testProjectName):
173+
""" getTestCaseIDByName :
174+
Find a test case by its name, by its suite and its project
175+
Suite name must not be duplicate, so only one test case must be found
176+
Return (id, None) if success
177+
or (-1, "error message") in case of error
178+
"""
179+
results = self.__getTestCaseIDByName(testCaseName, testSuiteName, testProjectName)
180+
if results[0].has_key("message"):
181+
return (-1, results[0]["message"])
182+
elif len(results) > 1:
183+
return (-1, "(getTestCaseIDByName) - Several case test found. Suite name must not be duplicate for the same project")
184+
else:
185+
if results[0]["name"] == testCaseName:
186+
return (results[0]["id"], None)
187+
return (-1, "(getTestCaseIDByName) - Internal server error. Return value is not expected one!")
149188

189+
150190
def getTestCasesForTestPlan(self, *args):
151191
""" getTestCasesForTestPlan :
152192
List test cases linked to a test plan
@@ -329,12 +369,12 @@ def createTestCase(self, *args):
329369
self.stepsList = []
330370
return ret
331371

332-
def reportTCResult(self, testcaseid, testplanid, buildid, status, notes ):
372+
def __reportTCResult(self, testcaseid, testplanid, buildname, status, notes ):
333373
"""
334374
Report execution result
335375
testcaseid: internal testlink id of the test case
336376
testplanid: testplan associated with the test case
337-
buildid: build version of the test case
377+
buildname: build name of the test case
338378
status: test verdict ('p': pass,'f': fail,'b': blocked)
339379
340380
Return : [{'status': True, 'operation': 'reportTCResult', 'message': 'Success!', 'overwrite': False, 'id': '37'}]
@@ -343,11 +383,31 @@ def reportTCResult(self, testcaseid, testplanid, buildid, status, notes ):
343383
argsAPI = {'devKey' : self.devKey,
344384
'testcaseid' : testcaseid,
345385
'testplanid' : testplanid,
346-
'buildid':buildid,
386+
'buildname':buildname,
347387
'status': status,
348388
'notes' : notes
349389
}
350390
return self.server.tl.reportTCResult(argsAPI)
391+
392+
def reportResult(self, testCaseName, testSuiteName, testProjectName):
393+
pass
394+
395+
def setReportCtx(self, testProjectName, testPlanName, buildName):
396+
"""
397+
Set project name test plan name and build name for reportResult function
398+
"""
399+
project = getTestProjectByName(testProjectName)
400+
if project[0].has_key("message"):
401+
sys.exit(project[0]["message"])
402+
403+
# Check if project is active
404+
if project[0]['active'] != '1':
405+
sys.exit("(getTestProjectByName) - Test project (name:%s) is not active" % testProjectName)
406+
407+
# Store project id
408+
self.projectId = project[0]['id']
409+
#TODO: à finir
410+
351411

352412
def uploadExecutionAttachment(self,attachmentfile,executionid,title,description):
353413
"""
@@ -486,35 +546,45 @@ def initStep(self, actions, expected_results, execution_type):
486546
Initializes the list which stores the Steps of a Test Case to create
487547
"""
488548
self.stepsList = []
489-
list = {}
490-
list['step_number'] = '1'
491-
list['actions'] = actions
492-
list['expected_results'] = expected_results
493-
list['execution_type'] = str(execution_type)
494-
self.stepsList.append(list)
549+
lst = {}
550+
lst['step_number'] = '1'
551+
lst['actions'] = actions
552+
lst['expected_results'] = expected_results
553+
lst['execution_type'] = str(execution_type)
554+
self.stepsList.append(lst)
495555
return True
496556

497557
def appendStep(self, actions, expected_results, execution_type):
498558
""" appendStep :
499559
Appends a step to the steps list
500560
"""
501-
list = {}
502-
list['step_number'] = str(len(self.stepsList)+1)
503-
list['actions'] = actions
504-
list['expected_results'] = expected_results
505-
list['execution_type'] = str(execution_type)
506-
self.stepsList.append(list)
561+
lst = {}
562+
lst['step_number'] = str(len(self.stepsList)+1)
563+
lst['actions'] = actions
564+
lst['expected_results'] = expected_results
565+
lst['execution_type'] = str(execution_type)
566+
self.stepsList.append(lst)
507567
return True
508568

509569
def getProjectIDByName(self, projectName):
510-
projects=self.server.tl.getProjects({'devKey' : self.devKey})
511-
for project in projects:
512-
if (project['name'] == projectName):
513-
result = project['id']
514-
else:
515-
result = -1
516-
return result
517-
570+
projects=self.server.tl.getProjects({'devKey' : self.devKey})
571+
for project in projects:
572+
if (project['name'] == projectName):
573+
result = project['id']
574+
else:
575+
result = -1
576+
return result
577+
578+
def __str__(self):
579+
message = """
580+
TestLinkAPIClient - version %s
581+
@author: Olivier Renault ([email protected])
582+
@author: kereval.com
583+
@author: Patrick Dassier
584+
585+
"""
586+
return message % self.__VERSION__
587+
518588
if __name__ == "__main__":
519589
myTestLinkServer = "http://YOURSERVER/testlink/lib/api/xmlrpc.php" #change
520590
myDevKey = "" # Put here your devKey
@@ -530,3 +600,4 @@ def getProjectIDByName(self, projectName):
530600
print ""
531601
else:
532602
print "Incorrect DevKey."
603+

0 commit comments

Comments
 (0)