2 # subunit: extensions to python unittest to get test results from subprocesses.
3 # Copyright (C) 2005 Robert Collins <robertc@robertcollins.net>
5 # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
6 # license at the users choice. A copy of both licenses are available in the
7 # project source as Apache-2.0 and BSD. You may not use this file except in
8 # compliance with one of these two licences.
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # license you chose for the specific language governing permissions and
14 # limitations under that license.
17 """Tests for TAP2SubUnit."""
20 from StringIO import StringIO
26 class TestTAP2SubUnit(unittest.TestCase):
27 """Tests for TAP2SubUnit.
29 These tests test TAP string data in, and subunit string data out.
30 This is ok because the subunit protocol is intended to be stable,
31 but it might be easier/pithier to write tests against TAP string in,
32 parsed subunit objects out (by hooking the subunit stream to a subunit
38 self.subunit = StringIO()
40 def test_skip_entire_file(self):
42 # 1..- # Skipped: comment
43 # results in a single skipped test.
44 self.tap.write("1..0 # Skipped: entire file skipped\n")
46 result = subunit.TAP2SubUnit(self.tap, self.subunit)
47 self.assertEqual(0, result)
51 "Skipped: entire file skipped",
54 self.subunit.getvalue().splitlines())
56 def test_ok_test_pass(self):
59 # results in a passed test with name 'test 1' (a synthetic name as tap
60 # does not require named fixtures - it is the first test in the tap
62 self.tap.write("ok\n")
64 result = subunit.TAP2SubUnit(self.tap, self.subunit)
65 self.assertEqual(0, result)
70 self.subunit.getvalue().splitlines())
72 def test_ok_test_number_pass(self):
75 # results in a passed test with name 'test 1'
76 self.tap.write("ok 1\n")
78 result = subunit.TAP2SubUnit(self.tap, self.subunit)
79 self.assertEqual(0, result)
84 self.subunit.getvalue().splitlines())
86 def test_ok_test_number_description_pass(self):
88 # ok 1 - There is a description
89 # results in a passed test with name 'test 1 - There is a description'
90 self.tap.write("ok 1 - There is a description\n")
92 result = subunit.TAP2SubUnit(self.tap, self.subunit)
93 self.assertEqual(0, result)
95 "test test 1 - There is a description",
96 "success test 1 - There is a description",
98 self.subunit.getvalue().splitlines())
100 def test_ok_test_description_pass(self):
102 # ok There is a description
103 # results in a passed test with name 'test 1 There is a description'
104 self.tap.write("ok There is a description\n")
106 result = subunit.TAP2SubUnit(self.tap, self.subunit)
107 self.assertEqual(0, result)
109 "test test 1 There is a description",
110 "success test 1 There is a description",
112 self.subunit.getvalue().splitlines())
114 def test_ok_SKIP_skip(self):
117 # results in a skkip test with name 'test 1'
118 self.tap.write("ok # SKIP\n")
120 result = subunit.TAP2SubUnit(self.tap, self.subunit)
121 self.assertEqual(0, result)
126 self.subunit.getvalue().splitlines())
128 def test_ok_number_description_SKIP_skip_comment(self):
130 # ok 1 foo # SKIP Not done yet
131 # results in a skip test with name 'test 1 foo' and a log of
133 self.tap.write("ok 1 foo # SKIP Not done yet\n")
135 result = subunit.TAP2SubUnit(self.tap, self.subunit)
136 self.assertEqual(0, result)
143 self.subunit.getvalue().splitlines())
145 def test_ok_SKIP_skip_comment(self):
147 # ok # SKIP Not done yet
148 # results in a skip test with name 'test 1' and a log of Not done yet
149 self.tap.write("ok # SKIP Not done yet\n")
151 result = subunit.TAP2SubUnit(self.tap, self.subunit)
152 self.assertEqual(0, result)
159 self.subunit.getvalue().splitlines())
161 def test_ok_TODO_xfail(self):
164 # results in a xfail test with name 'test 1'
165 self.tap.write("ok # TODO\n")
167 result = subunit.TAP2SubUnit(self.tap, self.subunit)
168 self.assertEqual(0, result)
173 self.subunit.getvalue().splitlines())
175 def test_ok_TODO_xfail_comment(self):
177 # ok # TODO Not done yet
178 # results in a xfail test with name 'test 1' and a log of Not done yet
179 self.tap.write("ok # TODO Not done yet\n")
181 result = subunit.TAP2SubUnit(self.tap, self.subunit)
182 self.assertEqual(0, result)
189 self.subunit.getvalue().splitlines())
191 def test_bail_out_errors(self):
192 # A file with line in it
194 # is treated as an error
195 self.tap.write("ok 1 foo\n")
196 self.tap.write("Bail out! Lifejacket engaged\n")
198 result = subunit.TAP2SubUnit(self.tap, self.subunit)
199 self.assertEqual(0, result)
202 "success test 1 foo",
203 "test Bail out! Lifejacket engaged",
204 "error Bail out! Lifejacket engaged",
206 self.subunit.getvalue().splitlines())
208 def test_missing_test_at_end_with_plan_adds_error(self):
213 # results in three tests, with the third being created
214 self.tap.write('1..3\n')
215 self.tap.write('ok first test\n')
216 self.tap.write('not ok second test\n')
218 result = subunit.TAP2SubUnit(self.tap, self.subunit)
219 self.assertEqual(0, result)
221 'test test 1 first test',
222 'success test 1 first test',
223 'test test 2 second test',
224 'failure test 2 second test',
227 'test missing from TAP output',
230 self.subunit.getvalue().splitlines())
232 def test_missing_test_with_plan_adds_error(self):
236 # not ok 3 third test
237 # results in three tests, with the second being created
238 self.tap.write('1..3\n')
239 self.tap.write('ok first test\n')
240 self.tap.write('not ok 3 third test\n')
242 result = subunit.TAP2SubUnit(self.tap, self.subunit)
243 self.assertEqual(0, result)
245 'test test 1 first test',
246 'success test 1 first test',
249 'test missing from TAP output',
251 'test test 3 third test',
252 'failure test 3 third test',
254 self.subunit.getvalue().splitlines())
256 def test_missing_test_no_plan_adds_error(self):
259 # not ok 3 third test
260 # results in three tests, with the second being created
261 self.tap.write('ok first test\n')
262 self.tap.write('not ok 3 third test\n')
264 result = subunit.TAP2SubUnit(self.tap, self.subunit)
265 self.assertEqual(0, result)
267 'test test 1 first test',
268 'success test 1 first test',
271 'test missing from TAP output',
273 'test test 3 third test',
274 'failure test 3 third test',
276 self.subunit.getvalue().splitlines())
278 def test_four_tests_in_a_row_trailing_plan(self):
280 # ok 1 - first test in a script with no plan at all
285 # results in four tests numbered and named
286 self.tap.write('ok 1 - first test in a script with trailing plan\n')
287 self.tap.write('not ok 2 - second\n')
288 self.tap.write('ok 3 - third\n')
289 self.tap.write('not ok 4 - fourth\n')
290 self.tap.write('1..4\n')
292 result = subunit.TAP2SubUnit(self.tap, self.subunit)
293 self.assertEqual(0, result)
295 'test test 1 - first test in a script with trailing plan',
296 'success test 1 - first test in a script with trailing plan',
297 'test test 2 - second',
298 'failure test 2 - second',
299 'test test 3 - third',
300 'success test 3 - third',
301 'test test 4 - fourth',
302 'failure test 4 - fourth'
304 self.subunit.getvalue().splitlines())
306 def test_four_tests_in_a_row_with_plan(self):
309 # ok 1 - first test in a script with no plan at all
313 # results in four tests numbered and named
314 self.tap.write('1..4\n')
315 self.tap.write('ok 1 - first test in a script with a plan\n')
316 self.tap.write('not ok 2 - second\n')
317 self.tap.write('ok 3 - third\n')
318 self.tap.write('not ok 4 - fourth\n')
320 result = subunit.TAP2SubUnit(self.tap, self.subunit)
321 self.assertEqual(0, result)
323 'test test 1 - first test in a script with a plan',
324 'success test 1 - first test in a script with a plan',
325 'test test 2 - second',
326 'failure test 2 - second',
327 'test test 3 - third',
328 'success test 3 - third',
329 'test test 4 - fourth',
330 'failure test 4 - fourth'
332 self.subunit.getvalue().splitlines())
334 def test_four_tests_in_a_row_no_plan(self):
336 # ok 1 - first test in a script with no plan at all
340 # results in four tests numbered and named
341 self.tap.write('ok 1 - first test in a script with no plan at all\n')
342 self.tap.write('not ok 2 - second\n')
343 self.tap.write('ok 3 - third\n')
344 self.tap.write('not ok 4 - fourth\n')
346 result = subunit.TAP2SubUnit(self.tap, self.subunit)
347 self.assertEqual(0, result)
349 'test test 1 - first test in a script with no plan at all',
350 'success test 1 - first test in a script with no plan at all',
351 'test test 2 - second',
352 'failure test 2 - second',
353 'test test 3 - third',
354 'success test 3 - third',
355 'test test 4 - fourth',
356 'failure test 4 - fourth'
358 self.subunit.getvalue().splitlines())
360 def test_todo_and_skip(self):
362 # not ok 1 - a fail but # TODO but is TODO
363 # not ok 2 - another fail # SKIP instead
364 # results in two tests, numbered and commented.
365 self.tap.write("not ok 1 - a fail but # TODO but is TODO\n")
366 self.tap.write("not ok 2 - another fail # SKIP instead\n")
368 result = subunit.TAP2SubUnit(self.tap, self.subunit)
369 self.assertEqual(0, result)
371 'test test 1 - a fail but',
372 'xfail test 1 - a fail but [',
375 'test test 2 - another fail',
376 'skip test 2 - another fail [',
380 self.subunit.getvalue().splitlines())
382 def test_leading_comments_add_to_next_test_log(self):
387 # results in a single test with the comment included
388 # in the first test and not the second.
389 self.tap.write("# comment\n")
390 self.tap.write("ok\n")
391 self.tap.write("ok\n")
393 result = subunit.TAP2SubUnit(self.tap, self.subunit)
394 self.assertEqual(0, result)
403 self.subunit.getvalue().splitlines())
405 def test_trailing_comments_are_included_in_last_test_log(self):
410 # results in a two tests, with the second having the comment
411 # attached to its log.
412 self.tap.write("ok\n")
413 self.tap.write("ok\n")
414 self.tap.write("# comment\n")
416 result = subunit.TAP2SubUnit(self.tap, self.subunit)
417 self.assertEqual(0, result)
426 self.subunit.getvalue().splitlines())
430 loader = subunit.tests.TestUtil.TestLoader()
431 result = loader.loadTestsFromName(__name__)