Skip to content

Commit 9713fcc

Browse files
committed
Add new TestLink class, with error management
1 parent fefb234 commit 9713fcc

File tree

2 files changed

+48
-52
lines changed

2 files changed

+48
-52
lines changed

test.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
'''
1414

1515
import re
16-
import TestLinkAPI
16+
from testlink import TestLink, TestLinkErrors
1717
from nose.tools import *
1818

1919
class TestClass():
@@ -23,22 +23,20 @@ def setUp(self):
2323

2424
SERVEUR_URL = "http://localhost/testlink/lib/api/xmlrpc.php"
2525
KEY = "7ec252ab966ce88fd92c25d08635672b"
26-
self.client = TestLinkAPI.TestLinkAPIClient(server_url=SERVEUR_URL, devKey=KEY)
26+
self.client = TestLink(server_url=SERVEUR_URL, key=KEY)
2727

2828
def test_getTestCaseIDByName(self):
2929
""" getTestCaseIDByName test
3030
"""
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-
31+
val = self.client.getTestCaseIDByName("Fin de programme", "Séquence 2", "Test 2")
32+
# 31 is test case id
33+
assert_equal(val, '31' )
34+
35+
try:
36+
val = self.client.getTestCaseIDByName("Initialisation", "Séquence 1", "Test 2")
37+
except TestLinkErrors, e:
38+
assert_equal(str(e), "(getTestCaseIDByName) - Several case test found. Suite name must not be duplicate for the same project")
39+
else:
40+
print "An error message is expected !"
41+
assert_equal(False, True)
4442

testlink.py

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import xmlrpclib
1313
import sys
1414

15-
class Errors(Exception):
15+
class TestLinkErrors(Exception):
1616
""" Basic error handler
1717
Return message pass as argument
1818
"""
@@ -22,9 +22,8 @@ def __init__(self, msg):
2222
def __str__(self):
2323
return self.__msg
2424

25-
class TestLinkAPIClient:
25+
class TestLinkAPIClient(object):
2626

27-
__VERSION__ = "0.3"
2827

2928
def __init__(self, server_url, devKey):
3029
self.server = xmlrpclib.Server(server_url)
@@ -152,9 +151,8 @@ def getTestCaseCustomFieldDesignValue(self, testcaseexternalid, version,
152151
'details' : str(details)}
153152
return self.server.tl.getTestCaseCustomFieldDesignValue(argsAPI)
154153

155-
def __getTestCaseIDByName(self, testCaseName, testSuiteName=None, testProjectName=None):
156-
""" __getTestCaseIDByName :
157-
Internal function
154+
def getTestCaseIDByName(self, testCaseName, testSuiteName=None, testProjectName=None):
155+
"""
158156
Find a test case by its name
159157
testSuiteName and testProjectName are optionals arguments
160158
This function return a list of tests cases
@@ -180,22 +178,6 @@ def __getTestCaseIDByName(self, testCaseName, testSuiteName=None, testProjectNam
180178
return ret_srv
181179

182180

183-
def getTestCaseIDByName(self, testCaseName, testSuiteName, testProjectName):
184-
""" getTestCaseIDByName :
185-
Find a test case by its name, by its suite and its project
186-
Suite name must not be duplicate, so only one test case must be found
187-
Return (id, None) if success
188-
or (-1, "error message") in case of error
189-
"""
190-
results = self.__getTestCaseIDByName(testCaseName, testSuiteName, testProjectName)
191-
if results[0].has_key("message"):
192-
return (-1, results[0]["message"])
193-
elif len(results) > 1:
194-
return (-1, "(getTestCaseIDByName) - Several case test found. Suite name must not be duplicate for the same project")
195-
else:
196-
if results[0]["name"] == testCaseName:
197-
return (results[0]["id"], None)
198-
return (-1, "(getTestCaseIDByName) - Internal server error. Return value is not expected one!")
199181

200182

201183
def getTestCasesForTestPlan(self, *args):
@@ -596,19 +578,35 @@ def __str__(self):
596578
"""
597579
return message % self.__VERSION__
598580

599-
if __name__ == "__main__":
600-
myTestLinkServer = "http://YOURSERVER/testlink/lib/api/xmlrpc.php" #change
601-
myDevKey = "" # Put here your devKey
602-
myTestLink = TestlinkAPIClient(myTestLinkServer, myDevKey)
603-
print "TestLinkAPIClient - v0.2"
604-
print "@author: Olivier Renault ([email protected])"
605-
print ""
606-
if myTestLink.checkDevKey() == True:
607-
methodList = [method for method in TestlinkAPIClient.__dict__]
608-
for method in methodList:
609-
if method[0:2] != "__":
610-
print method
611-
print ""
612-
else:
613-
print "Incorrect DevKey."
581+
class TestLink(TestLinkAPIClient):
582+
"""
583+
TestLink API library
584+
"""
585+
586+
__VERSION__ = "0.1"
587+
588+
def __init__(self, server_url, key):
589+
"""
590+
Class initialisation
591+
"""
592+
593+
super(testlink, self).__init__(server_url, key)
594+
595+
def getTestCaseIDByName(self, testCaseName, testSuiteName, testProjectName):
596+
"""
597+
Find a test case by its name, by its suite and its project
598+
Suite name must not be duplicate, so only one test case must be found
599+
Return test case id if success
600+
or raise TestLinkErrors exception with error message in case of error
601+
"""
602+
results = super(testlink, self).getTestCaseIDByName(testCaseName, testSuiteName, testProjectName)
603+
if results[0].has_key("message"):
604+
raise TestLinkErrors(results[0]["message"])
605+
elif len(results) > 1:
606+
raise TestLinkErrors("(getTestCaseIDByName) - Several case test found. Suite name must not be duplicate for the same project")
607+
else:
608+
if results[0]["name"] == testCaseName:
609+
return results[0]["id"]
610+
raise TestLinkErrors("(getTestCaseIDByName) - Internal server error. Return value is not expected one!")
611+
614612

0 commit comments

Comments
 (0)