-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestResults.py
169 lines (155 loc) · 6.37 KB
/
TestResults.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
#!/usr/bin/env python
## Program: testDriver
## Module: TestResults.py
## Language: Python
## Date: $Date: 2012/02/10 10:01:27 $
## Version: $Revision: 0.1.6 $
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the above copyright notices for more information.
from xml.etree import ElementTree as etree
import os
class TestResults(object):
'''
TestResults class writes testing results in a .xml file
and in a .txt file.
This class has the following methods:
SetTestingResults: a method for setting results from testRuns class
RetrieveResults: a method for retrieving testResults
WriteTxt: a method for writing results in a .txt file
WriteXml: a method for writing results in a .xml file
'''
def __init__(self):
'''
Constructor
'''
self.testCases = None
self.testPlanName = None
self.testedCases = None
self.testPlanName = None
self.results = {} #test:result
self.mainDir = None
def SetTestingResults(self, testRuns):
'''
Setting testRuns class
'''
self.testCases = testRuns.testCases
self.mainDir = testRuns.mainDir
if testRuns.testPlan:
self.testPlanName = testRuns.testPlan.keys()[0]
self.testedCases = testRuns.testPlan[self.testPlanName]
else:
self.testedCases = testRuns.testingCases[0].id
def RetrieveResults(self):
'''
This method retrieves test results
'''
for c in self.testCases.Cases.itervalues():
for t in self.testedCases:
if c.id == t:
self.results[c] = c.status
def ReadTxt(self, txt_path):
'''
This method read test results txt file for a single
case or a complete test plan.
'''
results = {} #id,status
text_file = open(txt_path, "r")
while True:
text = text_file.readline()
if text.startswith("case"):
row = text.split(':')
caseId = row[0][4:]
result = row[1]
results[caseId]=result
if text == "":
break
text_file.close()
for case_id, case in self.testCases.iteritems():
for c,r in results.iteritems():
if case_id == c:
case.status = r
return results
def WriteTxt(self):
'''
This method writes test results related to a single
test case or a complete test plan into a txt file
'''
test_list =[]
if self.testPlanName:
name = 'plan_%s'% self.testPlanName
else:
name = 'case_%s' % self.testedCases
for c in self.testCases.Cases.itervalues():
test_list.append(c.id)
test_list.sort()
if self.mainDir != 'projects/':
txtpath = self.mainDir+"results/%s.txt" % name
if not os.path.exists (self.mainDir+"results/"):
os.mkdir(self.mainDir+"results/")
else:
txtpath = "projects/%s/results/%s.txt" % (self.testCases.projectName,name)
if not os.path.exists ("projects/%s/" % self.testCases.projectName):
os.mkdir("projects/%s/" % self.testCases.projectName)
text_file = open(txtpath, "w")
text_file.write("Project Id:%s, Name:%s, Version:%s\n" % (self.testCases.projectId, self.testCases.projectName, self.testCases.projectVersion))
text_file.write("TestResults for %s\n" % name)
for case in test_list:
for c in self.testCases.Cases.itervalues():
if c.id == case:
for t in self.testedCases:
if c.id == t:
if self.results[c]:
text_file.write('case%s: ' % c.id + str(c.status))
text_file.write("\n")
text_file.close()
def WriteXml(self):
'''
This method writes test results related to a single
test case or a complete test plan into an xml file
'''
test_list = []
if self.testPlanName:
name = 'plan_%s'% self.testPlanName
else:
name = 'case_%s' % self.testedCases
for c in self.testCases.Cases.itervalues():
test_list.append(c.id)
test_list.sort()
if self.mainDir != 'projects/':
filepath = self.mainDir+"results/%s.txt" % name
if not os.path.exists (self.mainDir+"results/"):
os.mkdir(self.mainDir+"results/")
else:
filepath = "projects/%s/results/%s.txt" % (self.testCases.projectName,name)
if not os.path.exists ("projects/%s/" % self.testCases.projectName):
os.mkdir("projects/%s/" % self.testCases.projectName)
root = etree.Element("Project", id=self.testCases.projectId, name=self.testCases.projectName, version=self.testCases.projectVersion)
results = etree.ElementTree(root)
testResults = etree.SubElement(root,"testResults", id=str(name))
testCases = etree.SubElement(testResults,"testCases")
for case in test_list:
for c in self.testCases.Cases.itervalues():
if c.id == case:
for t in self.testedCases:
if c.id == t:
if self.results[c]:
testCase = etree.SubElement(testCases,"testCase", id=str(c.id),sd=str(c.sd),type=str(c.type))
result = etree.SubElement(testCase,"result")
result.text = str(c.status)
indent(root)
results.write (filepath, encoding='iso-8859-1')
def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i