Skip to content

Commit c6c3c90

Browse files
feffesysradium
authored andcommitted
Convert tests to pytest style. Separate suite and case test modules. Break out test helpers. Fix flake8 issues. Fix formatting.
1 parent 5ea26cd commit c6c3c90

9 files changed

+616
-586
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ env:
1919
- TOXENV=py27
2020
- TOXENV=py36
2121
- TOXENV=cover
22+
- TOXENV=flake8
2223

2324
install:
2425
- travis_retry pip install tox==3.13.2

asserts.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
3+
def verify_test_case(
4+
test_case_element,
5+
expected_attributes,
6+
error_message=None,
7+
error_output=None,
8+
error_type=None,
9+
failure_message=None,
10+
failure_output=None,
11+
failure_type=None,
12+
skipped_message=None,
13+
skipped_output=None,
14+
stdout=None,
15+
stderr=None
16+
):
17+
for k, v in expected_attributes.items():
18+
assert test_case_element.attributes[k].value == v
19+
20+
for k in test_case_element.attributes.keys():
21+
assert k in expected_attributes.keys()
22+
23+
if stderr:
24+
assert test_case_element.getElementsByTagName('system-err')[0].firstChild.nodeValue.strip() == stderr
25+
if stdout:
26+
assert test_case_element.getElementsByTagName('system-out')[0].firstChild.nodeValue.strip() == stdout
27+
28+
errors = test_case_element.getElementsByTagName('error')
29+
if error_message or error_output:
30+
assert len(errors) > 0
31+
else:
32+
assert len(errors) == 0
33+
34+
if error_message:
35+
assert errors[0].attributes['message'].value == error_message
36+
37+
if error_type and errors:
38+
assert errors[0].attributes['type'].value == error_type
39+
40+
if error_output:
41+
assert errors[0].firstChild.nodeValue.strip() == error_output
42+
43+
failures = test_case_element.getElementsByTagName('failure')
44+
if failure_message or failure_output:
45+
assert len(failures) > 0
46+
else:
47+
assert len(failures) == 0
48+
49+
if failure_message:
50+
assert failures[0].attributes['message'].value == failure_message
51+
52+
if failure_type and failures:
53+
assert failures[0].attributes['type'].value == failure_type
54+
55+
if failure_output:
56+
assert failures[0].firstChild.nodeValue.strip() == failure_output
57+
58+
skipped = test_case_element.getElementsByTagName('skipped')
59+
if skipped_message or skipped_output:
60+
assert len(skipped) > 0
61+
else:
62+
assert len(skipped) == 0

junit_xml/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ def decode(var, encoding):
5959
If not already unicode, decode it.
6060
"""
6161
if PY2:
62-
if isinstance(var, unicode):
62+
if isinstance(var, unicode): # NOQA
6363
ret = var
6464
elif isinstance(var, str):
6565
if encoding:
6666
ret = var.decode(encoding)
6767
else:
68-
ret = unicode(var)
68+
ret = unicode(var) # NOQA
6969
else:
70-
ret = unicode(var)
70+
ret = unicode(var) # NOQA
7171
else:
7272
ret = str(var)
7373
return ret

serializer.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import codecs
2+
import os
3+
import tempfile
4+
from xml.dom import minidom
5+
6+
from six import PY2
7+
8+
from junit_xml import TestSuite as Suite
9+
10+
11+
def serialize_and_read(test_suites, to_file=False, prettyprint=False, encoding=None):
12+
"""writes the test suite to an XML string and then re-reads it using minidom,
13+
returning => (test suite element, list of test case elements)"""
14+
try:
15+
iter(test_suites)
16+
except TypeError:
17+
test_suites = [test_suites]
18+
19+
if to_file:
20+
fd, filename = tempfile.mkstemp(text=True)
21+
os.close(fd)
22+
with codecs.open(filename, mode='w', encoding=encoding) as f:
23+
Suite.to_file(f, test_suites, prettyprint=prettyprint, encoding=encoding)
24+
print("Serialized XML to temp file [%s]" % filename)
25+
xmldoc = minidom.parse(filename)
26+
os.remove(filename)
27+
else:
28+
xml_string = Suite.to_xml_string(
29+
test_suites, prettyprint=prettyprint, encoding=encoding)
30+
if PY2:
31+
assert isinstance(xml_string, unicode) # NOQA
32+
print("Serialized XML to string:\n%s" % xml_string)
33+
if encoding:
34+
xml_string = xml_string.encode(encoding)
35+
xmldoc = minidom.parseString(xml_string)
36+
37+
def remove_blanks(node):
38+
for x in node.childNodes:
39+
if x.nodeType == minidom.Node.TEXT_NODE:
40+
if x.nodeValue:
41+
x.nodeValue = x.nodeValue.strip()
42+
elif x.nodeType == minidom.Node.ELEMENT_NODE:
43+
remove_blanks(x)
44+
45+
remove_blanks(xmldoc)
46+
xmldoc.normalize()
47+
48+
ret = []
49+
suites = xmldoc.getElementsByTagName("testsuites")[0]
50+
for suite in suites.getElementsByTagName("testsuite"):
51+
cases = suite.getElementsByTagName("testcase")
52+
ret.append((suite, cases))
53+
return ret

setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
max-line-length = 120
3+
exclude = .git,.tox,build,dist,venv

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
def read(fname):
77
return open(os.path.join(os.path.dirname(__file__), fname)).read()
88

9+
910
setup(
1011
name='junit-xml',
1112
author='Brian Beyer',

0 commit comments

Comments
 (0)