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(subunit.RemotedTestCase(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.addError(subunit.RemotedTestCase(testname), "reason (%s) interrupted" % result)
86 if result in ("success", "successful"):
88 open_tests.remove(testname)
90 statistics['TESTS_ERROR']+=1
91 msg_ops.addError(subunit.RemotedTestCase(testname), "Test was never started")
93 statistics['TESTS_EXPECTED_OK']+=1
94 msg_ops.end_test(testname, "success", False, reason)
95 elif result in ("xfail", "knownfail"):
97 open_tests.remove(testname)
99 statistics['TESTS_ERROR']+=1
100 msg_ops.addError(subunit.RemotedTestCase(testname), "Test was never started")
102 statistics['TESTS_EXPECTED_FAIL']+=1
103 msg_ops.end_test(testname, "xfail", False, reason)
105 elif result in ("failure", "fail"):
107 open_tests.remove(testname)
109 statistics['TESTS_ERROR']+=1
110 msg_ops.addError(subunit.RemotedTestCase(testname), "Test was never started")
112 statistics['TESTS_UNEXPECTED_FAIL']+=1
113 msg_ops.end_test(testname, "failure", True, reason)
114 elif result == "skip":
115 statistics['TESTS_SKIP']+=1
116 # Allow tests to be skipped without prior announcement of test
117 last = open_tests.pop()
118 if last is not None and last != testname:
119 open_tests.append(testname)
120 msg_ops.end_test(testname, "skip", False, reason)
121 elif result == "error":
122 statistics['TESTS_ERROR']+=1
124 open_tests.remove(testname)
127 msg_ops.addError(subunit.RemotedTestCase(testname), reason)
128 elif result == "skip-testsuite":
129 msg_ops.skip_testsuite(testname)
130 elif result == "testsuite-success":
131 msg_ops.end_testsuite(testname, "success", reason)
132 elif result == "testsuite-failure":
133 msg_ops.end_testsuite(testname, "failure", reason)
134 elif result == "testsuite-xfail":
135 msg_ops.end_testsuite(testname, "xfail", reason)
136 elif result == "testsuite-error":
137 msg_ops.end_testsuite(testname, "error", reason)
139 raise AssertionError("Recognized but unhandled result %r" %
141 elif command == "testsuite":
142 msg_ops.start_testsuite(arg.strip())
143 elif command == "progress":
146 msg_ops.progress(None, subunit.PROGRESS_POP)
148 msg_ops.progress(None, subunit.PROGRESS_PUSH)
150 msg_ops.progress(int(arg), subunit.PROGRESS_CUR)
152 msg_ops.progress(int(arg), subunit.PROGRESS_SET)
154 msg_ops.output_msg(l)
157 msg_ops.end_test(open_tests.pop(), "error", True,
158 "was started but never finished!")
159 statistics['TESTS_ERROR']+=1
161 if statistics['TESTS_ERROR'] > 0:
163 if statistics['TESTS_UNEXPECTED_FAIL'] > 0:
168 class SubunitOps(subunit.TestProtocolClient,TestsuiteEnabledTestResult):
170 def addError(self, test, details=None):
171 self.end_test(test.id(), "error", details)
173 def end_test(self, name, result, reason=None):
175 self._stream.write("%s: %s [\n%s\n]\n" % (result, name, reason))
177 self._stream.write("%s: %s\n" % (result, name))
179 def skip_test(self, name, reason=None):
180 self.end_test(name, "skip", reason)
182 def fail_test(self, name, reason=None):
183 self.end_test(name, "fail", reason)
185 def success_test(self, name, reason=None):
186 self.end_test(name, "success", reason)
188 def xfail_test(self, name, reason=None):
189 self.end_test(name, "xfail", reason)
191 # The following are Samba extensions:
192 def start_testsuite(self, name):
193 self._stream.write("testsuite: %s\n" % name)
195 def skip_testsuite(self, name, reason=None):
197 self._stream.write("skip-testsuite: %s [\n%s\n]\n" % (name, reason))
199 self._stream.write("skip-testsuite: %s\n" % name)
201 def end_testsuite(self, name, result, reason=None):
203 self._stream.write("testsuite-%s: %s [\n%s\n]\n" % (result, name, reason))
205 self._stream.write("testsuite-%s: %s\n" % (result, name))
208 def read_test_regexes(name):
214 if l == "" or l[0] == "#":
217 (regex, reason) = l.split("#", 1)
218 ret[regex.strip()] = reason.strip()
226 def find_in_list(regexes, fullname):
227 for regex, reason in regexes.iteritems():
228 if re.match(regex, fullname):
235 class FilterOps(testtools.testresult.TestResult):
237 def control_msg(self, msg):
238 pass # We regenerate control messages, so ignore this
240 def time(self, time):
243 def progress(self, delta, whence):
244 self._ops.progress(delta, whence)
246 def output_msg(self, msg):
247 if self.output is None:
248 sys.stdout.write(msg)
252 def startTest(self, test):
253 if self.prefix is not None:
254 test = subunit.RemotedTestCase(self.prefix + test.id())
256 if self.strip_ok_output:
259 self._ops.startTest(test)
261 def addError(self, test, details=None):
262 self.end_test(test.id(), "error", details)
264 def end_test(self, testname, result, unexpected, reason):
265 if self.prefix is not None:
266 testname = self.prefix + testname
268 if result in ("fail", "failure") and not unexpected:
272 xfail_reason = find_in_list(self.expected_failures, testname)
273 if xfail_reason is not None and result in ("fail", "failure"):
277 reason += xfail_reason
279 if result in ("fail", "failure"):
283 if result == "error":
287 if self.strip_ok_output:
288 if result not in ("success", "xfail", "skip"):
292 self._ops.end_test(testname, result, reason)
294 def skip_testsuite(self, name, reason=None):
295 self._ops.skip_testsuite(name, reason)
297 def start_testsuite(self, name):
298 self._ops.start_testsuite(name)
304 def end_testsuite(self, name, result, reason=None):
307 if self.xfail_added > 0:
309 if self.fail_added > 0 or self.error_added > 0:
312 if xfail and result in ("fail", "failure"):
315 if self.fail_added > 0 and result != "failure":
318 reason = "Subunit/Filter Reason"
319 reason += "\n failures[%d]" % self.fail_added
321 if self.error_added > 0 and result != "error":
324 reason = "Subunit/Filter Reason"
325 reason += "\n errors[%d]" % self.error_added
327 self._ops.end_testsuite(name, result, reason)
329 def __init__(self, out, prefix, expected_failures, strip_ok_output):
333 self.expected_failures = expected_failures
334 self.strip_ok_output = strip_ok_output