1 # Python module for parsing and generating the Subunit protocol
3 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 __all__ = ['parse_results']
23 import subunit.iso8601
26 VALID_RESULTS = ['success', 'successful', 'failure', 'fail', 'skip', 'knownfail', 'error', 'xfail', 'skip-testsuite', 'testsuite-failure', 'testsuite-xfail', 'testsuite-success', 'testsuite-error']
28 class TestsuiteEnabledTestResult(testtools.testresult.TestResult):
30 def start_testsuite(self, name):
31 raise NotImplementedError(self.start_testsuite)
34 def parse_results(msg_ops, statistics, fh):
42 parts = l.split(None, 1)
43 if not len(parts) == 2 or not l.startswith(parts[0]):
46 command = parts[0].rstrip(":")
48 if command in ("test", "testing"):
49 msg_ops.control_msg(l)
50 msg_ops.startTest(arg.rstrip())
51 open_tests.append(arg.rstrip())
52 elif command == "time":
53 msg_ops.control_msg(l)
55 dt = subunit.iso8601.parse_date(arg.rstrip("\n"))
57 print "Unable to parse time line: %s" % arg.rstrip("\n")
60 elif command in VALID_RESULTS:
61 msg_ops.control_msg(l)
63 grp = re.match("(.*?)( \[)?([ \t]*)( multipart)?\n", arg)
64 (testname, hasreason) = (grp.group(1), grp.group(2))
67 # reason may be specified in next lines
73 msg_ops.control_msg(l)
81 statistics['TESTS_ERROR']+=1
82 msg_ops.end_test(testname, "error", True,
83 "reason (%s) interrupted" % result)
87 if result in ("success", "successful"):
89 open_tests.remove(testname)
91 statistics['TESTS_ERROR']+=1
92 msg_ops.end_test(testname, "error", True,
93 "Test was never started")
95 statistics['TESTS_EXPECTED_OK']+=1
96 msg_ops.end_test(testname, "success", False, reason)
97 elif result in ("xfail", "knownfail"):
99 open_tests.remove(testname)
101 statistics['TESTS_ERROR']+=1
102 msg_ops.end_test(testname, "error", True,
103 "Test was never started")
105 statistics['TESTS_EXPECTED_FAIL']+=1
106 msg_ops.end_test(testname, "xfail", False, reason)
108 elif result in ("failure", "fail"):
110 open_tests.remove(testname)
112 statistics['TESTS_ERROR']+=1
113 msg_ops.end_test(testname, "error", True,
114 "Test was never started")
116 statistics['TESTS_UNEXPECTED_FAIL']+=1
117 msg_ops.end_test(testname, "failure", True, reason)
118 elif result == "skip":
119 statistics['TESTS_SKIP']+=1
120 # Allow tests to be skipped without prior announcement of test
121 last = open_tests.pop()
122 if last is not None and last != testname:
123 open_tests.append(testname)
124 msg_ops.end_test(testname, "skip", False, reason)
125 elif result == "error":
126 statistics['TESTS_ERROR']+=1
128 open_tests.remove(testname)
131 msg_ops.end_test(testname, "error", True, reason)
132 elif result == "skip-testsuite":
133 msg_ops.skip_testsuite(testname)
134 elif result == "testsuite-success":
135 msg_ops.end_testsuite(testname, "success", reason)
136 elif result == "testsuite-failure":
137 msg_ops.end_testsuite(testname, "failure", reason)
138 elif result == "testsuite-xfail":
139 msg_ops.end_testsuite(testname, "xfail", reason)
140 elif result == "testsuite-error":
141 msg_ops.end_testsuite(testname, "error", reason)
143 raise AssertionError("Recognized but unhandled result %r" %
145 elif command == "testsuite":
146 msg_ops.start_testsuite(arg.strip())
147 elif command == "progress":
150 msg_ops.progress(None, subunit.PROGRESS_POP)
152 msg_ops.progress(None, subunit.PROGRESS_PUSH)
154 msg_ops.progress(int(arg), subunit.PROGRESS_CUR)
156 msg_ops.progress(int(arg), subunit.PROGRESS_SET)
158 msg_ops.output_msg(l)
161 msg_ops.end_test(open_tests.pop(), "error", True,
162 "was started but never finished!")
163 statistics['TESTS_ERROR']+=1
165 if statistics['TESTS_ERROR'] > 0:
167 if statistics['TESTS_UNEXPECTED_FAIL'] > 0:
172 class SubunitOps(subunit.TestProtocolClient,TestsuiteEnabledTestResult):
174 def startTest(self, testname):
175 self._stream.write("test: %s\n" % testname)
177 def end_test(self, name, result, reason=None):
179 self._stream.write("%s: %s [\n%s\n]\n" % (result, name, reason))
181 self._stream.write("%s: %s\n" % (result, name))
183 def skip_test(self, name, reason=None):
184 self.end_test(name, "skip", reason)
186 def fail_test(self, name, reason=None):
187 self.end_test(name, "fail", reason)
189 def success_test(self, name, reason=None):
190 self.end_test(name, "success", reason)
192 def xfail_test(self, name, reason=None):
193 self.end_test(name, "xfail", reason)
195 # The following are Samba extensions:
196 def start_testsuite(self, name):
197 self._stream.write("testsuite: %s\n" % name)
199 def skip_testsuite(self, name, reason=None):
201 self._stream.write("skip-testsuite: %s [\n%s\n]\n" % (name, reason))
203 self._stream.write("skip-testsuite: %s\n" % name)
205 def end_testsuite(self, name, result, reason=None):
207 self._stream.write("testsuite-%s: %s [\n%s\n]\n" % (result, name, reason))
209 self._stream.write("testsuite-%s: %s\n" % (result, name))
212 def read_test_regexes(name):
218 if l == "" or l[0] == "#":
221 (regex, reason) = l.split("#", 1)
222 ret[regex.strip()] = reason.strip()
230 def find_in_list(regexes, fullname):
231 for regex, reason in regexes.iteritems():
232 if re.match(regex, fullname):
239 class FilterOps(testtools.testresult.TestResult):
241 def control_msg(self, msg):
242 pass # We regenerate control messages, so ignore this
244 def time(self, time):
247 def progress(self, delta, whence):
248 self._ops.progress(delta, whence)
250 def output_msg(self, msg):
251 if self.output is None:
252 sys.stdout.write(msg)
256 def startTest(self, testname):
257 if self.prefix is not None:
258 testname = self.prefix + testname
260 if self.strip_ok_output:
263 self._ops.startTest(testname)
265 def end_test(self, testname, result, unexpected, reason):
266 if self.prefix is not None:
267 testname = self.prefix + testname
269 if result in ("fail", "failure") and not unexpected:
273 xfail_reason = find_in_list(self.expected_failures, testname)
274 if xfail_reason is not None and result in ("fail", "failure"):
278 reason += xfail_reason
280 if result in ("fail", "failure"):
284 if result == "error":
288 if self.strip_ok_output:
289 if result not in ("success", "xfail", "skip"):
293 self._ops.end_test(testname, result, reason)
295 def skip_testsuite(self, name, reason=None):
296 self._ops.skip_testsuite(name, reason)
298 def start_testsuite(self, name):
299 self._ops.start_testsuite(name)
305 def end_testsuite(self, name, result, reason=None):
308 if self.xfail_added > 0:
310 if self.fail_added > 0 or self.error_added > 0:
313 if xfail and result in ("fail", "failure"):
316 if self.fail_added > 0 and result != "failure":
319 reason = "Subunit/Filter Reason"
320 reason += "\n failures[%d]" % self.fail_added
322 if self.error_added > 0 and result != "error":
325 reason = "Subunit/Filter Reason"
326 reason += "\n errors[%d]" % self.error_added
328 self._ops.end_testsuite(name, result, reason)
330 def __init__(self, out, prefix, expected_failures, strip_ok_output):
334 self.expected_failures = expected_failures
335 self.strip_ok_output = strip_ok_output