-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjtl_junit_converter.py
179 lines (148 loc) · 5.59 KB
/
jtl_junit_converter.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
import sys, csv
from datetime import datetime
from xml.etree import ElementTree
from xml.dom import minidom
from xml.etree.ElementTree import Element, SubElement, Comment
# jtl table columns
TIMESTAMP = 0
ELAPSED = 1
LABEL = 2
RESPONSE_MESSAGE = 4
SUCCESS = 7
def prettify(elem):
"""Returns a pretty-printed XML string for the Element.
"""
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
def retrieve_jmeter_results(jmeter_file):
"""Returns a list of JMeter JTL rows without header.
The JMeter JTL file must be in CSV format.
"""
csv_reader = csv.reader(jmeter_file)
next(csv_reader)
return list(csv_reader)
def create_request_attrib(jmeter_result):
"""Returns a JSON with attributes for a JUnit testsuite: https://llg.cubic.org/docs/junit/
The JMeter JTL file must be in CSV format.
"""
request_time = float(jmeter_result[ELAPSED])/1000.0
return {
'name': jmeter_result[LABEL],
'time': str(request_time),
'error_message': jmeter_result[RESPONSE_MESSAGE]
}
def create_test_case_attrib(request_attrib):
"""Returns a JSON with attributes for a JUnit testcase: https://llg.cubic.org/docs/junit/
The JMeter JTL file must be in CSV format.
"""
return {
'classname': 'httpSample',
'name': request_attrib['name'],
'time': request_attrib['time']
}
def create_test_suite_attrib(junit_results):
"""Returns a JSON with attributes for JUnit testsuite: https://llg.cubic.org/docs/junit/
The JMeter JTL file must be in CSV format.
"""
return {
'id': '1',
'name': 'load test',
'package': 'load test',
'hostname': 'Azure DevOps',
'time': str(junit_results['time']),
'tests': str(junit_results['tests']),
'failures': str(len(junit_results['requests']['failures'])),
'errors': '0'
}
def create_error_test_case_attrib(error_message):
"""Returns a JSON with attributes for JUnit testcase for failed requests: https://llg.cubic.org/docs/junit/
The JMeter JTL file must be in CSV format.
"""
return {
'message': error_message,
'type': 'exception'
}
def requests(jmeter_results):
"""Returns a JSON with successful and failed HTTP requests.
The JMeter JTL file must be in CSV format.
"""
failed_requests = []
successful_requests = []
for result in jmeter_results:
request_attrib = create_request_attrib(result)
if result[SUCCESS] == 'true':
successful_requests.append(request_attrib)
else:
failed_requests.append(request_attrib)
return {
'success': successful_requests,
'failures': failed_requests
}
def total_time_seconds(jmeter_results):
"""Returns the total test duration in seconds.
The JMeter JTL file must be in CSV format.
"""
max_timestamp = max(jmeter_results, key=lambda result: int(result[TIMESTAMP]))
min_timestamp = min(jmeter_results, key=lambda result: int(result[TIMESTAMP]))
total_timestamp = int(max_timestamp[TIMESTAMP]) - int(min_timestamp[TIMESTAMP])
return float(total_timestamp)/1000.0
def create_junit_results(jtl_results_filename):
with open(jtl_results_filename) as jmeter_file:
jmeter_results = retrieve_jmeter_results(jmeter_file)
time = total_time_seconds(jmeter_results)
return {
'tests': len(jmeter_results),
'time': time,
'requests': requests(jmeter_results),
}
def create_properties(test_suite):
"""Creates a JUnit properties element for testsuite: https://llg.cubic.org/docs/junit/
The JMeter JTL file must be in CSV format.
"""
return SubElement(test_suite, 'properties')
def create_test_suite(test_suites, junit_results):
"""Creates a JUnit testsuite: https://llg.cubic.org/docs/junit/
The JMeter JTL file must be in CSV format.
"""
test_suite_attrib = create_test_suite_attrib(junit_results)
test_suite = SubElement(test_suites, 'testsuite', test_suite_attrib)
create_properties(test_suite)
failed_requests = len(junit_results['requests']['failures'])
successful_requests = len(junit_results['requests']['success'])
for success_index in range(successful_requests):
successful_request = junit_results['requests']['success'][success_index]
create_successful_test_case(test_suite, successful_request)
for error_index in range(failed_requests):
failed_request = junit_results['requests']['failures'][error_index]
create_failed_test_case(test_suite, failed_request)
def create_failed_test_case(test_suite, failed_request):
"""Creates a JUnit test case for failed HTTP requests: https://llg.cubic.org/docs/junit/
The JMeter JTL file must be in CSV format.
"""
test_case_attrib = create_test_case_attrib(failed_request)
error_test_case_attrib = create_error_test_case_attrib(failed_request['error_message'])
test_case = SubElement(test_suite, 'testcase', test_case_attrib)
test_case_error = SubElement(test_case, 'error', error_test_case_attrib)
def create_successful_test_case(test_suite, successful_request):
"""Creates a JUnit test case for successful HTTP requests: https://llg.cubic.org/docs/junit/
The JMeter JTL file must be in CSV format.
"""
test_case_attrib = create_test_case_attrib(successful_request)
test_case = SubElement(test_suite, 'testcase', test_case_attrib)
def create_test_suites(jtl_results_filename):
"""Creates a JUnit testsuites element: https://llg.cubic.org/docs/junit/
The JMeter JTL file must be in CSV format.
"""
test_suites = Element('testsuites')
junit_results = create_junit_results(jtl_results_filename)
create_test_suite(test_suites, junit_results)
return prettify(test_suites)
def main():
print('Converting...')
with open(sys.argv[2], "w") as output_file:
test_suites = create_test_suites(sys.argv[1])
output_file.write(test_suites)
print('Done!')
if __name__ == '__main__':
main()