Skip to content

Commit 1328b5b

Browse files
authored
Add suite duration (#8)
* add test_suite duration * add annotation test order check * revert file deletion change * fix static analysis warning * add annotations tests * update annotations order * fix flake8 errors * add test_suite duration * add annotation test order check * revert file deletion change * fix static analysis warning * add annotations tests * update annotations order * fix flake8 errors * remove redundant testcase from merge * update missing invalid file path logic and tests * fix flake8 errors * add tests for annotation comments * revent comment * fix missing line
1 parent 46758c0 commit 1328b5b

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

python_testspace_xml/testspace_xml.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def add_file_annotation(self, file_path=None, mime_type='text/plain'):
5353
self.mimeType = mime_type
5454
if file_path is not None:
5555
if not os.path.isfile(self.file_path):
56-
ta = Annotation(self.file_path, level='error')
57-
ta.description = 'File: ' + self.file_path + ' not found.'
58-
self.comments.append(ta)
56+
self.level = 'error'
57+
self.description = 'File: ' + self.file_path + ' not found.'
58+
self.file_path = None
5959
else:
6060
with io.open(self.file_path, 'rb') as inFile:
6161
out = BytesIO()
@@ -70,9 +70,8 @@ def add_link_annotation(self, file_path=None):
7070
if match_path and file_path:
7171
self.file_path = "file://" + file_path.replace('\\', '/')
7272
else:
73-
ta = Annotation(self.file_path, level='error')
74-
ta.description = 'Invalid network file path given:' + file_path
75-
self.comments.append(ta)
73+
self.level = 'error'
74+
self.description = 'Invalid network file path given:' + file_path
7675

7776
def write_xml(self, parent_element, dom):
7877
annotation = dom.createElement("annotation")
@@ -167,6 +166,9 @@ def add_text_annotation(self, name, level='info', description=''):
167166
self.annotations.append(ta)
168167
return ta
169168

169+
def add_annotation(self, annotation):
170+
self.annotations.append(annotation)
171+
170172

171173
class TestSuite:
172174
def __init__(self, name):
@@ -175,6 +177,7 @@ def __init__(self, name):
175177
self.is_root_suite = False
176178
self.name = name
177179
self.description = ''
180+
self.duration = 0
178181
self.start_time = ''
179182
self.test_cases = []
180183
self.custom_data = []
@@ -220,6 +223,12 @@ def add_text_annotation(self, name, level='info', description=''):
220223
self.annotations.append(ta)
221224
return ta
222225

226+
def add_annotation(self, annotation):
227+
self.annotations.append(annotation)
228+
229+
def set_duration_ms(self, duration):
230+
self.duration = duration
231+
223232

224233
class XmlWriter:
225234
def __init__(self, report):
@@ -251,6 +260,8 @@ def _write_suite(self, parent_node, test_suite):
251260
suite_elem.setAttribute('description', test_suite.description)
252261
suite_elem.setAttribute('start_time', str(test_suite.start_time))
253262
parent_node.appendChild(suite_elem)
263+
if test_suite.duration > 0:
264+
suite_elem.setAttribute('duration', test_suite.duration)
254265

255266
for a in test_suite.annotations:
256267
a.write_xml(suite_elem, self.dom)

tests/test_TestspaceReport.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import pytest
21
import os
32
from lxml import etree, objectify
43
from lxml.etree import XMLSyntaxError
@@ -7,17 +6,34 @@
76

87

98
def create_simple_testspace_xml(self):
9+
self.annotation_tuple = [
10+
('zannotation warning example', 'warn', 'to confirm order of annotations'),
11+
('annotation info example', 'info', 'description of annotation'),
12+
('aannotation error example', 'error', 'to confirm order of annotations')]
13+
1014
testspace_report = testspace_xml.TestspaceReport()
15+
1116
example_suite = testspace_report.get_or_add_test_suite('Example Suite')
1217
test_case = testspace_xml.TestCase('passing case 1', 'passed')
13-
test_case.add_text_annotation('annotation example', description='description of annotation')
18+
for annotation in self.annotation_tuple:
19+
test_case.add_text_annotation(
20+
annotation[0], level=annotation[1], description=annotation[2])
1421
example_suite.add_test_case(test_case)
22+
1523
test_case = testspace_xml.TestCase('passing case 2', 'passed')
16-
test_case.add_file_annotation('report_v1.xsd', file_path='tests/report_v1.xsd')
24+
test_case.add_file_annotation('tests/report_v1.xsd', file_path='tests/report_v1.xsd')
25+
test_case.add_file_annotation('report_v1.xsd', file_path='/report_v1.xsd')
1726
test_case.add_link_annotation(file_path=r'\\machine/public')
27+
test_case.add_info_annotation(self.annotation_tuple[0][2])
28+
test_case.add_error_annotation(self.annotation_tuple[1][2])
29+
test_case.add_warning_annotation(self.annotation_tuple[2][2])
1830
example_suite.add_test_case(test_case)
19-
test_case = testspace_xml.TestCase('failing case 1', 'failed')
31+
32+
test_case = testspace_xml.TestCase('failing case 1')
33+
test_case.fail('failing testcase') # adds annotation to testcase
2034
example_suite.add_test_case(test_case)
35+
test_annotation = test_case.add_text_annotation('annotation with comment')
36+
test_annotation.add_comment("comment", "annotation comment")
2137
testspace_report.write_xml('testspace.xml', to_pretty=True)
2238

2339
xml_file = open('testspace.xml', 'r')
@@ -28,9 +44,9 @@ def create_simple_testspace_xml(self):
2844

2945

3046
class TestTestspaceXml:
31-
@pytest.fixture(autouse=True)
32-
def setup_class(self):
33-
create_simple_testspace_xml(self)
47+
@classmethod
48+
def setup_class(cls):
49+
create_simple_testspace_xml(cls)
3450

3551
@classmethod
3652
def teardown_class(cls):
@@ -53,12 +69,22 @@ def test_number_failed_testcases(self):
5369

5470
def test_number_annotations(self):
5571
test_cases = self.testspace_xml_root.xpath("//test_suite/test_case/annotation")
56-
assert len(test_cases) is 3
72+
assert len(test_cases) is 11
5773

5874
def test_number_file_annotations(self):
5975
test_cases = self.testspace_xml_root.xpath("//test_suite/test_case/annotation[@file]")
6076
assert len(test_cases) is 2
6177

78+
def test_number_annotation_comments(self):
79+
test_cases = self.testspace_xml_root.xpath("//test_suite/test_case/annotation/comment")
80+
assert len(test_cases) is 1
81+
82+
def test_annotation_order(self):
83+
annotations = self.testspace_xml_root.xpath(
84+
"//test_suite/test_case[@name='passing case 1']/annotation")
85+
for idx, annotation in enumerate(annotations):
86+
assert annotation.get('name') == self.annotation_tuple[idx][0]
87+
6288
def test_validate_xsd(self):
6389
assert xml_validator(self.testspace_xml_string, 'tests/report_v1.xsd')
6490

0 commit comments

Comments
 (0)