Debian Codegraph @

python/tests/test_tap2subunit.py

raw
1 #
2 # subunit: extensions to python unittest to get test results from subprocesses.
3 # Copyright (C) 2005 Robert Collins <[email protected]>
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 from io import BytesIO, StringIO
20
21 from testtools import TestCase
22
23 from testtools.testresult.doubles import StreamResult
24
25 import subunit
26
27 UTF8_TEXT = "text/plain; charset=UTF8"
28
29
30 class TestTAP2SubUnit(TestCase):
31 """Tests for TAP2SubUnit.
32
33 These tests test TAP string data in, and subunit string data out.
34 This is ok because the subunit protocol is intended to be stable,
35 but it might be easier/pithier to write tests against TAP string in,
36 parsed subunit objects out (by hooking the subunit stream to a subunit
37 protocol server.
38 """
39
40 def setUp(self):
41 super().setUp()
42 self.tap = StringIO()
43 self.subunit = BytesIO()
44
45 def test_skip_entire_file(self):
46 # A file
47 # 1..- # Skipped: comment
48 # results in a single skipped test.
49 self.tap.write("1..0 # Skipped: entire file skipped\n")
50 self.tap.seek(0)
51 result = subunit.TAP2SubUnit(self.tap, self.subunit)
52 self.assertEqual(0, result)
53 self.check_events(
54 [
55 (
56 "status",
57 "file skip",
58 "skip",
59 None,
60 True,
61 "tap comment",
62 b"Skipped: entire file skipped",
63 True,
64 None,
65 None,
66 None,
67 )
68 ]
69 )
70
71 def test_ok_test_pass(self):
72 # A file
73 # ok
74 # results in a passed test with name 'test 1' (a synthetic name as tap
75 # does not require named fixtures - it is the first test in the tap
76 # stream).
77 self.tap.write("ok\n")
78 self.tap.seek(0)
79 result = subunit.TAP2SubUnit(self.tap, self.subunit)
80 self.assertEqual(0, result)
81 self.check_events([("status", "test 1", "success", None, False, None, None, True, None, None, None)])
82
83 def test_ok_test_number_pass(self):
84 # A file
85 # ok 1
86 # results in a passed test with name 'test 1'
87 self.tap.write("ok 1\n")
88 self.tap.seek(0)
89 result = subunit.TAP2SubUnit(self.tap, self.subunit)
90 self.assertEqual(0, result)
91 self.check_events([("status", "test 1", "success", None, False, None, None, True, None, None, None)])
92
93 def test_ok_test_number_description_pass(self):
94 # A file
95 # ok 1 - There is a description
96 # results in a passed test with name 'test 1 - There is a description'
97 self.tap.write("ok 1 - There is a description\n")
98 self.tap.seek(0)
99 result = subunit.TAP2SubUnit(self.tap, self.subunit)
100 self.assertEqual(0, result)
101 self.check_events(
102 [("status", "test 1 - There is a description", "success", None, False, None, None, True, None, None, None)]
103 )
104
105 def test_ok_test_description_pass(self):
106 # A file
107 # ok There is a description
108 # results in a passed test with name 'test 1 There is a description'
109 self.tap.write("ok There is a description\n")
110 self.tap.seek(0)
111 result = subunit.TAP2SubUnit(self.tap, self.subunit)
112 self.assertEqual(0, result)
113 self.check_events(
114 [("status", "test 1 There is a description", "success", None, False, None, None, True, None, None, None)]
115 )
116
117 def test_ok_SKIP_skip(self):
118 # A file
119 # ok # SKIP
120 # results in a skkip test with name 'test 1'
121 self.tap.write("ok # SKIP\n")
122 self.tap.seek(0)
123 result = subunit.TAP2SubUnit(self.tap, self.subunit)
124 self.assertEqual(0, result)
125 self.check_events([("status", "test 1", "skip", None, False, None, None, True, None, None, None)])
126
127 def test_ok_skip_number_comment_lowercase(self):
128 self.tap.write("ok 1 # skip no samba environment available, skipping compilation\n")
129 self.tap.seek(0)
130 result = subunit.TAP2SubUnit(self.tap, self.subunit)
131 self.assertEqual(0, result)
132 self.check_events(
133 [
134 (
135 "status",
136 "test 1",
137 "skip",
138 None,
139 False,
140 "tap comment",
141 b"no samba environment available, skipping compilation",
142 True,
143 "text/plain; charset=UTF8",
144 None,
145 None,
146 )
147 ]
148 )
149
150 def test_ok_number_description_SKIP_skip_comment(self):
151 # A file
152 # ok 1 foo # SKIP Not done yet
153 # results in a skip test with name 'test 1 foo' and a log of
154 # Not done yet
155 self.tap.write("ok 1 foo # SKIP Not done yet\n")
156 self.tap.seek(0)
157 result = subunit.TAP2SubUnit(self.tap, self.subunit)
158 self.assertEqual(0, result)
159 self.check_events(
160 [
161 (
162 "status",
163 "test 1 foo",
164 "skip",
165 None,
166 False,
167 "tap comment",
168 b"Not done yet",
169 True,
170 "text/plain; charset=UTF8",
171 None,
172 None,
173 )
174 ]
175 )
176
177 def test_ok_SKIP_skip_comment(self):
178 # A file
179 # ok # SKIP Not done yet
180 # results in a skip test with name 'test 1' and a log of Not done yet
181 self.tap.write("ok # SKIP Not done yet\n")
182 self.tap.seek(0)
183 result = subunit.TAP2SubUnit(self.tap, self.subunit)
184 self.assertEqual(0, result)
185 self.check_events(
186 [
187 (
188 "status",
189 "test 1",
190 "skip",
191 None,
192 False,
193 "tap comment",
194 b"Not done yet",
195 True,
196 "text/plain; charset=UTF8",
197 None,
198 None,
199 )
200 ]
201 )
202
203 def test_ok_TODO_xfail(self):
204 # A file
205 # ok # TODO
206 # results in a xfail test with name 'test 1'
207 self.tap.write("ok # TODO\n")
208 self.tap.seek(0)
209 result = subunit.TAP2SubUnit(self.tap, self.subunit)
210 self.assertEqual(0, result)
211 self.check_events([("status", "test 1", "xfail", None, False, None, None, True, None, None, None)])
212
213 def test_ok_TODO_xfail_comment(self):
214 # A file
215 # ok # TODO Not done yet
216 # results in a xfail test with name 'test 1' and a log of Not done yet
217 self.tap.write("ok # TODO Not done yet\n")
218 self.tap.seek(0)
219 result = subunit.TAP2SubUnit(self.tap, self.subunit)
220 self.assertEqual(0, result)
221 self.check_events(
222 [
223 (
224 "status",
225 "test 1",
226 "xfail",
227 None,
228 False,
229 "tap comment",
230 b"Not done yet",
231 True,
232 "text/plain; charset=UTF8",
233 None,
234 None,
235 )
236 ]
237 )
238
239 def test_bail_out_errors(self):
240 # A file with line in it
241 # Bail out! COMMENT
242 # is treated as an error
243 self.tap.write("ok 1 foo\n")
244 self.tap.write("Bail out! Lifejacket engaged\n")
245 self.tap.seek(0)
246 result = subunit.TAP2SubUnit(self.tap, self.subunit)
247 self.assertEqual(0, result)
248 self.check_events(
249 [
250 ("status", "test 1 foo", "success", None, False, None, None, True, None, None, None),
251 ("status", "Bail out! Lifejacket engaged", "fail", None, False, None, None, True, None, None, None),
252 ]
253 )
254
255 def test_missing_test_at_end_with_plan_adds_error(self):
256 # A file
257 # 1..3
258 # ok first test
259 # not ok third test
260 # results in three tests, with the third being created
261 self.tap.write("1..3\n")
262 self.tap.write("ok first test\n")
263 self.tap.write("not ok second test\n")
264 self.tap.seek(0)
265 result = subunit.TAP2SubUnit(self.tap, self.subunit)
266 self.assertEqual(0, result)
267 self.check_events(
268 [
269 ("status", "test 1 first test", "success", None, False, None, None, True, None, None, None),
270 ("status", "test 2 second test", "fail", None, False, None, None, True, None, None, None),
271 (
272 "status",
273 "test 3",
274 "fail",
275 None,
276 False,
277 "tap meta",
278 b"test missing from TAP output",
279 True,
280 "text/plain; charset=UTF8",
281 None,
282 None,
283 ),
284 ]
285 )
286
287 def test_missing_test_with_plan_adds_error(self):
288 # A file
289 # 1..3
290 # ok first test
291 # not ok 3 third test
292 # results in three tests, with the second being created
293 self.tap.write("1..3\n")
294 self.tap.write("ok first test\n")
295 self.tap.write("not ok 3 third test\n")
296 self.tap.seek(0)
297 result = subunit.TAP2SubUnit(self.tap, self.subunit)
298 self.assertEqual(0, result)
299 self.check_events(
300 [
301 ("status", "test 1 first test", "success", None, False, None, None, True, None, None, None),
302 (
303 "status",
304 "test 2",
305 "fail",
306 None,
307 False,
308 "tap meta",
309 b"test missing from TAP output",
310 True,
311 "text/plain; charset=UTF8",
312 None,
313 None,
314 ),
315 ("status", "test 3 third test", "fail", None, False, None, None, True, None, None, None),
316 ]
317 )
318
319 def test_missing_test_no_plan_adds_error(self):
320 # A file
321 # ok first test
322 # not ok 3 third test
323 # results in three tests, with the second being created
324 self.tap.write("ok first test\n")
325 self.tap.write("not ok 3 third test\n")
326 self.tap.seek(0)
327 result = subunit.TAP2SubUnit(self.tap, self.subunit)
328 self.assertEqual(0, result)
329 self.check_events(
330 [
331 ("status", "test 1 first test", "success", None, False, None, None, True, None, None, None),
332 (
333 "status",
334 "test 2",
335 "fail",
336 None,
337 False,
338 "tap meta",
339 b"test missing from TAP output",
340 True,
341 "text/plain; charset=UTF8",
342 None,
343 None,
344 ),
345 ("status", "test 3 third test", "fail", None, False, None, None, True, None, None, None),
346 ]
347 )
348
349 def test_four_tests_in_a_row_trailing_plan(self):
350 # A file
351 # ok 1 - first test in a script with no plan at all
352 # not ok 2 - second
353 # ok 3 - third
354 # not ok 4 - fourth
355 # 1..4
356 # results in four tests numbered and named
357 self.tap.write("ok 1 - first test in a script with trailing plan\n")
358 self.tap.write("not ok 2 - second\n")
359 self.tap.write("ok 3 - third\n")
360 self.tap.write("not ok 4 - fourth\n")
361 self.tap.write("1..4\n")
362 self.tap.seek(0)
363 result = subunit.TAP2SubUnit(self.tap, self.subunit)
364 self.assertEqual(0, result)
365 self.check_events(
366 [
367 (
368 "status",
369 "test 1 - first test in a script with trailing plan",
370 "success",
371 None,
372 False,
373 None,
374 None,
375 True,
376 None,
377 None,
378 None,
379 ),
380 ("status", "test 2 - second", "fail", None, False, None, None, True, None, None, None),
381 ("status", "test 3 - third", "success", None, False, None, None, True, None, None, None),
382 ("status", "test 4 - fourth", "fail", None, False, None, None, True, None, None, None),
383 ]
384 )
385
386 def test_four_tests_in_a_row_with_plan(self):
387 # A file
388 # 1..4
389 # ok 1 - first test in a script with no plan at all
390 # not ok 2 - second
391 # ok 3 - third
392 # not ok 4 - fourth
393 # results in four tests numbered and named
394 self.tap.write("1..4\n")
395 self.tap.write("ok 1 - first test in a script with a plan\n")
396 self.tap.write("not ok 2 - second\n")
397 self.tap.write("ok 3 - third\n")
398 self.tap.write("not ok 4 - fourth\n")
399 self.tap.seek(0)
400 result = subunit.TAP2SubUnit(self.tap, self.subunit)
401 self.assertEqual(0, result)
402 self.check_events(
403 [
404 (
405 "status",
406 "test 1 - first test in a script with a plan",
407 "success",
408 None,
409 False,
410 None,
411 None,
412 True,
413 None,
414 None,
415 None,
416 ),
417 ("status", "test 2 - second", "fail", None, False, None, None, True, None, None, None),
418 ("status", "test 3 - third", "success", None, False, None, None, True, None, None, None),
419 ("status", "test 4 - fourth", "fail", None, False, None, None, True, None, None, None),
420 ]
421 )
422
423 def test_four_tests_in_a_row_no_plan(self):
424 # A file
425 # ok 1 - first test in a script with no plan at all
426 # not ok 2 - second
427 # ok 3 - third
428 # not ok 4 - fourth
429 # results in four tests numbered and named
430 self.tap.write("ok 1 - first test in a script with no plan at all\n")
431 self.tap.write("not ok 2 - second\n")
432 self.tap.write("ok 3 - third\n")
433 self.tap.write("not ok 4 - fourth\n")
434 self.tap.seek(0)
435 result = subunit.TAP2SubUnit(self.tap, self.subunit)
436 self.assertEqual(0, result)
437 self.check_events(
438 [
439 (
440 "status",
441 "test 1 - first test in a script with no plan at all",
442 "success",
443 None,
444 False,
445 None,
446 None,
447 True,
448 None,
449 None,
450 None,
451 ),
452 ("status", "test 2 - second", "fail", None, False, None, None, True, None, None, None),
453 ("status", "test 3 - third", "success", None, False, None, None, True, None, None, None),
454 ("status", "test 4 - fourth", "fail", None, False, None, None, True, None, None, None),
455 ]
456 )
457
458 def test_todo_and_skip(self):
459 # A file
460 # not ok 1 - a fail but # TODO but is TODO
461 # not ok 2 - another fail # SKIP instead
462 # results in two tests, numbered and commented.
463 self.tap.write("not ok 1 - a fail but # TODO but is TODO\n")
464 self.tap.write("not ok 2 - another fail # SKIP instead\n")
465 self.tap.seek(0)
466 result = subunit.TAP2SubUnit(self.tap, self.subunit)
467 self.assertEqual(0, result)
468 self.subunit.seek(0)
469 events = StreamResult()
470 subunit.ByteStreamToStreamResult(self.subunit).run(events)
471 self.check_events(
472 [
473 (
474 "status",
475 "test 1 - a fail but",
476 "xfail",
477 None,
478 False,
479 "tap comment",
480 b"but is TODO",
481 True,
482 "text/plain; charset=UTF8",
483 None,
484 None,
485 ),
486 (
487 "status",
488 "test 2 - another fail",
489 "skip",
490 None,
491 False,
492 "tap comment",
493 b"instead",
494 True,
495 "text/plain; charset=UTF8",
496 None,
497 None,
498 ),
499 ]
500 )
501
502 def test_leading_comments_add_to_next_test_log(self):
503 # A file
504 # # comment
505 # ok
506 # ok
507 # results in a single test with the comment included
508 # in the first test and not the second.
509 self.tap.write("# comment\n")
510 self.tap.write("ok\n")
511 self.tap.write("ok\n")
512 self.tap.seek(0)
513 result = subunit.TAP2SubUnit(self.tap, self.subunit)
514 self.assertEqual(0, result)
515 self.check_events(
516 [
517 (
518 "status",
519 "test 1",
520 "success",
521 None,
522 False,
523 "tap comment",
524 b"# comment",
525 True,
526 "text/plain; charset=UTF8",
527 None,
528 None,
529 ),
530 ("status", "test 2", "success", None, False, None, None, True, None, None, None),
531 ]
532 )
533
534 def test_trailing_comments_are_included_in_last_test_log(self):
535 # A file
536 # ok foo
537 # ok foo
538 # # comment
539 # results in a two tests, with the second having the comment
540 # attached to its log.
541 self.tap.write("ok\n")
542 self.tap.write("ok\n")
543 self.tap.write("# comment\n")
544 self.tap.seek(0)
545 result = subunit.TAP2SubUnit(self.tap, self.subunit)
546 self.assertEqual(0, result)
547 self.check_events(
548 [
549 ("status", "test 1", "success", None, False, None, None, True, None, None, None),
550 (
551 "status",
552 "test 2",
553 "success",
554 None,
555 False,
556 "tap comment",
557 b"# comment",
558 True,
559 "text/plain; charset=UTF8",
560 None,
561 None,
562 ),
563 ]
564 )
565
566 def check_events(self, events):
567 self.subunit.seek(0)
568 eventstream = StreamResult()
569 subunit.ByteStreamToStreamResult(self.subunit).run(eventstream)
570 self.assertEqual(events, eventstream._events)