-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTestLinkRunner.py
More file actions
266 lines (228 loc) · 9.21 KB
/
TestLinkRunner.py
File metadata and controls
266 lines (228 loc) · 9.21 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import unittest
import sys
import StringIO
import xmlrpclib
TestResult = unittest.TestResult
class _TestResult(TestResult):
def __init__(self,_testLinkURL,_devKey,_testPlanId,_buildName):
TestResult.__init__(self)
self.stdout0 = None
self.stderr0 = None
self.result = []
self.testLinkURL = _testLinkURL
self.devKey = _devKey
self.testPlanId = _testPlanId
self.buildName = _buildName
def startTest(self, test):
TestResult.startTest(self, test)
# just one buffer for both stdout and stderr
self.outputBuffer = StringIO.StringIO()
stdout_redirector.fp = self.outputBuffer
stderr_redirector.fp = self.outputBuffer
self.stdout0 = sys.stdout
self.stderr0 = sys.stderr
sys.stdout = stdout_redirector
sys.stderr = stderr_redirector
def complete_output(self):
"""
Disconnect output redirection and return buffer.
Safe to call multiple times.
"""
if self.stdout0:
sys.stdout = self.stdout0
sys.stderr = self.stderr0
self.stdout0 = None
self.stderr0 = None
return self.outputBuffer.getvalue()
def stopTest(self, test):
# Usually one of addSuccess, addError or addFailure would have been called.
# But there are some path in unittest that would bypass this.
# We must disconnect stdout in stopTest(), which is guaranteed to be called.
self.complete_output()
def addSuccess(self, test):
TestResult.addSuccess(self, test)
output = self.complete_output()
self.result.append((0, test, output, ''))
testNameInString = str(test)
_testCaseApiId = self.getTestCaseId(testNameInString)
_SuccessStatus = 'p'
self.updateTestCaseResult(_testCaseApiId, _SuccessStatus)
def addError(self, test, err):
TestResult.addError(self, test, err)
_, _exc_str = self.errors[-1]
output = self.complete_output()
self.result.append((2, test, output, _exc_str))
testNameInString= str(test)
_testCaseApiId = self.getTestCaseId(testNameInString)
_Errorstatus = 'f'
_stackTrace = str(err)
self.updateTestCaseResult(_testCaseApiId, _Errorstatus, _stackTrace)
def addFailure(self, test, err):
TestResult.addFailure(self, test, err)
_, _exc_str = self.failures[-1]
output = self.complete_output()
self.result.append((1, test, output, _exc_str))
testNameInString= str(test)
_testCaseApiId = self.getTestCaseId(testNameInString)
_Failurestatus = 'f'
_stackTrace = str(err)
self.updateTestCaseResult(_testCaseApiId, _Failurestatus, _stackTrace)
def getTestCaseId(self,fullTestName):
testCaseName = fullTestName.split("(")[0]
splitUntilTestId = testCaseName.split("_")[1]
testCaseApiId = self.getDigits(splitUntilTestId)
return testCaseApiId
def getDigits(self,str):
c = ""
for i in str:
if i.isdigit():
c+=i
return c
def updateTestCaseResult(self,_testCaseApiId,_status,_notes=None):
"""
"""
conn = xmlrpclib.Server(self.testLinkURL)
data = {}
data["devKey"] = self.devKey
data["testcaseid"] = _testCaseApiId
data["testplanid"] = self.testPlanId
data["buildname"] = self.buildName
data["status"] = _status
if _notes == None:
_notes = 'Successful test does not contain notes'
data["notes"] = _notes
updateTestResult = conn.tl.reportTCResult(data)
#------------------------------------------------------------------------
class TestLinkRunner():
def __init__(self,_testLinkURL,_devKey,_testPlanId, _buildName):
self.testLinkURL = _testLinkURL
self.devKey = _devKey
self.testPlanId = _testPlanId
self.buildName = _buildName
def run(self,test):
result = _TestResult(self.testLinkURL,self.devKey,self.testPlanId,self.buildName)
test(result)
#-------------------------------------------------------------------------
class OutputRedirector(object):
""" Wrapper to redirect stdout or stderr """
def __init__(self, fp):
self.fp = fp
def write(self, s):
self.fp.write(s)
def writelines(self, lines):
self.fp.writelines(lines)
def flush(self):
self.fp.flush()
stdout_redirector = OutputRedirector(sys.stdout)
stderr_redirector = OutputRedirector(sys.stderr)
#-- Import section
import unittest, sys, StringIO, xmlrpclib
#-- Global variables section
TestResult = unittest.TestResult
class _TestResult(TestResult):
def __init__(self,_testLinkURL,_apiKey,_testPlanId,_buildName):
TestResult.__init__(self)
self.stdout0 = None
self.stderr0 = None
self.result = []
self.testLinkURL = _testLinkURL
self.devKey = _apiKey
self.testPlanId = _testPlanId
self.buildName = _buildName
def startTest(self, test):
TestResult.startTest(self, test)
# just one buffer for both stdout and stderr
self.outputBuffer = StringIO.StringIO()
stdout_redirector.fp = self.outputBuffer
stderr_redirector.fp = self.outputBuffer
self.stdout0 = sys.stdout
self.stderr0 = sys.stderr
sys.stdout = stdout_redirector
sys.stderr = stderr_redirector
def complete_output(self):
"""
Disconnect output redirection and return buffer.
Safe to call multiple times.
"""
if self.stdout0:
sys.stdout = self.stdout0
sys.stderr = self.stderr0
self.stdout0 = None
self.stderr0 = None
return self.outputBuffer.getvalue()
def stopTest(self, test):
# Usually one of addSuccess, addError or addFailure would have been called.
# But there are some path in unittest that would bypass this.
# We must disconnect stdout in stopTest(), which is guaranteed to be called.
self.complete_output()
def addSuccess(self, test):
TestResult.addSuccess(self, test)
output = self.complete_output()
self.result.append((0, test, output, ''))
testNameInString = str(test)
_testCaseApiId = self.getTestCaseId(testNameInString)
self.updateTestCaseResult(_testCaseApiId, 'p',str(output))
def addError(self, test, err):
TestResult.addError(self, test, err)
_, _exc_str = self.errors[-1]
output = self.complete_output()
self.result.append((2, test, output, _exc_str))
testNameInString= str(test)
_testCaseApiId = self.getTestCaseId(testNameInString)
self.updateTestCaseResult(_testCaseApiId, 'f', str(err))
def addFailure(self, test, err):
TestResult.addFailure(self, test, err)
_, _exc_str = self.failures[-1]
output = self.complete_output()
self.result.append((1, test, output, _exc_str))
testNameInString= str(test)
_testCaseApiId = self.getTestCaseId(testNameInString)
self.updateTestCaseResult(_testCaseApiId, 'f', str(err))
def getTestCaseId(self,fullTestName):
testCaseName = fullTestName.split("(")[0]
splitUntilTestId = testCaseName.split("_")[1]
testCaseApiId = self.getDigits(splitUntilTestId)
return testCaseApiId
def getDigits(self,str):
c = ""
for i in str:
if i.isdigit():
c+=i
return c
def updateTestCaseResult(self,_testCaseApiId,_status,_notes):
conn = xmlrpclib.Server(self.testLinkURL)
data = {}
data["devKey"] = self.devKey
data["testcaseid"] = _testCaseApiId
data["testplanid"] = self.testPlanId
data["buildname"] = self.buildName
data["status"] = _status
data["notes"] = _notes
updateTestResult = conn.tl.reportTCResult(data)
#------------------------------------------------------------------------
#-- gets input from the user and pass it on to the _TestResult class
class TestLinkRunner():
#-- Initializing variables
def __init__(self,_testLinkURL,_devKey,_testPlanId, _buildName):
self.testLinkURL = _testLinkURL
self.devKey = _devKey
self.testPlanId = _testPlanId
self.buildName = _buildName
#-- Calling the _TestResult with the necessary arguments
def run(self,test):
result = _TestResult(self.testLinkURL,self.devKey,self.testPlanId,self.buildName)
test(result)
#-------------------------------------------------------------------------
#-- To redirect the output
class OutputRedirector(object):
""" Wrapper to redirect stdout or stderr """
def __init__(self, fp):
self.fp = fp
def write(self, s):
self.fp.write(s)
def writelines(self, lines):
self.fp.writelines(lines)
def flush(self):
self.fp.flush()
stdout_redirector = OutputRedirector(sys.stdout)
stderr_redirector = OutputRedirector(sys.stderr)