-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdater.py
214 lines (187 loc) · 9.15 KB
/
updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding:utf-8 -*-
# !/usr/bin/python
import os
import sys
import pprint
import testlink
import xmlrpclib
import argparse
from time import strftime
from xml.etree.ElementTree import ElementTree
from robot.api import ExecutionResult, ResultVisitor
class RobotI:
def __init__(self):
#os.environ['TESTLINK_API_PYTHON_SERVER_URL'] = "{Your testlink url}/lib/api/xmlrpc/v1/xmlrpc.php"
#os.environ['TESTLINK_API_PYTHON_DEVKEY'] = "{Your testlink dev api key}"
tl_helper = testlink.TestLinkHelper()
self.testlinker = tl_helper.connect(testlink.TestlinkAPIClient)
def getTestPlanPlatforms(self, testplan_id):
return self.testlinker.getTestPlanPlatforms(testplan_id)
def getTestPlanPlatformsIDByName(self, testplan_id, platform_name):
try:
PLlist = self.testlinker.getTestPlanPlatforms(testplan_id)
PLid = ""
for i in PLlist:
if platform_name.lower() in i['name'].lower():
PLid = i['id']
break
return PLid
except IOError as e:
errno, strerror = e.args
print "getTestPlanPlatformsIDByName I/O error({0}): {1}".format(errno, strerror)
except testlink.testlinkerrors.TLResponseError as e:
print "TestLink error({0}): {1}".format(e.code, e.message)
return False
except testlink.testlinkerrors.TLConnectionError as e:
print "TestLink error:({0})".format(e.message)
return False
except:
print "Unexpected error:", sys.exc_info()[0]
raise
def getTestProjectIDByName(self, project_name):
return self.testlinker.getTestProjectByName(project_name)['id']
def getTestCaseIDByName(self, testcase_name, project_name):
return self.testlinker.getTestCaseIDByName(testcase_name ,testprojectname=project_name)[0]['id']
def getTestProjectPrefixByName(self, project_name):
return self.testlinker.getTestProjectByName(project_name)['prefix']
def getTestCasesForTestPlan(self, testplain_id):
return self.testlinker.getTestCasesForTestPlan(testplain_id)
def getTestPlanIDByName(self, project_name, test_plan_name):
try:
tp = self.testlinker.getTestPlanByName(project_name, test_plan_name)
return tp[0]['id']
except IOError as e:
errno, strerror = e.args
print "getTestPlanIDByName I/O error({0}): {1}".format(errno, strerror)
except testlink.testlinkerrors.TLResponseError as e:
print "TestLink error({0}): {1}".format(e.code, e.message)
return False
except testlink.testlinkerrors.TLConnectionError as e:
print "TestLink error:({0})".format(e.message)
return False
except:
print "Unexpected error:", sys.exc_info()[0]
raise
def reportTCResult(self, testcase_id, testplan_id, build_id, result, platform_id, testcase_id_result_dict):
self.testlinker.reportTCResult(testcase_id, testplan_id, build_id, result, platform_id, platformid=platform_id)
def createBuild(self, testplanid, build_name, buildnotes):
try:
newbuildID = self.testlinker.createBuild(testplanid, build_name, buildnotes)
if 'exist' in newbuildID[0]['message']:
return newbuildID[0]['message'].split(":")[1].replace(")", "")
else:
return self.existingBuild(testplanid)
except IOError as e:
errno, strerror = e.args
print "createBuild I/O error({0}): {1}".format(errno, strerror)
except testlink.testlinkerrors.TLResponseError as e:
print "TestLink error({0}): {1}".format(e.code, e.message)
return False
except testlink.testlinkerrors.TLConnectionError as e:
print "TestLink error:({0})".format(e.message)
return False
except:
print "Unexpected error:", sys.exc_info()[0]
raise
def getInternalTestcaseID(self, externalTestCaseID):
try:
tc_info = self.testlinker.getTestCase(testcaseexternalid=externalTestCaseID)
return tc_info[0]['testcase_id']
except IOError as e:
errno, strerror = e.args
print "getInternalTestcaseID I/O error({0}): {1}".format(errno, strerror)
except testlink.testlinkerrors.TLResponseError as e:
print "TestLink error({0}): {1}".format(e.code, e.message)
return False
except testlink.testlinkerrors.TLConnectionError as e:
print "TestLink error:({0})".format(e.message)
return False
except:
print "Unexpected error:", sys.exc_info()[0]
raise
def existingBuild(self, testplanid):
try:
existingBuild = self.testlinker.getLatestBuildForTestPlan(testplanid)
return existingBuild['id']
except IOError as e:
errno, strerror = e.args
print "existingBuild I/O error({0}): {1}".format(errno, strerror)
except testlink.testlinkerrors.TLResponseError as e:
print "TestLink error({0}): {1}".format(e.code, e.message)
return False
except testlink.testlinkerrors.TLConnectionError as e:
print "TestLink error:({0})".format(e.message)
return False
except:
print "Unexpected error:", sys.exc_info()[0]
raise
class PrintTestInfo(ResultVisitor):
def visit_test(self, test):
if test.status == "PASS":
testcase_id_result_dict['p'].append([test.name, test.status])
else:
testcase_id_result_dict['f'].append([test.name, test.status])
class TestlinkFeeder:
def __init__(self, project_name, testplan_name, platform_name, build_name):
self.robotI = RobotI()
self.project_name = project_name
self.TPid = self.robotI.getTestPlanIDByName(project_name, testplan_name)
self.PLid = self.robotI.getTestPlanPlatformsIDByName(self.TPid, platform_name)
self.Bid = self.robotI.createBuild(self.TPid, build_name, '')
self.build_name = build_name
global testcase_id_result_dict
testcase_id_result_dict = {
"f": [],
"p": []
}
def parseRFreport(self, RFreport):
try:
project_prefix_name = self.robotI.getTestProjectPrefixByName(self.project_name)
tcIntID = ""
result = ExecutionResult(RFreport)
result.visit(PrintTestInfo())
pass_index = 0
fail_index = 0
for name, result in testcase_id_result_dict['p']:
tcIntID = self.robotI.getTestCaseIDByName(name, self.project_name)
testcase_id_result_dict['p'][pass_index][1] = tcIntID
pass_index += 1
for name, result in testcase_id_result_dict['f']:
tcIntID = self.robotI.getTestCaseIDByName(name, self.project_name)
testcase_id_result_dict['f'][fail_index][1] = tcIntID
fail_index += 1
except IOError as e:
errno, strerror = e.args
print "parseRFreport I/O error({0}): {1}".format(errno, strerror)
except testlink.testlinkerrors.TLResponseError as e:
print "TestLink error({0}): {1}".format(e.code, e.message)
return False
except testlink.testlinkerrors.TLConnectionError as e:
print "TestLink error:({0})".format(e.message)
return False
except:
print "Unexpected error:", sys.exc_info()[0]
raise
def reportTCResult(self):
for name, pass_id in testcase_id_result_dict["p"]:
self.robotI.testlinker.reportTCResult(pass_id, self.TPid, self.build_name, "p", '')
for name, pass_id in testcase_id_result_dict["f"]:
self.robotI.testlinker.reportTCResult(pass_id, self.TPid, self.build_name, "f", '')
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog='testlinker.py', usage='%(prog)s', description='Auto update testlink test cases status from RF report')
parser.add_argument('-p', '--project', required=True, help='Project name from testlink. Example: "test 123"')
parser.add_argument('-t', '--testplan', required=True, help='Test plan name from testlink. Example: "Automation"')
parser.add_argument('-f', '--platform', required=True, help='Platform name from testlink. Example: "chrome"')
parser.add_argument('-b', '--build', required=True, help='build name for testlink. Example: "1.1.1.0001"')
parser.add_argument('--file', required=True, help='Robotframework output.xml file for test case result. Example: "output.xml"')
args = parser.parse_args()
testlink_feeder = TestlinkFeeder(args.project, args.testplan, args.platform, args.build)
testlink_feeder.parseRFreport(args.file)
testlink_feeder.reportTCResult()
print('\nSuccessful update result: \nTestlink: ' + os.environ['TESTLINK_API_PYTHON_SERVER_URL'] + '\nReport: ' + args.file)
print("====PASS====")
for name, result in testcase_id_result_dict['p']:
print(name)
print("====FAIL====")
for name, result in testcase_id_result_dict['f']:
print(name)