/
xunit_output.py
112 lines (92 loc) · 4.17 KB
/
xunit_output.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
# -*- coding: utf-8 -*-
# <Lettuce - Behaviour Driven Development for python>
# Copyright (C) <2010-2012> Gabriel Falcão <gabriel@nacaolivre.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERsteps.pyCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime, timedelta
from lettuce.terrain import after
from lettuce.terrain import before
from xml.dom import minidom
from lettuce.strings import utf8_string
def wrt_output(filename, content):
f = open(filename, "w")
if isinstance(content, unicode):
content = content.encode('utf-8')
f.write(content)
f.close()
def total_seconds(td):
return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 1e6) / 1e6
def enable(filename=None):
doc = minidom.Document()
root = doc.createElement("testsuite")
root.setAttribute("name", "lettuce")
root.setAttribute("hostname", "localhost")
root.setAttribute("timestamp", datetime.now().strftime("%Y-%m-%dT%H:%M:%S"))
output_filename = filename or "lettucetests.xml"
@before.each_step
def time_step(step):
step.started = datetime.now()
@after.each_step
def create_test_case_step(step):
parent = step.scenario or step.background
if getattr(parent, 'outlines', None):
return
name = getattr(parent, 'name', 'Background') # Background sections are nameless
classname = utf8_string(u"%s : %s" % (parent.feature.name, name))
tc = doc.createElement("testcase")
tc.setAttribute("classname", classname)
tc.setAttribute("name", step.sentence.encode('utf-8'))
try:
tc.setAttribute("time", str(total_seconds((datetime.now() - step.started))))
except AttributeError:
tc.setAttribute("time", str(total_seconds(timedelta(seconds=0))))
if not step.ran:
skip = doc.createElement("skipped")
skip.setAttribute("type", "UndefinedStep(%s)" % step.sentence)
tc.appendChild(skip)
if step.failed:
cdata = doc.createCDATASection(step.why.traceback.encode('utf-8'))
failure = doc.createElement("failure")
if hasattr(step.why, 'cause'):
failure.setAttribute("message", step.why.cause.encode('utf-8'))
failure.setAttribute("type", step.why.exception.__class__.__name__.encode('utf-8'))
failure.appendChild(cdata)
tc.appendChild(failure)
root.appendChild(tc)
@before.outline
def time_outline(scenario, order, outline, reasons_to_fail):
scenario.outline_started = datetime.now()
pass
@after.outline
def create_test_case_outline(scenario, order, outline, reasons_to_fail):
classname = "%s : %s" % (scenario.feature.name, scenario.name)
tc = doc.createElement("testcase")
tc.setAttribute("classname", classname)
tc.setAttribute("name", u'| %s |' % u' | '.join(outline.values()))
tc.setAttribute("time", str(total_seconds((datetime.now() - scenario.outline_started))))
for reason_to_fail in reasons_to_fail:
cdata = doc.createCDATASection(reason_to_fail.traceback)
failure = doc.createElement("failure")
failure.setAttribute("message", reason_to_fail.cause)
failure.appendChild(cdata)
tc.appendChild(failure)
root.appendChild(tc)
@after.all
def output_xml(total):
root.setAttribute("tests", str(total.steps))
root.setAttribute("failures", str(total.steps_failed))
root.setAttribute("errors", '0')
root.setAttribute("time", '0')
doc.appendChild(root)
wrt_output(output_filename, doc.toxml())