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
24 class TestTAP2SubUnit(unittest.TestCase):
25 """Tests for TAP2SubUnit.
27 These tests test TAP string data in, and subunit string data out.
28 This is ok because the subunit protocol is intended to be stable,
29 but it might be easier/pithier to write tests against TAP string in,
30 parsed subunit objects out (by hooking the subunit stream to a subunit
36 self.subunit = StringIO()
38 def test_skip_entire_file(self):
40 # 1..- # Skipped: comment
41 # results in a single skipped test.
42 self.tap.write("1..0 # Skipped: entire file skipped\n")
44 result = subunit.TAP2SubUnit(self.tap, self.subunit)
45 self.assertEqual(0, result)
49 "Skipped: entire file skipped",
52 self.subunit.getvalue().splitlines())
54 def test_ok_test_pass(self):
57 # results in a passed test with name 'test 1' (a synthetic name as tap
58 # does not require named fixtures - it is the first test in the tap
60 self.tap.write("ok\n")
62 result = subunit.TAP2SubUnit(self.tap, self.subunit)
63 self.assertEqual(0, result)
68 self.subunit.getvalue().splitlines())
70 def test_ok_test_number_pass(self):
73 # results in a passed test with name 'test 1'
74 self.tap.write("ok 1\n")
76 result = subunit.TAP2SubUnit(self.tap, self.subunit)
77 self.assertEqual(0, result)
82 self.subunit.getvalue().splitlines())
84 def test_ok_test_number_description_pass(self):
86 # ok 1 - There is a description
87 # results in a passed test with name 'test 1 - There is a description'
88 self.tap.write("ok 1 - There is a description\n")
90 result = subunit.TAP2SubUnit(self.tap, self.subunit)
91 self.assertEqual(0, result)
93 "test test 1 - There is a description",
94 "success test 1 - There is a description",
96 self.subunit.getvalue().splitlines())
98 def test_ok_test_description_pass(self):
100 # ok There is a description
101 # results in a passed test with name 'test 1 There is a description'
102 self.tap.write("ok There is a description\n")
104 result = subunit.TAP2SubUnit(self.tap, self.subunit)
105 self.assertEqual(0, result)
107 "test test 1 There is a description",
108 "success test 1 There is a description",
110 self.subunit.getvalue().splitlines())
112 def test_ok_SKIP_skip(self):
115 # results in a skkip test with name 'test 1'
116 self.tap.write("ok # SKIP\n")
118 result = subunit.TAP2SubUnit(self.tap, self.subunit)
119 self.assertEqual(0, result)
124 self.subunit.getvalue().splitlines())
126 def test_ok_skip_number_comment_lowercase(self):
127 self.tap.write("ok 1 # skip no samba environment available, skipping compilation\n")
129 result = subunit.TAP2SubUnit(self.tap, self.subunit)
130 self.assertEqual(0, result)
134 "no samba environment available, skipping compilation",
137 self.subunit.getvalue().splitlines())
139 def test_ok_number_description_SKIP_skip_comment(self):
141 # ok 1 foo # SKIP Not done yet
142 # results in a skip test with name 'test 1 foo' and a log of
144 self.tap.write("ok 1 foo # SKIP Not done yet\n")
146 result = subunit.TAP2SubUnit(self.tap, self.subunit)
147 self.assertEqual(0, result)
154 self.subunit.getvalue().splitlines())
156 def test_ok_SKIP_skip_comment(self):
158 # ok # SKIP Not done yet
159 # results in a skip test with name 'test 1' and a log of Not done yet
160 self.tap.write("ok # SKIP Not done yet\n")
162 result = subunit.TAP2SubUnit(self.tap, self.subunit)
163 self.assertEqual(0, result)
170 self.subunit.getvalue().splitlines())
172 def test_ok_TODO_xfail(self):
175 # results in a xfail test with name 'test 1'
176 self.tap.write("ok # TODO\n")
178 result = subunit.TAP2SubUnit(self.tap, self.subunit)
179 self.assertEqual(0, result)
184 self.subunit.getvalue().splitlines())
186 def test_ok_TODO_xfail_comment(self):
188 # ok # TODO Not done yet
189 # results in a xfail test with name 'test 1' and a log of Not done yet
190 self.tap.write("ok # TODO Not done yet\n")
192 result = subunit.TAP2SubUnit(self.tap, self.subunit)
193 self.assertEqual(0, result)
200 self.subunit.getvalue().splitlines())
202 def test_bail_out_errors(self):
203 # A file with line in it
205 # is treated as an error
206 self.tap.write("ok 1 foo\n")
207 self.tap.write("Bail out! Lifejacket engaged\n")
209 result = subunit.TAP2SubUnit(self.tap, self.subunit)
210 self.assertEqual(0, result)
213 "success test 1 foo",
214 "test Bail out! Lifejacket engaged",
215 "error Bail out! Lifejacket engaged",
217 self.subunit.getvalue().splitlines())
219 def test_missing_test_at_end_with_plan_adds_error(self):
224 # results in three tests, with the third being created
225 self.tap.write('1..3\n')
226 self.tap.write('ok first test\n')
227 self.tap.write('not ok second test\n')
229 result = subunit.TAP2SubUnit(self.tap, self.subunit)
230 self.assertEqual(0, result)
232 'test test 1 first test',
233 'success test 1 first test',
234 'test test 2 second test',
235 'failure test 2 second test',
238 'test missing from TAP output',
241 self.subunit.getvalue().splitlines())
243 def test_missing_test_with_plan_adds_error(self):
247 # not ok 3 third test
248 # results in three tests, with the second being created
249 self.tap.write('1..3\n')
250 self.tap.write('ok first test\n')
251 self.tap.write('not ok 3 third test\n')
253 result = subunit.TAP2SubUnit(self.tap, self.subunit)
254 self.assertEqual(0, result)
256 'test test 1 first test',
257 'success test 1 first test',
260 'test missing from TAP output',
262 'test test 3 third test',
263 'failure test 3 third test',
265 self.subunit.getvalue().splitlines())
267 def test_missing_test_no_plan_adds_error(self):
270 # not ok 3 third test
271 # results in three tests, with the second being created
272 self.tap.write('ok first test\n')
273 self.tap.write('not ok 3 third test\n')
275 result = subunit.TAP2SubUnit(self.tap, self.subunit)
276 self.assertEqual(0, result)
278 'test test 1 first test',
279 'success test 1 first test',
282 'test missing from TAP output',
284 'test test 3 third test',
285 'failure test 3 third test',
287 self.subunit.getvalue().splitlines())
289 def test_four_tests_in_a_row_trailing_plan(self):
291 # ok 1 - first test in a script with no plan at all
296 # results in four tests numbered and named
297 self.tap.write('ok 1 - first test in a script with trailing plan\n')
298 self.tap.write('not ok 2 - second\n')
299 self.tap.write('ok 3 - third\n')
300 self.tap.write('not ok 4 - fourth\n')
301 self.tap.write('1..4\n')
303 result = subunit.TAP2SubUnit(self.tap, self.subunit)
304 self.assertEqual(0, result)
306 'test test 1 - first test in a script with trailing plan',
307 'success test 1 - first test in a script with trailing plan',
308 'test test 2 - second',
309 'failure test 2 - second',
310 'test test 3 - third',
311 'success test 3 - third',
312 'test test 4 - fourth',
313 'failure test 4 - fourth'
315 self.subunit.getvalue().splitlines())
317 def test_four_tests_in_a_row_with_plan(self):
320 # ok 1 - first test in a script with no plan at all
324 # results in four tests numbered and named
325 self.tap.write('1..4\n')
326 self.tap.write('ok 1 - first test in a script with a plan\n')
327 self.tap.write('not ok 2 - second\n')
328 self.tap.write('ok 3 - third\n')
329 self.tap.write('not ok 4 - fourth\n')
331 result = subunit.TAP2SubUnit(self.tap, self.subunit)
332 self.assertEqual(0, result)
334 'test test 1 - first test in a script with a plan',
335 'success test 1 - first test in a script with a plan',
336 'test test 2 - second',
337 'failure test 2 - second',
338 'test test 3 - third',
339 'success test 3 - third',
340 'test test 4 - fourth',
341 'failure test 4 - fourth'
343 self.subunit.getvalue().splitlines())
345 def test_four_tests_in_a_row_no_plan(self):
347 # ok 1 - first test in a script with no plan at all
351 # results in four tests numbered and named
352 self.tap.write('ok 1 - first test in a script with no plan at all\n')
353 self.tap.write('not ok 2 - second\n')
354 self.tap.write('ok 3 - third\n')
355 self.tap.write('not ok 4 - fourth\n')
357 result = subunit.TAP2SubUnit(self.tap, self.subunit)
358 self.assertEqual(0, result)
360 'test test 1 - first test in a script with no plan at all',
361 'success test 1 - first test in a script with no plan at all',
362 'test test 2 - second',
363 'failure test 2 - second',
364 'test test 3 - third',
365 'success test 3 - third',
366 'test test 4 - fourth',
367 'failure test 4 - fourth'
369 self.subunit.getvalue().splitlines())
371 def test_todo_and_skip(self):
373 # not ok 1 - a fail but # TODO but is TODO
374 # not ok 2 - another fail # SKIP instead
375 # results in two tests, numbered and commented.
376 self.tap.write("not ok 1 - a fail but # TODO but is TODO\n")
377 self.tap.write("not ok 2 - another fail # SKIP instead\n")
379 result = subunit.TAP2SubUnit(self.tap, self.subunit)
380 self.assertEqual(0, result)
382 'test test 1 - a fail but',
383 'xfail test 1 - a fail but [',
386 'test test 2 - another fail',
387 'skip test 2 - another fail [',
391 self.subunit.getvalue().splitlines())
393 def test_leading_comments_add_to_next_test_log(self):
398 # results in a single test with the comment included
399 # in the first test and not the second.
400 self.tap.write("# comment\n")
401 self.tap.write("ok\n")
402 self.tap.write("ok\n")
404 result = subunit.TAP2SubUnit(self.tap, self.subunit)
405 self.assertEqual(0, result)
414 self.subunit.getvalue().splitlines())
416 def test_trailing_comments_are_included_in_last_test_log(self):
421 # results in a two tests, with the second having the comment
422 # attached to its log.
423 self.tap.write("ok\n")
424 self.tap.write("ok\n")
425 self.tap.write("# comment\n")
427 result = subunit.TAP2SubUnit(self.tap, self.subunit)
428 self.assertEqual(0, result)
437 self.subunit.getvalue().splitlines())
441 loader = subunit.tests.TestUtil.TestLoader()
442 result = loader.loadTestsFromName(__name__)