Skip to content

Commit 3d480f6

Browse files
authored
Code cleanup (#16)
* use string format, add product version * add missing set to TestSuite
1 parent 6f3fa39 commit 3d480f6

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

python_testspace_xml/testspace_xml.py

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import base64
33
import gzip
44
import os
5+
import os.path
56
import io
67
from io import BytesIO
7-
import os.path
88
import sys
99
from xml.dom.minidom import parseString
1010

@@ -49,32 +49,32 @@ def set_file_annotation(self, file_path=None, mime_type='text/plain', string_buf
4949
if file_path is not None:
5050
if not os.path.isfile(self.file_path):
5151
self.level = 'error'
52-
self.description = 'File: ' + self.file_path + ' not found.'
52+
self.description = 'File: {0} not found'.format(self.file_path)
5353
self.file_path = None
5454
else:
55-
with io.open(self.file_path, 'rb') as inFile:
55+
with io.open(self.file_path, 'rb') as in_file:
5656
out = BytesIO()
57-
with gzip.GzipFile(fileobj=out, mode="wb") as f:
58-
f.writelines(inFile)
59-
f.close()
57+
with gzip.GzipFile(fileobj=out, mode='wb') as out_fileobj:
58+
out_fileobj.writelines(in_file)
59+
out_fileobj.close()
6060
self.gzip_data = out.getvalue()
6161
elif string_buffer is not None:
6262
byte_string_buffer = string_buffer.encode()
6363
out = BytesIO()
64-
with gzip.GzipFile(fileobj=out, mode="wb") as f:
65-
f.write(byte_string_buffer)
66-
f.close()
64+
with gzip.GzipFile(fileobj=out, mode='wb') as out_fileobj:
65+
out_fileobj.write(byte_string_buffer)
66+
out_fileobj.close()
6767
self.gzip_data = out.getvalue()
6868

6969
def set_link_annotation(self, path=None):
7070
self.link_file = True
7171
if path.startswith(r'\\'):
72-
self.file_path = "file://" + path.replace('\\', '/')
73-
elif path.startswith(r'https') or path.startswith(r'http://'):
72+
self.file_path = 'file:// {0}'.format(path.replace('\\', '/'))
73+
elif path.startswith('https') or path.startswith('http://'):
7474
self.file_path = path
7575
else:
7676
self.level = 'error'
77-
self.description = 'Invalid path given:' + path
77+
self.description = 'Invalid path given: {0}'.format(path)
7878

7979
def write_xml(self, parent_element, dom):
8080
annotation = dom.createElement("annotation")
@@ -90,18 +90,18 @@ def write_xml(self, parent_element, dom):
9090
annotation.setAttribute("file", self.file_path)
9191

9292
if self.gzip_data is not None:
93-
annotation.setAttribute("link_file", "false")
94-
annotation.setAttribute("mime_type", self.mime_type)
95-
b64_data = base64.b64encode(self.gzip_data)
96-
b64_data_string = b64_data.decode()
97-
cdata = dom.createCDATASection(b64_data_string)
98-
annotation.appendChild(cdata)
93+
annotation.setAttribute("link_file", "false")
94+
annotation.setAttribute("mime_type", self.mime_type)
95+
b64_data = base64.b64encode(self.gzip_data)
96+
b64_data_string = b64_data.decode()
97+
cdata = dom.createCDATASection(b64_data_string)
98+
annotation.appendChild(cdata)
9999

100100
# add comments
101-
for c in self.comments:
102-
c_elem = dom.createElement('comment')
103-
c_elem.setAttribute("label", c.name)
104-
cdata = dom.createCDATASection(c.comment)
101+
for comment in self.comments:
102+
c_elem = dom.createElement("comment")
103+
c_elem.setAttribute("label", comment.name)
104+
cdata = dom.createCDATASection(comment.comment)
105105
c_elem.appendChild(cdata)
106106
annotation.appendChild(c_elem)
107107

@@ -132,20 +132,20 @@ def set_start_time(self, gmt_string):
132132

133133
def fail(self, message):
134134
self.status = 'failed'
135-
ta = self.add_text_annotation('FAIL', 'error')
136-
ta.description = message
135+
text_annotation = self.add_text_annotation('FAIL', 'error')
136+
text_annotation.description = message
137137

138138
def add_info_annotation(self, message):
139-
ta = self.add_text_annotation('Info', 'info')
140-
ta.description = message
139+
text_annotation = self.add_text_annotation('Info', 'info')
140+
text_annotation.description = message
141141

142142
def add_warning_annotation(self, message):
143-
ta = self.add_text_annotation('Warning', 'warn')
144-
ta.description = message
143+
text_annotation = self.add_text_annotation('Warning', 'warn')
144+
text_annotation.description = message
145145

146146
def add_error_annotation(self, message):
147-
ta = self.add_text_annotation('Error', 'error')
148-
ta.description = message
147+
text_annotation = self.add_text_annotation('Error', 'error')
148+
text_annotation.description = message
149149

150150
def add_file_annotation(self, name, level='info', description='',
151151
file_path=None, mime_type='text/plain'):
@@ -170,14 +170,14 @@ def add_link_annotation(self, path, level='info', description='', name=None):
170170
return fa
171171

172172
def add_text_annotation(self, name, level='info', description=''):
173-
ta = Annotation(name, level, description)
174-
self.annotations.append(ta)
175-
return ta
173+
text_annotation = Annotation(name, level, description)
174+
self.annotations.append(text_annotation)
175+
return text_annotation
176176

177177
def add_custom_metric(self, name, value):
178-
d = CustomData(name, value)
179-
self.custom_data.append(d)
180-
return d
178+
custom_data = CustomData(name, value)
179+
self.custom_data.append(custom_data)
180+
return custom_data
181181

182182
def add_annotation(self, annotation):
183183
self.annotations.append(annotation)
@@ -242,32 +242,45 @@ def add_link_annotation(self, path, level='info', description='', name=None):
242242
return fa
243243

244244
def add_text_annotation(self, name, level='info', description=''):
245-
ta = Annotation(name, level, description)
246-
self.annotations.append(ta)
247-
return ta
245+
text_annotation = Annotation(name, level, description)
246+
self.annotations.append(text_annotation)
247+
return text_annotation
248248

249249
def add_annotation(self, annotation):
250250
self.annotations.append(annotation)
251251

252252
def set_duration_ms(self, duration):
253253
self.duration = duration
254254

255+
def set_description(self, description):
256+
self.description = description
257+
258+
def set_start_time(self, gmt_string):
259+
self.start_time = gmt_string
260+
255261

256262
class XmlWriter:
257263
def __init__(self, report):
258264
self.report = report
259-
self.dom = parseString('<reporter schema_version="1.0"/>')
265+
266+
if report.product_version is None:
267+
reporter_string = '<reporter schema_version="1.0"/>'
268+
else:
269+
reporter_string = '<reporter schema_version="1.0" product_version="{0}"/>'\
270+
.format(report.product_version)
271+
272+
self.dom = parseString(reporter_string)
260273

261274
def write(self, target_file_path, to_pretty=False):
262275
doc_elem = self.dom.documentElement
263276
self._write_suite(doc_elem, self.report.get_root_suite())
264277
if target_file_path:
265-
with open(target_file_path, 'w') as f:
278+
with open(target_file_path, 'w') as target_file:
266279
if to_pretty:
267-
f.write(self.dom.toprettyxml())
280+
target_file.write(self.dom.toprettyxml())
268281
else:
269-
f.write(self.dom.toxml())
270-
f.flush()
282+
target_file.write(self.dom.toxml())
283+
target_file.flush()
271284
else:
272285
if to_pretty:
273286
sys.stdout.write(self.dom.toprettyxml())
@@ -320,10 +333,14 @@ class TestspaceReport(TestSuite):
320333
def __init__(self):
321334
TestSuite.__init__(self, '__root__')
322335
self.is_root_suite = True
336+
self.product_version = None
323337

324338
def get_root_suite(self):
325339
return self
326340

341+
def set_product_version(self, product_version):
342+
self.product_version = product_version
343+
327344
def write_xml(self, out_file=None, to_pretty=False):
328345
writer = XmlWriter(self)
329346
writer.write(out_file, to_pretty)

tests/test_testspace_report_xsd.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ class TestTestspaceReportXsd:
2828
@classmethod
2929
def setup_class(cls):
3030
testspace_report = testspace_xml.TestspaceReport()
31+
testspace_report.set_product_version('pytest')
3132

3233
for suite in cls.testsuite_list:
3334
testspace_report.add_test_suite(suite)
3435

3536
example_suite = testspace_report.get_or_add_test_suite('Example Suite')
37+
example_suite.set_description('https://testspace.com')
3638
example_suite.add_link_annotation('https://help.testspace.com')
3739
example_suite.add_link_annotation('https://testspace.com', name='Testspace.com')
3840
example_suite.add_string_buffer_annotation(

0 commit comments

Comments
 (0)