-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversionUtility
149 lines (112 loc) · 5.79 KB
/
conversionUtility
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
import sys
import json;
class ConverterUtility:
def __init__(self, filePath):
self.filePath = filePath;
jsonFile = open(self.filePath)
self.jsonData = json.load(jsonFile)
def sanitize(self, input):
input = input.replace("&", "&").replace("\"", """)
input = input.replace("<", "<").replace(">", ">")
return input;
def getSuiteInfo(self):
testSuiteDataDictionary = {};
totalExecutionTime = 0;
failedTestCasesCount = 0;
totalTestCases = len(self.jsonData['results']);
for testCaseCounter in range(0, totalTestCases):
testCaseInfo = self.getTestCasesInfo(testCaseCounter);
## Total Execution Time
executionTime = testCaseInfo['executionTime']
totalExecutionTime += executionTime;
if testCaseInfo['testCaseFailureStatus']:
failedTestCasesCount = failedTestCasesCount + 1;
testSuiteDataDictionary["totalTestCases"] = totalTestCases;
testSuiteDataDictionary["totalExecutionTime"] = totalExecutionTime;
testSuiteDataDictionary['failedTestCasesCount'] = failedTestCasesCount;
return testSuiteDataDictionary;
# GET Test cases information and return Dictionary
def getTestCasesInfo(self, testCaseIndex):
testCaseDatDictionary = {};
jsonResultData = self.jsonData['results'][testCaseIndex];
nodeData = jsonResultData['node'];
executionTime = jsonResultData['execution_time'];
# ASSUMPTION - "fail" parameter with either "false" or (fail==null and error==null and warn==null) considered as FAILED
if (jsonResultData['fail'] == False) or (jsonResultData['fail'] == None and jsonResultData['error'] == None and jsonResultData['warn'] == None):
testCaseFailureStatus = False;
else:
testCaseFailureStatus = True;
# Getting Error Message - if Error Message is null, adding empty string in XML Error Tag
if jsonResultData['error'] != None:
testCaseErrorMessage = self.sanitize(jsonResultData['error']);
else:
testCaseErrorMessage = "Test Failed.";
## Getting Type Of Failure - If No condition matching then TYPE = ERROR
if jsonResultData['fail']:
testCaseFailureType = 'FAILED';
elif jsonResultData['warn']:
testCaseFailureType = 'WARNING';
elif jsonResultData['skip']:
testCaseFailureType = 'SKIPPED';
else:
testCaseFailureType = 'ERROR';
## UPDATING THE DICTIONARY
testCaseDatDictionary["executionTime"] = executionTime;
testCaseDatDictionary['testCaseFailureStatus'] = testCaseFailureStatus;
testCaseDatDictionary['testCaseErrorMessage'] = testCaseErrorMessage;
testCaseDatDictionary['testCaseClassName'] = nodeData['package_name'];
testCaseDatDictionary['testCaseName'] = nodeData['name'];
testCaseDatDictionary['testCaseFailureStatus'] = testCaseFailureStatus;
testCaseDatDictionary['testCaseFailureType'] = testCaseFailureType;
return testCaseDatDictionary;
def generateXMlFile(self):
#Constant and Variable declaration
XMLHEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
testSuitesTag = "";
TESTSUITES_STARTTAG = "<testsuites"
TESTSUITES_ENDTAG = "</testsuites>"
testSuiteTag = "";
TESTSUITE_STARTTAG = "\n" + "<testsuite"
TESTSUITE_ENDTAG = "</testsuite>"
testCaseTag = ""
TESTCASE_STARTTAG = "\n" + "<testcase"
TESTCASE_ENDTAG = "</testcase>"
FAILURE_STARTTAG = "\n" + "<failure"
FAILURE_ENDTAG = "</failure>"
#Initialization of Test Suites Tag and Test Suite Tag
testSuitesTag += TESTSUITES_STARTTAG;
testSuitesTag += ">"
TEST_SUITE_INFO = self.getSuiteInfo(); # Getting Suite Level Info
totalTestCasesInSuite = TEST_SUITE_INFO['totalTestCases'];
testSuiteTag += TESTSUITE_STARTTAG;
testSuiteTag += " tests=" + "\"" + str(totalTestCasesInSuite) + "\"";
testSuiteTag += " failures=" + "\"" + str(TEST_SUITE_INFO['failedTestCasesCount']) + "\"";
testSuiteTag += " time=" + "\"" + str(TEST_SUITE_INFO['totalExecutionTime']) + "\"" + ">";
#Loop Counter to iterate over all Test Cases of a Suite
for testcaseCounter in range(0, totalTestCasesInSuite):
TEST_CASE_INFO = self.getTestCasesInfo(testcaseCounter); # Getting Test Case Level Info
testCaseTag += TESTCASE_STARTTAG
testCaseTag += " name=" + "\"" + TEST_CASE_INFO['testCaseName'] + "\"";
testCaseTag += " class=" + "\"" + TEST_CASE_INFO['testCaseClassName'] + "\"";
testCaseTag += " time=" + "\"" + str(TEST_CASE_INFO['executionTime']) + "\"" + ">";
#Check for Failed Test Case
if TEST_CASE_INFO['testCaseFailureStatus']:
testCaseTag += FAILURE_STARTTAG;
if TEST_CASE_INFO['testCaseErrorMessage']!=" ":
testCaseTag += " message=" + "\"" + TEST_CASE_INFO['testCaseErrorMessage'] + "\"";
testCaseTag += " type=" + "\"" + TEST_CASE_INFO['testCaseFailureType'] + "\"";
testCaseTag += ">" + FAILURE_ENDTAG + "\n";
testCaseTag += TESTCASE_ENDTAG + "\n";
testSuiteTag += testCaseTag;
testSuiteTag += TESTSUITE_ENDTAG + "\n";
testSuitesTag += testSuiteTag;
testSuitesTag += TESTSUITES_ENDTAG + "\n";
return XMLHEADER + testSuitesTag;
#Calling Utility
object = ConverterUtility(sys.argv[1]);
convertedData = object.generateXMlFile()
xmlFile = open(sys.argv[2], 'w');
xmlFile.write(convertedData)
xmlFile.close();
# Commands To Run -
# utility.py "<Input JSOn File Location>" "<Output File Location>"