Skip to content

Commit 40f01ad

Browse files
authored
Add tox (#10)
* add tox to run pytest * remove testcase custom data * update annotation link and tests * add annotation as buffer * fix python 2.7 gzip compress * add metric data * add tox to travis * fix pip syntax error
1 parent ee69387 commit 40f01ad

File tree

6 files changed

+113
-39
lines changed

6 files changed

+113
-39
lines changed

.coveragerc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# .coveragerc to control coverage.py
2+
[run]
3+
branch = True

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ before_install:
99
- testspace config url s2.testspace.com
1010

1111
install:
12-
- pip install -r requirements.txt
12+
- pip install tox
1313

1414
script:
1515
- export PYTHONPATH=$PYTHONPATH:$TRAVIS_BUILD_DIR
16-
- py.test --junit-xml=testresults.xml --cov=python_testspace_xml --cov-report xml:coverage.xml --cov-report term
16+
- tox -e py
1717

1818
after_script:
1919
- if [ "${TRAVIS_PYTHON_VERSION}" = "3.5" ]; then
20-
flake8 --max-line-length=100 --output-file=flake8.txt --exit-zero;
2120
testspace [${TRAVIS_PYTHON_VERSION}]flake8.txt{lint} [${TRAVIS_PYTHON_VERSION}/tests]testresults.xml{tests} [${TRAVIS_PYTHON_VERSION}]coverage.xml;
2221
else
2322
testspace [${TRAVIS_PYTHON_VERSION}/tests]testresults.xml{tests};

python_testspace_xml/testspace_xml.py

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import print_function
22
import base64
33
import gzip
4-
import re
54
import os
65
import io
76
from io import BytesIO
@@ -10,10 +9,6 @@
109
from xml.dom.minidom import parseString
1110

1211

13-
def make_file_href_link(file_path):
14-
return "<a href='file://" + file_path.replace('\\', '/') + "'>" + file_path + '</a>'
15-
16-
1712
class CustomData:
1813
def __init__(self, name, value):
1914
self.name = name
@@ -48,7 +43,7 @@ def add_comment(self, name, comment):
4843
comment = AnnotationComment(name, comment)
4944
self.comments.append(comment)
5045

51-
def add_file_annotation(self, file_path=None, mime_type='text/plain'):
46+
def set_file_annotation(self, file_path=None, mime_type='text/plain', string_buffer=None):
5247
self.file_path = file_path
5348
self.mimeType = mime_type
5449
if file_path is not None:
@@ -63,15 +58,23 @@ def add_file_annotation(self, file_path=None, mime_type='text/plain'):
6358
f.writelines(inFile)
6459
f.close()
6560
self.gzip_data = out.getvalue()
66-
67-
def add_link_annotation(self, file_path=None):
61+
elif string_buffer is not None:
62+
byte_string_buffer = string_buffer.encode()
63+
out = BytesIO()
64+
with gzip.GzipFile(fileobj=out, mode="wb") as f:
65+
f.write(byte_string_buffer)
66+
f.close()
67+
self.gzip_data = out.getvalue()
68+
69+
def set_link_annotation(self, path=None):
6870
self.link_file = True
69-
match_path = re.search(r'\\\w', file_path)
70-
if match_path and file_path:
71-
self.file_path = "file://" + file_path.replace('\\', '/')
71+
if path.startswith(r'\\'):
72+
self.file_path = "file://" + path.replace('\\', '/')
73+
elif path.startswith(r'https') or path.startswith(r'http://'):
74+
self.file_path = path
7275
else:
7376
self.level = 'error'
74-
self.description = 'Invalid network file path given:' + file_path
77+
self.description = 'Invalid path given:' + path
7578

7679
def write_xml(self, parent_element, dom):
7780
annotation = dom.createElement("annotation")
@@ -84,10 +87,11 @@ def write_xml(self, parent_element, dom):
8487
annotation.setAttribute("link_file", "true")
8588
annotation.setAttribute("file", self.file_path)
8689
else:
87-
annotation.setAttribute("link_file", "false")
8890
annotation.setAttribute("file", self.file_path)
89-
annotation.setAttribute("mime_type", self.mimeType)
90-
if self.gzip_data is not None:
91+
92+
if self.gzip_data is not None:
93+
annotation.setAttribute("link_file", "false")
94+
annotation.setAttribute("mime_type", self.mimeType)
9195
b64_data = base64.b64encode(self.gzip_data)
9296
b64_data_string = b64_data.decode()
9397
cdata = dom.createCDATASection(b64_data_string)
@@ -143,21 +147,23 @@ def add_error_annotation(self, message):
143147
ta = self.add_text_annotation('Error', 'error')
144148
ta.description = message
145149

146-
def add_custom_data(self, name, value):
147-
d = CustomData(name, value)
148-
self.custom_data.append(d)
149-
return d
150-
151150
def add_file_annotation(self, name, level='info', description='',
152151
file_path=None, mime_type='text/plain'):
153152
fa = Annotation(name, level, description)
154-
fa.add_file_annotation(file_path, mime_type)
153+
fa.set_file_annotation(file_path, mime_type)
154+
self.annotations.append(fa)
155+
return fa
156+
157+
def add_string_buffer_annotation(self, name, level='info', description='',
158+
string_buffer=None, mime_type='text/plain'):
159+
fa = Annotation(name, level, description)
160+
fa.set_file_annotation(string_buffer=string_buffer, mime_type='text/plain')
155161
self.annotations.append(fa)
156162
return fa
157163

158-
def add_link_annotation(self, level='info', description='', file_path=None):
159-
fa = Annotation(file_path, level, description)
160-
fa.add_link_annotation(file_path)
164+
def add_link_annotation(self, level='info', description='', path=None):
165+
fa = Annotation(path, level, description)
166+
fa.set_link_annotation(path)
161167
self.annotations.append(fa)
162168
return fa
163169

@@ -166,6 +172,11 @@ def add_text_annotation(self, name, level='info', description=''):
166172
self.annotations.append(ta)
167173
return ta
168174

175+
def add_custom_metric(self, name, value):
176+
d = CustomData(name, value)
177+
self.custom_data.append(d)
178+
return d
179+
169180
def add_annotation(self, annotation):
170181
self.annotations.append(annotation)
171182

@@ -200,21 +211,28 @@ def add_test_suite(self, name):
200211
self.sub_suites[str(name)] = new_suite
201212
return new_suite
202213

203-
def add_custom_data(self, name, value):
214+
def add_custom_metric(self, name, value):
204215
d = CustomData(name, value)
205216
self.custom_data.append(d)
206217
return d
207218

208219
def add_file_annotation(self, name, level='info', description='',
209220
file_path=None, mime_type='text/plain'):
210221
fa = Annotation(name, level, description)
211-
fa.add_file_annotation(file_path, mime_type)
222+
fa.set_file_annotation(file_path, mime_type)
223+
self.annotations.append(fa)
224+
return fa
225+
226+
def add_string_buffer_annotation(self, name, level='info', description='',
227+
string_buffer=None, mime_type='text/plain'):
228+
fa = Annotation(name, level, description)
229+
fa.set_file_annotation(string_buffer=string_buffer, mime_type='text/plain')
212230
self.annotations.append(fa)
213231
return fa
214232

215-
def add_link_annotation(self, level='info', description='', file_path=None):
216-
fa = Annotation(file_path, level, description)
217-
fa.add_link_annotation(file_path)
233+
def add_link_annotation(self, level='info', description='', path=None):
234+
fa = Annotation(path, level, description)
235+
fa.set_link_annotation(path)
218236
self.annotations.append(fa)
219237
return fa
220238

setup.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name='testspace_api',
5+
version='',
6+
packages=find_packages(),
7+
url='',
8+
license='',
9+
author='jaschultz',
10+
author_email='',
11+
description='',
12+
install_requires=[
13+
'lxml',
14+
'pytest',
15+
'pytest-cov',
16+
'flake8',
17+
'pep8-naming',
18+
]
19+
)
Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@ def create_simple_testspace_xml(self):
1111
('annotation info example', 'info', 'description of annotation'),
1212
('aannotation error example', 'error', 'to confirm order of annotations')]
1313

