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)
51 test = subunit.RemotedTestCase(name)
52 if name in open_tests:
53 msg_ops.addError(open_tests.pop(name), "Test already running")
54 msg_ops.startTest(test)
55 open_tests[name] = test
56 elif command == "time":
57 msg_ops.control_msg(l)
59 dt = subunit.iso8601.parse_date(arg.rstrip("\n"))
61 print "Unable to parse time line: %s" % arg.rstrip("\n")
64 elif command in VALID_RESULTS:
65 msg_ops.control_msg(l)
67 grp = re.match("(.*?)( \[)?([ \t]*)( multipart)?\n", arg)
68 (testname, hasreason) = (grp.group(1), grp.group(2))
71 # reason may be specified in next lines
77 msg_ops.control_msg(l)
85 statistics['TESTS_ERROR']+=1
86 msg_ops.addError(subunit.RemotedTestCase(testname), "reason (%s) interrupted" % result)
90 if result in ("success", "successful"):
92 test = open_tests.pop(testname)
94 statistics['TESTS_ERROR']+=1
95 msg_ops.addError(subunit.RemotedTestCase(testname), "Test was never started")
97 statistics['TESTS_EXPECTED_OK']+=1
98 msg_ops.addSuccess(test, reason)
99 elif result in ("xfail", "knownfail"):
101 test = open_tests.pop(testname)
103 statistics['TESTS_ERROR']+=1
104 msg_ops.addError(subunit.RemotedTestCase(testname), "Test was never started")
106 statistics['TESTS_EXPECTED_FAIL']+=1
107 msg_ops.addExpectedFail(test, reason)
109 elif result in ("failure", "fail"):
111 test = open_tests.pop(testname)
113 statistics['TESTS_ERROR']+=1
114 msg_ops.addError(subunit.RemotedTestCase(testname), "Test was never started")
116 statistics['TESTS_UNEXPECTED_FAIL']+=1
117 msg_ops.addFailure(test, reason)
118 elif result == "skip":
119 statistics['TESTS_SKIP']+=1
120 # Allow tests to be skipped without prior announcement of test
122 test = open_tests.pop(testname)
124 test = subunit.RemotedTestCase(testname)
125 msg_ops.addSkip(test, reason)
126 elif result == "error":
127 statistics['TESTS_ERROR']+=1
129 test = open_tests.pop(testname)
131 test = subunit.RemotedTestCase(testname)
132 msg_ops.addError(test, reason)
133 elif result == "skip-testsuite":
134 msg_ops.skip_testsuite(testname)
135 elif result == "testsuite-success":
136 msg_ops.end_testsuite(testname, "success", reason)
137 elif result == "testsuite-failure":
138 msg_ops.end_testsuite(testname, "failure", reason)
139 elif result == "testsuite-xfail":
140 msg_ops.end_testsuite(testname, "xfail", reason)
141 elif result == "testsuite-error":
142 msg_ops.end_testsuite(testname, "error", reason)
144 raise AssertionError("Recognized but unhandled result %r" %
146 elif command == "testsuite":
147 msg_ops.start_testsuite(arg.strip())
148 elif command == "progress":
151 msg_ops.progress(None, subunit.PROGRESS_POP)
153 msg_ops.progress(None, subunit.PROGRESS_PUSH)
155 msg_ops.progress(int(arg), subunit.PROGRESS_CUR)
157 msg_ops.progress(int(arg), subunit.PROGRESS_SET)
159 msg_ops.output_msg(l)
162 test = subunit.RemotedTestCase(open_tests.popitem()[1])
163 msg_ops.addError(test, "was started but never finished!")
164 statistics['TESTS_ERROR']+=1
166 if statistics['TESTS_ERROR'] > 0:
168 if statistics['TESTS_UNEXPECTED_FAIL'] > 0:
173 class SubunitOps(subunit.TestProtocolClient,TestsuiteEnabledTestResult):
175 def addError(self, test, details=None):
176 self.end_test(test.id(), "error", details)
178 def addSuccess(self, test, details=None):
179 self.end_test(test.id(), "success", details)
181 def addExpectedFail(self, test, details=None):
182 self.end_test(test.id(), "xfail", details)
184 def addFailure(self, test, details=None):
185 self.end_test(test.id(), "failure", details)
187 def addSkip(self, test, details=None):
188 self.end_test(test.id(), "skip", details)
190 def end_test(self, name, result, reason=None):
192 self._stream.write("%s: %s [\n%s\n]\n" % (result, name, reason))
194 self._stream.write("%s: %s\n" % (result, name))
196 # The following are Samba extensions:
197 def start_testsuite(self, name):
198 self._stream.write("testsuite: %s\n" % name)
200 def skip_testsuite(self, name, reason=None):
202 self._stream.write("skip-testsuite: %s [\n%s\n]\n" % (name, reason))
204 self._stream.write("skip-testsuite: %s\n" % name)
206 def end_testsuite(self, name, result, reason=None):
208 self._stream.write("testsuite-%s: %s [\n%s\n]\n" % (result, name, reason))
210 self._stream.write("testsuite-%s: %s\n" % (result, name))
213 def read_test_regexes(name):
219 if l == "" or l[0] == "#":
222 (regex, reason) = l.split("#", 1)
223 ret[regex.strip()] = reason.strip()
231 def find_in_list(regexes, fullname):
232 for regex, reason in regexes.iteritems():
233 if re.match(regex, fullname):
240 class FilterOps(testtools.testresult.TestResult):
242 def control_msg(self, msg):
243 pass # We regenerate control messages, so ignore this
245 def time(self, time):
248 def progress(self, delta, whence):
249 self._ops.progress(delta, whence)
251 def output_msg(self, msg):
252 if self.output is None:
253 sys.stdout.write(msg)
257 def startTest(self, test):
258 test = self._add_prefix(test)
259 if self.strip_ok_output:
262 self._ops.startTest(test)
264 def _add_prefix(self, test):
265 if self.prefix is not None:
266 return subunit.RemotedTestCase(self.prefix + test.id())
270 def addError(self, test, details=None):
271 test = self._add_prefix(test)
272 self.end_test(test.id(), "error", True, details)
274 def addSkip(self, test, details=None):
275 test = self._add_prefix(test)
276 self.end_test(test.id(), "skip", False, details)
278 def addExpectedFail(self, test, details=None):
279 test = self._add_prefix(test)
280 self.end_test(test.id(), "xfail", False, details)
282 def addFailure(self, test, details=None):
283 test = self._add_prefix(test)
284 self.end_test(test.id(), "failure", True, details)
286 def addSuccess(self, test, details=None):
287 test = self._add_prefix(test)
288 self.end_test(test.id(), "success", False, details)
290 def end_test(self, testname, result, unexpected, reason):
291 if result in ("fail", "failure") and not unexpected:
295 xfail_reason = find_in_list(self.expected_failures, testname)
296 if xfail_reason is not None and result in ("fail", "failure"):
300 reason += xfail_reason
302 if result in ("fail", "failure"):
306 if result == "error":
310 if self.strip_ok_output:
311 if result not in ("success", "xfail", "skip"):
315 self._ops.end_test(testname, result, reason)
317 def skip_testsuite(self, name, reason=None):
318 self._ops.skip_testsuite(name, reason)
320 def start_testsuite(self, name):
321 self._ops.start_testsuite(name)
327 def end_testsuite(self, name, result, reason=None):
330 if self.xfail_added > 0:
332 if self.fail_added > 0 or self.error_added > 0:
335 if xfail and result in ("fail", "failure"):
338 if self.fail_added > 0 and result != "failure":
341 reason = "Subunit/Filter Reason"
342 reason += "\n failures[%d]" % self.fail_added
344 if self.error_added > 0 and result != "error":
347 reason = "Subunit/Filter Reason"
348 reason += "\n errors[%d]" % self.error_added
350 self._ops.end_testsuite(name, result, reason)
352 def __init__(self, out, prefix, expected_failures, strip_ok_output):
356 self.expected_failures = expected_failures
357 self.strip_ok_output = strip_ok_output