3 # Pretty-format subunit output
4 # Copyright (C) 2008-2010 Jelmer Vernooij <jelmer@samba.org>
5 # Published under the GNU GPL, v3 or later
13 class PlainFormatter(object):
15 def __init__(self
, summaryfile
, verbose
, immediate
, statistics
, totaltests
=None):
16 self
.verbose
= verbose
17 self
.immediate
= immediate
18 self
.statistics
= statistics
19 self
.start_time
= None
21 self
.suitesfailed
= []
24 self
.summaryfile
= summaryfile
27 self
.totalsuites
= totaltests
29 def testsuite_count(self
, count
):
30 self
.totalsuites
= count
32 def report_time(self
, time
):
33 if self
.start_time
is None:
34 self
.start_time
= time
37 def start_testsuite(self
, name
):
40 self
.START_TIME
= self
.last_time
42 duration
= self
.START_TIME
- self
.start_time
45 self
.test_output
[name
] = ""
48 out
+= "[%d" % self
.index
49 if self
.totalsuites
is not None:
50 out
+= "/%d" % self
.totalsuites
51 out
+= " in %ds" % duration
53 out
+= ", %d errors" % (len(self
.suitesfailed
),)
56 sys
.stdout
.write(out
+ "\n")
58 sys
.stdout
.write(out
+ ": ")
60 def output_msg(self
, output
):
62 sys
.stdout
.write(output
)
63 elif self
.NAME
is not None:
64 self
.test_output
[self
.NAME
] += output
66 sys
.stdout
.write(output
)
68 def control_msg(self
, output
):
69 #$self->output_msg($output)
72 def end_testsuite(self
, name
, result
, reason
):
76 if not name
in self
.test_output
:
77 print "no output for name[%s]" % name
79 if result
in ("success", "xfail"):
82 self
.output_msg("ERROR: Testsuite[%s]\nREASON: %s\n" % (name
, reason
))
83 self
.suitesfailed
.append(name
)
84 if self
.immediate
and not self
.verbose
:
85 out
+= self
.test_output
[name
]
88 if not self
.immediate
:
92 out
+= " " + result
.upper() + "\n"
96 def start_test(self
, testname
):
99 def end_test(self
, testname
, result
, unexpected
, reason
):
103 self
.test_output
[self
.NAME
] = ""
104 if not self
.immediate
:
109 'success': '.'}.get(result
, "?(%s)" % result
))
112 reason
= reason
.strip()
114 append
= "UNEXPECTED(%s): %s\nREASON: %s\n" % (result
, testname
, reason
)
116 self
.test_output
[self
.NAME
] += append
118 if self
.immediate
and not self
.verbose
:
119 print self
.test_output
[self
.NAME
]
120 self
.test_output
[self
.NAME
] = ""
122 if not self
.immediate
:
126 'success': 'S'}.get(result
, "?"))
129 f
= open(self
.summaryfile
, 'w+')
131 if self
.suitesfailed
:
132 f
.write("= Failed tests =\n")
134 for suite
in self
.suitesfailed
:
135 f
.write("== %s ==\n" % suite
)
136 f
.write(self
.test_output
[suite
]+"\n\n")
140 if not self
.immediate
and not self
.verbose
:
141 for suite
in self
.suitesfailed
:
142 print "==============================================================================="
143 print "FAIL: %s" % suite
144 print self
.test_output
[suite
]
147 f
.write("= Skipped tests =\n")
148 for reason
in self
.skips
.keys():
149 f
.write(reason
+ "\n")
150 for name
in self
.skips
[reason
]:
151 f
.write("\t%s\n" % name
)
155 print "\nA summary with detailed information can be found in:\n %s\n" % self
.summaryfile
157 if not self
.suitesfailed
:
158 ok
= self
.statistics
['TESTS_EXPECTED_OK'] + self
.statistics
['TESTS_EXPECTED_FAIL']
159 print "\nALL OK (%d tests in %d testsuites)" % (ok
, self
.suites_ok
)
161 print "\nFAILED (%d failures and %d errors in %d testsuites)" % (self
.statistics
['TESTS_UNEXPECTED_FAIL'], self
.statistics
['TESTS_ERROR'], len(self
.suitesfailed
))
163 def skip_testsuite(self
, name
, reason
="UNKNOWN"):
164 self
.skips
.setdefault(reason
, []).append(name
)
168 parser
= optparse
.OptionParser("format-subunit [options]")
169 parser
.add_option("--verbose", action
="store_true",
171 parser
.add_option("--immediate", action
="store_true",
172 help="Show failures immediately, don't wait until test run has finished")
173 parser
.add_option("--prefix", type="string", default
=".",
174 help="Prefix to write summary to")
176 opts
, args
= parser
.parse_args()
180 'TESTS_UNEXPECTED_OK': 0,
181 'TESTS_EXPECTED_OK': 0,
182 'TESTS_UNEXPECTED_FAIL': 0,
183 'TESTS_EXPECTED_FAIL': 0,
188 msg_ops
= PlainFormatter(os
.path
.join(opts
.prefix
, "summary"), opts
.verbose
, opts
.immediate
, statistics
)
190 expected_ret
= subunithelper
.parse_results(msg_ops
, statistics
, sys
.stdin
)
194 sys
.exit(expected_ret
)