14+
string_buffer = 'Text content to be displayed in Testspace\n' \
15+
'Additional line of content'
16+
17+
test_annotation = testspace_xml.Annotation('annotation with comment')
18+
test_annotation.add_comment("comment", "annotation comment")
19+
1420
testspace_report = testspace_xml.TestspaceReport()
1521

1622
example_suite = testspace_report.get_or_add_test_suite('Example Suite')
23+
example_suite.add_link_annotation(path='https://help.testspace.com')
24+
example_suite.add_string_buffer_annotation(
25+
'Suite string annotation as file', string_buffer=string_buffer)
26+
example_suite.add_text_annotation(
27+
'Suite Text Annotation', description='This is a string annotation only')
28+
example_suite.add_file_annotation('tests/report_v1.xsd', file_path='tests/report_v1.xsd')
29+
example_suite.add_custom_metric('suite stats', '0, 3, 4')
30+
example_suite.add_annotation(test_annotation)
31+
1732
test_case = testspace_xml.TestCase('passing case 1', 'passed')
1833
for annotation in self.annotation_tuple:
1934
test_case.add_text_annotation(
@@ -23,17 +38,20 @@ def create_simple_testspace_xml(self):
2338
test_case = testspace_xml.TestCase('passing case 2', 'passed')
2439
test_case.add_file_annotation('tests/report_v1.xsd', file_path='tests/report_v1.xsd')
2540
test_case.add_file_annotation('report_v1.xsd', file_path='/report_v1.xsd')
26-
test_case.add_link_annotation(file_path=r'\\machine/public')
41+
test_case.add_string_buffer_annotation(
42+
'Case string annotation as file', string_buffer=string_buffer)
43+
test_case.add_link_annotation(path=r'\\machine/public')
2744
test_case.add_info_annotation(self.annotation_tuple[0][2])
2845
test_case.add_error_annotation(self.annotation_tuple[1][2])
2946
test_case.add_warning_annotation(self.annotation_tuple[2][2])
3047
example_suite.add_test_case(test_case)
3148

3249
test_case = testspace_xml.TestCase('failing case 1')
3350
test_case.fail('failing testcase') # adds annotation to testcase
51+
test_case.add_custom_metric('suite stats', '0, 3, 4')
52+
test_case.add_annotation(test_annotation)
3453
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")
54+
3755
testspace_report.write_xml('testspace.xml', to_pretty=True)
3856

3957
xml_file = open('testspace.xml', 'r')
@@ -55,6 +73,14 @@ def teardown_class(cls):
5573
"""
5674
os.remove('testspace.xml')
5775

76+
def test_number_testsuites(self):
77+
test_cases = self.testspace_xml_root.xpath("//test_suite")
78+
assert len(test_cases) is 1
79+
80+
def test_number_testsuite_annotations(self):
81+
test_cases = self.testspace_xml_root.xpath("//test_suite/annotation")
82+
assert len(test_cases) is 5
83+
5884
def test_number_testcases(self):
5985
test_cases = self.testspace_xml_root.xpath("//test_suite/test_case")
6086
assert len(test_cases) is 3
@@ -67,15 +93,15 @@ def test_number_failed_testcases(self):
6793
test_cases = self.testspace_xml_root.xpath("//test_suite/test_case[@status='failed']")
6894
assert len(test_cases) is 1
6995

70-
def test_number_annotations(self):
96+
def test_number_testcase_annotations(self):
7197
test_cases = self.testspace_xml_root.xpath("//test_suite/test_case/annotation")
72-
assert len(test_cases) is 11
98+
assert len(test_cases) is 12
7399

74-
def test_number_file_annotations(self):
100+
def test_number_testcase_file_annotations(self):
75101
test_cases = self.testspace_xml_root.xpath("//test_suite/test_case/annotation[@file]")
76102
assert len(test_cases) is 2
77103

78-
def test_number_annotation_comments(self):
104+
def test_number_testcase_annotation_comments(self):
79105
test_cases = self.testspace_xml_root.xpath("//test_suite/test_case/annotation/comment")
80106
assert len(test_cases) is 1
81107

tox.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[testenv]
2+
deps=lxml
3+
pytest
4+
pytest-cov
5+
flake8
6+
pep8-naming
7+
8+
commands=py.test tests --junit-xml=testresults.xml --cov-report term --cov-report xml:coverage.xml --cov {envsitepackagesdir}/python_testspace_xml --cov-config .coveragerc
9+
flake8 --max-line-length=120 --output-file=flake8.txt --exit-zero

0 commit comments

Comments
 (0)