c4ca4cdb3af3f4e4c1320dc3403e0a3adebe6329
[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 subunit
22
23
24 class TestTAP2SubUnit(unittest.TestCase):
25     """Tests for TAP2SubUnit.
26
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
31     protocol server.
32     """
33
34     def setUp(self):
35         self.tap = StringIO()
36         self.subunit = StringIO()
37
38     def test_skip_entire_file(self):
39         # A file
40         # 1..- # Skipped: comment
41         # results in a single skipped test.
42         self.tap.write("1..0 # Skipped: entire file skipped\n")
43         self.tap.seek(0)
44         result = subunit.TAP2SubUnit(self.tap, self.subunit)
45         self.assertEqual(0, result)
46         self.assertEqual([
47             "test file skip",
48             "skip file skip [",
49             "Skipped: entire file skipped",
50             "]",
51             ],
52             self.subunit.getvalue().splitlines())
53
54     def test_ok_test_pass(self):
55         # A file
56         # ok
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
59         # stream).
60         self.tap.write("ok\n")
61         self.tap.seek(0)
62         result = subunit.TAP2SubUnit(self.tap, self.subunit)
63         self.assertEqual(0, result)
64         self.assertEqual([
65             "test test 1",
66             "success test 1",
67             ],
68             self.subunit.getvalue().splitlines())
69
70     def test_ok_test_number_pass(self):
71         # A file
72         # ok 1
73         # results in a passed test with name 'test 1'
74         self.tap.write("ok 1\n")
75         self.tap.seek(0)
76         result = subunit.TAP2SubUnit(self.tap, self.subunit)
77         self.assertEqual(0, result)
78         self.assertEqual([
79             "test test 1",
80             "success test 1",
81             ],
82             self.subunit.getvalue().splitlines())
83
84     def test_ok_test_number_description_pass(self):
85         # A file
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")
89         self.tap.seek(0)
90         result = subunit.TAP2SubUnit(self.tap, self.subunit)
91         self.assertEqual(0, result)
92         self.assertEqual([
93             "test test 1 - There is a description",
94             "success test 1 - There is a description",
95             ],
96             self.subunit.getvalue().splitlines())
97
98     def test_ok_test_description_pass(self):
99         # A file
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")
103         self.tap.seek(0)
104         result = subunit.TAP2SubUnit(self.tap, self.subunit)
105         self.assertEqual(0, result)
106         self.assertEqual([
107             "test test 1 There is a description",
108             "success test 1 There is a description",
109             ],
110             self.subunit.getvalue().splitlines())
111
112     def test_ok_SKIP_skip(self):
113         # A file
114         # ok # SKIP
115         # results in a skkip test with name 'test 1'
116         self.tap.write("ok # SKIP\n")
117         self.tap.seek(0)
118         result = subunit.TAP2SubUnit(self.tap, self.subunit)
119         self.assertEqual(0, result)
120         self.assertEqual([
121             "test test 1",
122             "skip test 1",
123             ],
124             self.subunit.getvalue().splitlines())
125
126     def test_ok_skip_number_comment_lowercase(self):
127         self.tap.write("ok 1 # skip no samba environment available, skipping compilation\n")
128         self.tap.seek(0)
129         result = subunit.TAP2SubUnit(self.tap, self.subunit)
130         self.assertEqual(0, result)
131         self.assertEqual([
132             "test test 1",
133             "skip test 1 [", 
134             "no samba environment available, skipping compilation",
135             "]"
136             ],
137             self.subunit.getvalue().splitlines())
138
139     def test_ok_number_description_SKIP_skip_comment(self):
140         # A file
141         # ok 1 foo  # SKIP Not done yet
142         # results in a skip test with name 'test 1 foo' and a log of
143         # Not done yet
144         self.tap.write("ok 1 foo  # SKIP Not done yet\n")
145         self.tap.seek(0)
146         result = subunit.TAP2SubUnit(self.tap, self.subunit)
147         self.assertEqual(0, result)
148         self.assertEqual([
149             "test test 1 foo",
150             "skip test 1 foo [",
151             "Not done yet",
152             "]",
153             ],
154             self.subunit.getvalue().splitlines())
155
156     def test_ok_SKIP_skip_comment(self):
157         # A file
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")
161         self.tap.seek(0)
162         result = subunit.TAP2SubUnit(self.tap, self.subunit)
163         self.assertEqual(0, result)
164         self.assertEqual([
165             "test test 1",
166             "skip test 1 [",
167             "Not done yet",
168             "]",
169             ],
170             self.subunit.getvalue().splitlines())
171
172     def test_ok_TODO_xfail(self):
173         # A file
174         # ok # TODO
175         # results in a xfail test with name 'test 1'
176         self.tap.write("ok # TODO\n")
177         self.tap.seek(0)
178         result = subunit.TAP2SubUnit(self.tap, self.subunit)
179         self.assertEqual(0, result)
180         self.assertEqual([
181             "test test 1",
182             "xfail test 1",
183             ],
184             self.subunit.getvalue().splitlines())
185
186     def test_ok_TODO_xfail_comment(self):
187         # A file
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")
191         self.tap.seek(0)
192         result = subunit.TAP2SubUnit(self.tap, self.subunit)
193         self.assertEqual(0, result)
194         self.assertEqual([
195             "test test 1",
196             "xfail test 1 [",
197             "Not done yet",
198             "]",
199             ],
200             self.subunit.getvalue().splitlines())
201
202     def test_bail_out_errors(self):
203         # A file with line in it
204         # Bail out! COMMENT
205         # is treated as an error
206         self.tap.write("ok 1 foo\n")
207         self.tap.write("Bail out! Lifejacket engaged\n")
208         self.tap.seek(0)
209         result = subunit.TAP2SubUnit(self.tap, self.subunit)
210         self.assertEqual(0, result)
211         self.assertEqual([
212             "test test 1 foo",
213             "success test 1 foo",
214             "test Bail out! Lifejacket engaged",
215             "error Bail out! Lifejacket engaged",
216             ],
217             self.subunit.getvalue().splitlines())
218
219     def test_missing_test_at_end_with_plan_adds_error(self):
220         # A file
221         # 1..3
222         # ok first test
223         # not ok third test
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')
228         self.tap.seek(0)
229         result = subunit.TAP2SubUnit(self.tap, self.subunit)
230         self.assertEqual(0, result)
231         self.assertEqual([
232             'test test 1 first test',
233             'success test 1 first test',
234             'test test 2 second test',
235             'failure test 2 second test',
236             'test test 3',
237             'error test 3 [',
238             'test missing from TAP output',
239             ']',
240             ],
241             self.subunit.getvalue().splitlines())
242
243     def test_missing_test_with_plan_adds_error(self):
244         # A file
245         # 1..3
246         # ok first test
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')
252         self.tap.seek(0)
253         result = subunit.TAP2SubUnit(self.tap, self.subunit)
254         self.assertEqual(0, result)
255         self.assertEqual([
256             'test test 1 first test',
257             'success test 1 first test',
258             'test test 2',
259             'error test 2 [',
260             'test missing from TAP output',
261             ']',
262             'test test 3 third test',
263             'failure test 3 third test',
264             ],
265             self.subunit.getvalue().splitlines())
266
267     def test_missing_test_no_plan_adds_error(self):
268         # A file
269         # ok first test
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')
274         self.tap.seek(0)
275         result = subunit.TAP2SubUnit(self.tap, self.subunit)
276         self.assertEqual(0, result)
277         self.assertEqual([
278             'test test 1 first test',
279             'success test 1 first test',
280             'test test 2',
281             'error test 2 [',
282             'test missing from TAP output',
283             ']',
284             'test test 3 third test',
285             'failure test 3 third test',
286             ],
287             self.subunit.getvalue().splitlines())
288
289     def test_four_tests_in_a_row_trailing_plan(self):
290         # A file
291         # ok 1 - first test in a script with no plan at all
292         # not ok 2 - second
293         # ok 3 - third
294         # not ok 4 - fourth
295         # 1..4
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')
302         self.tap.seek(0)
303         result = subunit.TAP2SubUnit(self.tap, self.subunit)
304         self.assertEqual(0, result)
305         self.assertEqual([
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'
314             ],
315             self.subunit.getvalue().splitlines())
316
317     def test_four_tests_in_a_row_with_plan(self):
318         # A file
319         # 1..4
320         # ok 1 - first test in a script with no plan at all
321         # not ok 2 - second
322         # ok 3 - third
323         # not ok 4 - fourth
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')
330         self.tap.seek(0)
331         result = subunit.TAP2SubUnit(self.tap, self.subunit)
332         self.assertEqual(0, result)
333         self.assertEqual([
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'
342             ],
343             self.subunit.getvalue().splitlines())
344
345     def test_four_tests_in_a_row_no_plan(self):
346         # A file
347         # ok 1 - first test in a script with no plan at all
348         # not ok 2 - second
349         # ok 3 - third
350         # not ok 4 - fourth
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')
356         self.tap.seek(0)
357         result = subunit.TAP2SubUnit(self.tap, self.subunit)
358         self.assertEqual(0, result)
359         self.assertEqual([
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'
368             ],
369             self.subunit.getvalue().splitlines())
370
371     def test_todo_and_skip(self):
372         # A file
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")
378         self.tap.seek(0)
379         result = subunit.TAP2SubUnit(self.tap, self.subunit)
380         self.assertEqual(0, result)
381         self.assertEqual([
382             'test test 1 - a fail but',
383             'xfail test 1 - a fail but [',
384             'but is TODO',
385             ']',
386             'test test 2 - another fail',
387             'skip test 2 - another fail [',
388             'instead',
389             ']',
390             ],
391             self.subunit.getvalue().splitlines())
392
393     def test_leading_comments_add_to_next_test_log(self):
394         # A file
395         # # comment
396         # ok 
397         # ok
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")
403         self.tap.seek(0)
404         result = subunit.TAP2SubUnit(self.tap, self.subunit)
405         self.assertEqual(0, result)
406         self.assertEqual([
407             'test test 1',
408             'success test 1 [',
409             '# comment',
410             ']',
411             'test test 2',
412             'success test 2',
413             ],
414             self.subunit.getvalue().splitlines())
415     
416     def test_trailing_comments_are_included_in_last_test_log(self):
417         # A file
418         # ok foo
419         # ok foo
420         # # comment
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")
426         self.tap.seek(0)
427         result = subunit.TAP2SubUnit(self.tap, self.subunit)
428         self.assertEqual(0, result)
429         self.assertEqual([
430             'test test 1',
431             'success test 1',
432             'test test 2',
433             'success test 2 [',
434             '# comment',
435             ']',
436             ],
437             self.subunit.getvalue().splitlines())
438
439
440 def test_suite():
441     loader = subunit.tests.TestUtil.TestLoader()
442     result = loader.loadTestsFromName(__name__)
443     return result