subunit: Import new version.
[ira/wip.git] / lib / subunit / python / subunit / tests / test_tap2subunit.py
1 #
2 #  subunit: extensions to python unittest to get test results from subprocesses.
3 #  Copyright (C) 2005  Robert Collins <robertc@robertcollins.net>
4 #
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.
9 #  
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.
15 #
16
17 """Tests for TAP2SubUnit."""
18
19 import unittest
20 from StringIO import StringIO
21 import os
22 import subunit
23 import sys
24
25
26 class TestTAP2SubUnit(unittest.TestCase):
27     """Tests for TAP2SubUnit.
28
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
33     protocol server.
34     """
35
36     def setUp(self):
37         self.tap = StringIO()
38         self.subunit = StringIO()
39
40     def test_skip_entire_file(self):
41         # A file
42         # 1..- # Skipped: comment
43         # results in a single skipped test.
44         self.tap.write("1..0 # Skipped: entire file skipped\n")
45         self.tap.seek(0)
46         result = subunit.TAP2SubUnit(self.tap, self.subunit)
47         self.assertEqual(0, result)
48         self.assertEqual([
49             "test file skip",
50             "skip file skip [",
51             "Skipped: entire file skipped",
52             "]",
53             ],
54             self.subunit.getvalue().splitlines())
55
56     def test_ok_test_pass(self):
57         # A file
58         # ok
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
61         # stream).
62         self.tap.write("ok\n")
63         self.tap.seek(0)
64         result = subunit.TAP2SubUnit(self.tap, self.subunit)
65         self.assertEqual(0, result)
66         self.assertEqual([
67             "test test 1",
68             "success test 1",
69             ],
70             self.subunit.getvalue().splitlines())
71
72     def test_ok_test_number_pass(self):
73         # A file
74         # ok 1
75         # results in a passed test with name 'test 1'
76         self.tap.write("ok 1\n")
77         self.tap.seek(0)
78         result = subunit.TAP2SubUnit(self.tap, self.subunit)
79         self.assertEqual(0, result)
80         self.assertEqual([
81             "test test 1",
82             "success test 1",
83             ],
84             self.subunit.getvalue().splitlines())
85
86     def test_ok_test_number_description_pass(self):
87         # A file
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")
91         self.tap.seek(0)
92         result = subunit.TAP2SubUnit(self.tap, self.subunit)
93         self.assertEqual(0, result)
94         self.assertEqual([
95             "test test 1 - There is a description",
96             "success test 1 - There is a description",
97             ],
98             self.subunit.getvalue().splitlines())
99
100     def test_ok_test_description_pass(self):
101         # A file
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")
105         self.tap.seek(0)
106         result = subunit.TAP2SubUnit(self.tap, self.subunit)
107         self.assertEqual(0, result)
108         self.assertEqual([
109             "test test 1 There is a description",
110             "success test 1 There is a description",
111             ],
112             self.subunit.getvalue().splitlines())
113
114     def test_ok_SKIP_skip(self):
115         # A file
116         # ok # SKIP
117         # results in a skkip test with name 'test 1'
118         self.tap.write("ok # SKIP\n")
119         self.tap.seek(0)
120         result = subunit.TAP2SubUnit(self.tap, self.subunit)
121         self.assertEqual(0, result)
122         self.assertEqual([
123             "test test 1",
124             "skip test 1",
125             ],
126             self.subunit.getvalue().splitlines())
127
128     def test_ok_number_description_SKIP_skip_comment(self):
129         # A file
130         # ok 1 foo  # SKIP Not done yet
131         # results in a skip test with name 'test 1 foo' and a log of
132         # Not done yet
133         self.tap.write("ok 1 foo  # SKIP Not done yet\n")
134         self.tap.seek(0)
135         result = subunit.TAP2SubUnit(self.tap, self.subunit)
136         self.assertEqual(0, result)
137         self.assertEqual([
138             "test test 1 foo",
139             "skip test 1 foo [",
140             "Not done yet",
141             "]",
142             ],
143             self.subunit.getvalue().splitlines())
144
145     def test_ok_SKIP_skip_comment(self):
146         # A file
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")
150         self.tap.seek(0)
151         result = subunit.TAP2SubUnit(self.tap, self.subunit)
152         self.assertEqual(0, result)
153         self.assertEqual([
154             "test test 1",
155             "skip test 1 [",
156             "Not done yet",
157             "]",
158             ],
159             self.subunit.getvalue().splitlines())
160
161     def test_ok_TODO_xfail(self):
162         # A file
163         # ok # TODO
164         # results in a xfail test with name 'test 1'
165         self.tap.write("ok # TODO\n")
166         self.tap.seek(0)
167         result = subunit.TAP2SubUnit(self.tap, self.subunit)
168         self.assertEqual(0, result)
169         self.assertEqual([
170             "test test 1",
171             "xfail test 1",
172             ],
173             self.subunit.getvalue().splitlines())
174
175     def test_ok_TODO_xfail_comment(self):
176         # A file
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")
180         self.tap.seek(0)
181         result = subunit.TAP2SubUnit(self.tap, self.subunit)
182         self.assertEqual(0, result)
183         self.assertEqual([
184             "test test 1",
185             "xfail test 1 [",
186             "Not done yet",
187             "]",
188             ],
189             self.subunit.getvalue().splitlines())
190
191     def test_bail_out_errors(self):
192         # A file with line in it
193         # Bail out! COMMENT
194         # is treated as an error
195         self.tap.write("ok 1 foo\n")
196         self.tap.write("Bail out! Lifejacket engaged\n")
197         self.tap.seek(0)
198         result = subunit.TAP2SubUnit(self.tap, self.subunit)
199         self.assertEqual(0, result)
200         self.assertEqual([
201             "test test 1 foo",
202             "success test 1 foo",
203             "test Bail out! Lifejacket engaged",
204             "error Bail out! Lifejacket engaged",
205             ],
206             self.subunit.getvalue().splitlines())
207
208     def test_missing_test_at_end_with_plan_adds_error(self):
209         # A file
210         # 1..3
211         # ok first test
212         # not ok third test
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')
217         self.tap.seek(0)
218         result = subunit.TAP2SubUnit(self.tap, self.subunit)
219         self.assertEqual(0, result)
220         self.assertEqual([
221             'test test 1 first test',
222             'success test 1 first test',
223             'test test 2 second test',
224             'failure test 2 second test',
225             'test test 3',
226             'error test 3 [',
227             'test missing from TAP output',
228             ']',
229             ],
230             self.subunit.getvalue().splitlines())
231
232     def test_missing_test_with_plan_adds_error(self):
233         # A file
234         # 1..3
235         # ok first test
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')
241         self.tap.seek(0)
242         result = subunit.TAP2SubUnit(self.tap, self.subunit)
243         self.assertEqual(0, result)
244         self.assertEqual([
245             'test test 1 first test',
246             'success test 1 first test',
247             'test test 2',
248             'error test 2 [',
249             'test missing from TAP output',
250             ']',
251             'test test 3 third test',
252             'failure test 3 third test',
253             ],
254             self.subunit.getvalue().splitlines())
255
256     def test_missing_test_no_plan_adds_error(self):
257         # A file
258         # ok first test
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')
263         self.tap.seek(0)
264         result = subunit.TAP2SubUnit(self.tap, self.subunit)
265         self.assertEqual(0, result)
266         self.assertEqual([
267             'test test 1 first test',
268             'success test 1 first test',
269             'test test 2',
270             'error test 2 [',
271             'test missing from TAP output',
272             ']',
273             'test test 3 third test',
274             'failure test 3 third test',
275             ],
276             self.subunit.getvalue().splitlines())
277
278     def test_four_tests_in_a_row_trailing_plan(self):
279         # A file
280         # ok 1 - first test in a script with no plan at all
281         # not ok 2 - second
282         # ok 3 - third
283         # not ok 4 - fourth
284         # 1..4
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')
291         self.tap.seek(0)
292         result = subunit.TAP2SubUnit(self.tap, self.subunit)
293         self.assertEqual(0, result)
294         self.assertEqual([
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'
303             ],
304             self.subunit.getvalue().splitlines())
305
306     def test_four_tests_in_a_row_with_plan(self):
307         # A file
308         # 1..4
309         # ok 1 - first test in a script with no plan at all
310         # not ok 2 - second
311         # ok 3 - third
312         # not ok 4 - fourth
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')
319         self.tap.seek(0)
320         result = subunit.TAP2SubUnit(self.tap, self.subunit)
321         self.assertEqual(0, result)
322         self.assertEqual([
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'
331             ],
332             self.subunit.getvalue().splitlines())
333
334     def test_four_tests_in_a_row_no_plan(self):
335         # A file
336         # ok 1 - first test in a script with no plan at all
337         # not ok 2 - second
338         # ok 3 - third
339         # not ok 4 - fourth
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')
345         self.tap.seek(0)
346         result = subunit.TAP2SubUnit(self.tap, self.subunit)
347         self.assertEqual(0, result)
348         self.assertEqual([
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'
357             ],
358             self.subunit.getvalue().splitlines())
359
360     def test_todo_and_skip(self):
361         # A file
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")
367         self.tap.seek(0)
368         result = subunit.TAP2SubUnit(self.tap, self.subunit)
369         self.assertEqual(0, result)
370         self.assertEqual([
371             'test test 1 - a fail but',
372             'xfail test 1 - a fail but [',
373             'but is TODO',
374             ']',
375             'test test 2 - another fail',
376             'skip test 2 - another fail [',
377             'instead',
378             ']',
379             ],
380             self.subunit.getvalue().splitlines())
381
382     def test_leading_comments_add_to_next_test_log(self):
383         # A file
384         # # comment
385         # ok 
386         # ok
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")
392         self.tap.seek(0)
393         result = subunit.TAP2SubUnit(self.tap, self.subunit)
394         self.assertEqual(0, result)
395         self.assertEqual([
396             'test test 1',
397             'success test 1 [',
398             '# comment',
399             ']',
400             'test test 2',
401             'success test 2',
402             ],
403             self.subunit.getvalue().splitlines())
404     
405     def test_trailing_comments_are_included_in_last_test_log(self):
406         # A file
407         # ok foo
408         # ok foo
409         # # comment
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")
415         self.tap.seek(0)
416         result = subunit.TAP2SubUnit(self.tap, self.subunit)
417         self.assertEqual(0, result)
418         self.assertEqual([
419             'test test 1',
420             'success test 1',
421             'test test 2',
422             'success test 2 [',
423             '# comment',
424             ']',
425             ],
426             self.subunit.getvalue().splitlines())
427
428
429 def test_suite():
430     loader = subunit.tests.TestUtil.TestLoader()
431     result = loader.loadTestsFromName(__name__)
432     return result