af31584a972ddb76aea10f29c1da9ffb2bdef32f
[nivanova/samba-autobuild/.git] / source4 / scripting / python / subunit / tests / test_test_protocol.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 #  This program is free software; you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation; either version 2 of the License, or
8 #  (at your option) any later version.
9 #
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 #
19
20 import unittest
21 from StringIO import StringIO
22 import os
23 import subunit
24 import sys
25
26 try:
27     class MockTestProtocolServerClient(object):
28         """A mock protocol server client to test callbacks."""
29
30         def __init__(self):
31             self.end_calls = []
32             self.error_calls = []
33             self.failure_calls = []
34             self.start_calls = []
35             self.success_calls = []
36             super(MockTestProtocolServerClient, self).__init__()
37
38         def addError(self, test, error):
39             self.error_calls.append((test, error))
40
41         def addFailure(self, test, error):
42             self.failure_calls.append((test, error))
43
44         def addSuccess(self, test):
45             self.success_calls.append(test)
46
47         def stopTest(self, test):
48             self.end_calls.append(test)
49
50         def startTest(self, test):
51             self.start_calls.append(test)
52
53 except AttributeError:
54     MockTestProtocolServer = None
55
56
57 class TestMockTestProtocolServer(unittest.TestCase):
58
59     def test_start_test(self):
60         protocol = MockTestProtocolServerClient()
61         protocol.startTest(subunit.RemotedTestCase("test old mcdonald"))
62         self.assertEqual(protocol.start_calls,
63                          [subunit.RemotedTestCase("test old mcdonald")])
64         self.assertEqual(protocol.end_calls, [])
65         self.assertEqual(protocol.error_calls, [])
66         self.assertEqual(protocol.failure_calls, [])
67         self.assertEqual(protocol.success_calls, [])
68
69     def test_add_error(self):
70         protocol = MockTestProtocolServerClient()
71         protocol.addError(subunit.RemotedTestCase("old mcdonald"),
72                           subunit.RemoteError("omg it works"))
73         self.assertEqual(protocol.start_calls, [])
74         self.assertEqual(protocol.end_calls, [])
75         self.assertEqual(protocol.error_calls, [(
76                             subunit.RemotedTestCase("old mcdonald"),
77                             subunit.RemoteError("omg it works"))])
78         self.assertEqual(protocol.failure_calls, [])
79         self.assertEqual(protocol.success_calls, [])
80
81     def test_add_failure(self):
82         protocol = MockTestProtocolServerClient()
83         protocol.addFailure(subunit.RemotedTestCase("old mcdonald"),
84                             subunit.RemoteError("omg it works"))
85         self.assertEqual(protocol.start_calls, [])
86         self.assertEqual(protocol.end_calls, [])
87         self.assertEqual(protocol.error_calls, [])
88         self.assertEqual(protocol.failure_calls, [
89                             (subunit.RemotedTestCase("old mcdonald"),
90                              subunit.RemoteError("omg it works"))])
91         self.assertEqual(protocol.success_calls, [])
92
93     def test_add_success(self):
94         protocol = MockTestProtocolServerClient()
95         protocol.addSuccess(subunit.RemotedTestCase("test old mcdonald"))
96         self.assertEqual(protocol.start_calls, [])
97         self.assertEqual(protocol.end_calls, [])
98         self.assertEqual(protocol.error_calls, [])
99         self.assertEqual(protocol.failure_calls, [])
100         self.assertEqual(protocol.success_calls,
101                          [subunit.RemotedTestCase("test old mcdonald")])
102
103     def test_end_test(self):
104         protocol = MockTestProtocolServerClient()
105         protocol.stopTest(subunit.RemotedTestCase("test old mcdonald"))
106         self.assertEqual(protocol.end_calls,
107                          [subunit.RemotedTestCase("test old mcdonald")])
108         self.assertEqual(protocol.error_calls, [])
109         self.assertEqual(protocol.failure_calls, [])
110         self.assertEqual(protocol.success_calls, [])
111         self.assertEqual(protocol.start_calls, [])
112
113
114 class TestTestImports(unittest.TestCase):
115
116     def test_imports(self):
117         from subunit import TestProtocolServer
118         from subunit import RemotedTestCase
119         from subunit import RemoteError
120         from subunit import ExecTestCase
121         from subunit import IsolatedTestCase
122         from subunit import TestProtocolClient
123
124
125 class TestTestProtocolServerPipe(unittest.TestCase):
126
127     def test_story(self):
128         client = unittest.TestResult()
129         protocol = subunit.TestProtocolServer(client)
130         pipe = StringIO("test old mcdonald\n"
131                         "success old mcdonald\n"
132                         "test bing crosby\n"
133                         "failure bing crosby [\n"
134                         "foo.c:53:ERROR invalid state\n"
135                         "]\n"
136                         "test an error\n"
137                         "error an error\n")
138         protocol.readFrom(pipe)
139         mcdonald = subunit.RemotedTestCase("old mcdonald")
140         bing = subunit.RemotedTestCase("bing crosby")
141         an_error = subunit.RemotedTestCase("an error")
142         self.assertEqual(client.errors,
143                          [(an_error, 'RemoteException: \n\n')])
144         self.assertEqual(
145             client.failures,
146             [(bing, "RemoteException: foo.c:53:ERROR invalid state\n\n")])
147         self.assertEqual(client.testsRun, 3)
148
149
150 class TestTestProtocolServerStartTest(unittest.TestCase):
151
152     def setUp(self):
153         self.client = MockTestProtocolServerClient()
154         self.protocol = subunit.TestProtocolServer(self.client)
155
156     def test_start_test(self):
157         self.protocol.lineReceived("test old mcdonald\n")
158         self.assertEqual(self.client.start_calls,
159                          [subunit.RemotedTestCase("old mcdonald")])
160
161     def test_start_testing(self):
162         self.protocol.lineReceived("testing old mcdonald\n")
163         self.assertEqual(self.client.start_calls,
164                          [subunit.RemotedTestCase("old mcdonald")])
165
166     def test_start_test_colon(self):
167         self.protocol.lineReceived("test: old mcdonald\n")
168         self.assertEqual(self.client.start_calls,
169                          [subunit.RemotedTestCase("old mcdonald")])
170
171     def test_start_testing_colon(self):
172         self.protocol.lineReceived("testing: old mcdonald\n")
173         self.assertEqual(self.client.start_calls,
174                          [subunit.RemotedTestCase("old mcdonald")])
175
176
177 class TestTestProtocolServerPassThrough(unittest.TestCase):
178
179     def setUp(self):
180         from StringIO import StringIO
181         self.stdout = StringIO()
182         self.test = subunit.RemotedTestCase("old mcdonald")
183         self.client = MockTestProtocolServerClient()
184         self.protocol = subunit.TestProtocolServer(self.client, self.stdout)
185
186     def keywords_before_test(self):
187         self.protocol.lineReceived("failure a\n")
188         self.protocol.lineReceived("failure: a\n")
189         self.protocol.lineReceived("error a\n")
190         self.protocol.lineReceived("error: a\n")
191         self.protocol.lineReceived("success a\n")
192         self.protocol.lineReceived("success: a\n")
193         self.protocol.lineReceived("successful a\n")
194         self.protocol.lineReceived("successful: a\n")
195         self.protocol.lineReceived("]\n")
196         self.assertEqual(self.stdout.getvalue(), "failure a\n"
197                                                  "failure: a\n"
198                                                  "error a\n"
199                                                  "error: a\n"
200                                                  "success a\n"
201                                                  "success: a\n"
202                                                  "successful a\n"
203                                                  "successful: a\n"
204                                                  "]\n")
205
206     def test_keywords_before_test(self):
207         self.keywords_before_test()
208         self.assertEqual(self.client.start_calls, [])
209         self.assertEqual(self.client.error_calls, [])
210         self.assertEqual(self.client.failure_calls, [])
211         self.assertEqual(self.client.success_calls, [])
212
213     def test_keywords_after_error(self):
214         self.protocol.lineReceived("test old mcdonald\n")
215         self.protocol.lineReceived("error old mcdonald\n")
216         self.keywords_before_test()
217         self.assertEqual(self.client.start_calls, [self.test])
218         self.assertEqual(self.client.end_calls, [self.test])
219         self.assertEqual(self.client.error_calls,
220                          [(self.test, subunit.RemoteError(""))])
221         self.assertEqual(self.client.failure_calls, [])
222         self.assertEqual(self.client.success_calls, [])
223
224     def test_keywords_after_failure(self):
225         self.protocol.lineReceived("test old mcdonald\n")
226         self.protocol.lineReceived("failure old mcdonald\n")
227         self.keywords_before_test()
228         self.assertEqual(self.client.start_calls, [self.test])
229         self.assertEqual(self.client.end_calls, [self.test])
230         self.assertEqual(self.client.error_calls, [])
231         self.assertEqual(self.client.failure_calls,
232                          [(self.test, subunit.RemoteError())])
233         self.assertEqual(self.client.success_calls, [])
234
235     def test_keywords_after_success(self):
236         self.protocol.lineReceived("test old mcdonald\n")
237         self.protocol.lineReceived("success old mcdonald\n")
238         self.keywords_before_test()
239         self.assertEqual(self.client.start_calls, [self.test])
240         self.assertEqual(self.client.end_calls, [self.test])
241         self.assertEqual(self.client.error_calls, [])
242         self.assertEqual(self.client.failure_calls, [])
243         self.assertEqual(self.client.success_calls, [self.test])
244
245     def test_keywords_after_test(self):
246         self.protocol.lineReceived("test old mcdonald\n")
247         self.protocol.lineReceived("test old mcdonald\n")
248         self.protocol.lineReceived("failure a\n")
249         self.protocol.lineReceived("failure: a\n")
250         self.protocol.lineReceived("error a\n")
251         self.protocol.lineReceived("error: a\n")
252         self.protocol.lineReceived("success a\n")
253         self.protocol.lineReceived("success: a\n")
254         self.protocol.lineReceived("successful a\n")
255         self.protocol.lineReceived("successful: a\n")
256         self.protocol.lineReceived("]\n")
257         self.protocol.lineReceived("failure old mcdonald\n")
258         self.assertEqual(self.stdout.getvalue(), "test old mcdonald\n"
259                                                  "failure a\n"
260                                                  "failure: a\n"
261                                                  "error a\n"
262                                                  "error: a\n"
263                                                  "success a\n"
264                                                  "success: a\n"
265                                                  "successful a\n"
266                                                  "successful: a\n"
267                                                  "]\n")
268         self.assertEqual(self.client.start_calls, [self.test])
269         self.assertEqual(self.client.end_calls, [self.test])
270         self.assertEqual(self.client.failure_calls,
271                          [(self.test, subunit.RemoteError())])
272         self.assertEqual(self.client.error_calls, [])
273         self.assertEqual(self.client.success_calls, [])
274
275     def test_keywords_during_failure(self):
276         self.protocol.lineReceived("test old mcdonald\n")
277         self.protocol.lineReceived("failure: old mcdonald [\n")
278         self.protocol.lineReceived("test old mcdonald\n")
279         self.protocol.lineReceived("failure a\n")
280         self.protocol.lineReceived("failure: a\n")
281         self.protocol.lineReceived("error a\n")
282         self.protocol.lineReceived("error: a\n")
283         self.protocol.lineReceived("success a\n")
284         self.protocol.lineReceived("success: a\n")
285         self.protocol.lineReceived("successful a\n")
286         self.protocol.lineReceived("successful: a\n")
287         self.protocol.lineReceived(" ]\n")
288         self.protocol.lineReceived("]\n")
289         self.assertEqual(self.stdout.getvalue(), "")
290         self.assertEqual(self.client.start_calls, [self.test])
291         self.assertEqual(self.client.failure_calls,
292                          [(self.test, subunit.RemoteError("test old mcdonald\n"
293                                                   "failure a\n"
294                                                   "failure: a\n"
295                                                   "error a\n"
296                                                   "error: a\n"
297                                                   "success a\n"
298                                                   "success: a\n"
299                                                   "successful a\n"
300                                                   "successful: a\n"
301                                                   "]\n"))])
302         self.assertEqual(self.client.end_calls, [self.test])
303         self.assertEqual(self.client.error_calls, [])
304         self.assertEqual(self.client.success_calls, [])
305
306     def test_stdout_passthrough(self):
307         """Lines received which cannot be interpreted as any protocol action
308         should be passed through to sys.stdout.
309         """
310         bytes = "randombytes\n"
311         self.protocol.lineReceived(bytes)
312         self.assertEqual(self.stdout.getvalue(), bytes)
313
314
315 class TestTestProtocolServerLostConnection(unittest.TestCase):
316
317     def setUp(self):
318         self.client = MockTestProtocolServerClient()
319         self.protocol = subunit.TestProtocolServer(self.client)
320         self.test = subunit.RemotedTestCase("old mcdonald")
321
322     def test_lost_connection_no_input(self):
323         self.protocol.lostConnection()
324         self.assertEqual(self.client.start_calls, [])
325         self.assertEqual(self.client.error_calls, [])
326         self.assertEqual(self.client.failure_calls, [])
327         self.assertEqual(self.client.success_calls, [])
328
329     def test_lost_connection_after_start(self):
330         self.protocol.lineReceived("test old mcdonald\n")
331         self.protocol.lostConnection()
332         self.assertEqual(self.client.start_calls, [self.test])
333         self.assertEqual(self.client.end_calls, [self.test])
334         self.assertEqual(self.client.error_calls, [
335             (self.test, subunit.RemoteError("lost connection during "
336                                             "test 'old mcdonald'"))])
337         self.assertEqual(self.client.failure_calls, [])
338         self.assertEqual(self.client.success_calls, [])
339
340     def test_lost_connected_after_error(self):
341         self.protocol.lineReceived("test old mcdonald\n")
342         self.protocol.lineReceived("error old mcdonald\n")
343         self.protocol.lostConnection()
344         self.assertEqual(self.client.start_calls, [self.test])
345         self.assertEqual(self.client.failure_calls, [])
346         self.assertEqual(self.client.end_calls, [self.test])
347         self.assertEqual(self.client.error_calls, [
348             (self.test, subunit.RemoteError(""))])
349         self.assertEqual(self.client.success_calls, [])
350
351     def test_lost_connection_during_error(self):
352         self.protocol.lineReceived("test old mcdonald\n")
353         self.protocol.lineReceived("error old mcdonald [\n")
354         self.protocol.lostConnection()
355         self.assertEqual(self.client.start_calls, [self.test])
356         self.assertEqual(self.client.end_calls, [self.test])
357         self.assertEqual(self.client.error_calls, [
358             (self.test, subunit.RemoteError("lost connection during error "
359                                             "report of test 'old mcdonald'"))])
360         self.assertEqual(self.client.failure_calls, [])
361         self.assertEqual(self.client.success_calls, [])
362
363     def test_lost_connected_after_failure(self):
364         self.protocol.lineReceived("test old mcdonald\n")
365         self.protocol.lineReceived("failure old mcdonald\n")
366         self.protocol.lostConnection()
367         test = subunit.RemotedTestCase("old mcdonald")
368         self.assertEqual(self.client.start_calls, [self.test])
369         self.assertEqual(self.client.end_calls, [self.test])
370         self.assertEqual(self.client.error_calls, [])
371         self.assertEqual(self.client.failure_calls,
372                          [(self.test, subunit.RemoteError())])
373         self.assertEqual(self.client.success_calls, [])
374
375     def test_lost_connection_during_failure(self):
376         self.protocol.lineReceived("test old mcdonald\n")
377         self.protocol.lineReceived("failure old mcdonald [\n")
378         self.protocol.lostConnection()
379         self.assertEqual(self.client.start_calls, [self.test])
380         self.assertEqual(self.client.end_calls, [self.test])
381         self.assertEqual(self.client.error_calls,
382                          [(self.test,
383                            subunit.RemoteError("lost connection during "
384                                                "failure report"
385                                                " of test 'old mcdonald'"))])
386         self.assertEqual(self.client.failure_calls, [])
387         self.assertEqual(self.client.success_calls, [])
388
389     def test_lost_connection_after_success(self):
390         self.protocol.lineReceived("test old mcdonald\n")
391         self.protocol.lineReceived("success old mcdonald\n")
392         self.protocol.lostConnection()
393         self.assertEqual(self.client.start_calls, [self.test])
394         self.assertEqual(self.client.end_calls, [self.test])
395         self.assertEqual(self.client.error_calls, [])
396         self.assertEqual(self.client.failure_calls, [])
397         self.assertEqual(self.client.success_calls, [self.test])
398
399
400 class TestTestProtocolServerAddError(unittest.TestCase):
401
402     def setUp(self):
403         self.client = MockTestProtocolServerClient()
404         self.protocol = subunit.TestProtocolServer(self.client)
405         self.protocol.lineReceived("test mcdonalds farm\n")
406         self.test = subunit.RemotedTestCase("mcdonalds farm")
407
408     def simple_error_keyword(self, keyword):
409         self.protocol.lineReceived("%s mcdonalds farm\n" % keyword)
410         self.assertEqual(self.client.start_calls, [self.test])
411         self.assertEqual(self.client.end_calls, [self.test])
412         self.assertEqual(self.client.error_calls, [
413             (self.test, subunit.RemoteError(""))])
414         self.assertEqual(self.client.failure_calls, [])
415
416     def test_simple_error(self):
417         self.simple_error_keyword("error")
418
419     def test_simple_error_colon(self):
420         self.simple_error_keyword("error:")
421
422     def test_error_empty_message(self):
423         self.protocol.lineReceived("error mcdonalds farm [\n")
424         self.protocol.lineReceived("]\n")
425         self.assertEqual(self.client.start_calls, [self.test])
426         self.assertEqual(self.client.end_calls, [self.test])
427         self.assertEqual(self.client.error_calls, [
428             (self.test, subunit.RemoteError(""))])
429         self.assertEqual(self.client.failure_calls, [])
430
431     def error_quoted_bracket(self, keyword):
432         self.protocol.lineReceived("%s mcdonalds farm [\n" % keyword)
433         self.protocol.lineReceived(" ]\n")
434         self.protocol.lineReceived("]\n")
435         self.assertEqual(self.client.start_calls, [self.test])
436         self.assertEqual(self.client.end_calls, [self.test])
437         self.assertEqual(self.client.error_calls, [
438             (self.test, subunit.RemoteError("]\n"))])
439         self.assertEqual(self.client.failure_calls, [])
440
441     def test_error_quoted_bracket(self):
442         self.error_quoted_bracket("error")
443
444     def test_error_colon_quoted_bracket(self):
445         self.error_quoted_bracket("error:")
446
447
448 class TestTestProtocolServerAddFailure(unittest.TestCase):
449
450     def setUp(self):
451         self.client = MockTestProtocolServerClient()
452         self.protocol = subunit.TestProtocolServer(self.client)
453         self.protocol.lineReceived("test mcdonalds farm\n")
454         self.test = subunit.RemotedTestCase("mcdonalds farm")
455
456     def simple_failure_keyword(self, keyword):
457         self.protocol.lineReceived("%s mcdonalds farm\n" % keyword)
458         self.assertEqual(self.client.start_calls, [self.test])
459         self.assertEqual(self.client.end_calls, [self.test])
460         self.assertEqual(self.client.error_calls, [])
461         self.assertEqual(self.client.failure_calls,
462                          [(self.test, subunit.RemoteError())])
463
464     def test_simple_failure(self):
465         self.simple_failure_keyword("failure")
466
467     def test_simple_failure_colon(self):
468         self.simple_failure_keyword("failure:")
469
470     def test_failure_empty_message(self):
471         self.protocol.lineReceived("failure mcdonalds farm [\n")
472         self.protocol.lineReceived("]\n")
473         self.assertEqual(self.client.start_calls, [self.test])
474         self.assertEqual(self.client.end_calls, [self.test])
475         self.assertEqual(self.client.error_calls, [])
476         self.assertEqual(self.client.failure_calls,
477                          [(self.test, subunit.RemoteError())])
478
479     def failure_quoted_bracket(self, keyword):
480         self.protocol.lineReceived("%s mcdonalds farm [\n" % keyword)
481         self.protocol.lineReceived(" ]\n")
482         self.protocol.lineReceived("]\n")
483         self.assertEqual(self.client.start_calls, [self.test])
484         self.assertEqual(self.client.end_calls, [self.test])
485         self.assertEqual(self.client.error_calls, [])
486         self.assertEqual(self.client.failure_calls,
487                          [(self.test, subunit.RemoteError("]\n"))])
488
489     def test_failure_quoted_bracket(self):
490         self.failure_quoted_bracket("failure")
491
492     def test_failure_colon_quoted_bracket(self):
493         self.failure_quoted_bracket("failure:")
494
495
496 class TestTestProtocolServerAddSuccess(unittest.TestCase):
497
498     def setUp(self):
499         self.client = MockTestProtocolServerClient()
500         self.protocol = subunit.TestProtocolServer(self.client)
501         self.protocol.lineReceived("test mcdonalds farm\n")
502         self.test = subunit.RemotedTestCase("mcdonalds farm")
503
504     def simple_success_keyword(self, keyword):
505         self.protocol.lineReceived("%s mcdonalds farm\n" % keyword)
506         self.assertEqual(self.client.start_calls, [self.test])
507         self.assertEqual(self.client.end_calls, [self.test])
508         self.assertEqual(self.client.error_calls, [])
509         self.assertEqual(self.client.success_calls, [self.test])
510
511     def test_simple_success(self):
512         self.simple_success_keyword("failure")
513
514     def test_simple_success_colon(self):
515         self.simple_success_keyword("failure:")
516
517     def test_simple_success(self):
518         self.simple_success_keyword("successful")
519
520     def test_simple_success_colon(self):
521         self.simple_success_keyword("successful:")
522
523
524 class TestRemotedTestCase(unittest.TestCase):
525
526     def test_simple(self):
527         test = subunit.RemotedTestCase("A test description")
528         self.assertRaises(NotImplementedError, test.setUp)
529         self.assertRaises(NotImplementedError, test.tearDown)
530         self.assertEqual("A test description",
531                          test.shortDescription())
532         self.assertEqual("subunit.RemotedTestCase.A test description",
533                          test.id())
534         self.assertEqual("A test description (subunit.RemotedTestCase)", "%s" % test)
535         self.assertEqual("<subunit.RemotedTestCase description="
536                          "'A test description'>", "%r" % test)
537         result = unittest.TestResult()
538         test.run(result)
539         self.assertEqual([(test, "RemoteException: "
540                                  "Cannot run RemotedTestCases.\n\n")],
541                          result.errors)
542         self.assertEqual(1, result.testsRun)
543         another_test = subunit.RemotedTestCase("A test description")
544         self.assertEqual(test, another_test)
545         different_test = subunit.RemotedTestCase("ofo")
546         self.assertNotEqual(test, different_test)
547         self.assertNotEqual(another_test, different_test)
548
549
550 class TestRemoteError(unittest.TestCase):
551
552     def test_eq(self):
553         error = subunit.RemoteError("Something went wrong")
554         another_error = subunit.RemoteError("Something went wrong")
555         different_error = subunit.RemoteError("boo!")
556         self.assertEqual(error, another_error)
557         self.assertNotEqual(error, different_error)
558         self.assertNotEqual(different_error, another_error)
559
560     def test_empty_constructor(self):
561         self.assertEqual(subunit.RemoteError(), subunit.RemoteError(""))
562
563
564 class TestExecTestCase(unittest.TestCase):
565
566     class SampleExecTestCase(subunit.ExecTestCase):
567
568         def test_sample_method(self):
569             """sample-script.py"""
570             # the sample script runs three tests, one each
571             # that fails, errors and succeeds
572
573
574     def test_construct(self):
575         test = self.SampleExecTestCase("test_sample_method")
576         self.assertEqual(test.script,
577                          subunit.join_dir(__file__, 'sample-script.py'))
578
579     def test_run(self):
580         runner = MockTestProtocolServerClient()
581         test = self.SampleExecTestCase("test_sample_method")
582         test.run(runner)
583         mcdonald = subunit.RemotedTestCase("old mcdonald")
584         bing = subunit.RemotedTestCase("bing crosby")
585         an_error = subunit.RemotedTestCase("an error")
586         self.assertEqual(runner.error_calls,
587                          [(an_error, subunit.RemoteError())])
588         self.assertEqual(runner.failure_calls,
589                          [(bing,
590                            subunit.RemoteError(
591                             "foo.c:53:ERROR invalid state\n"))])
592         self.assertEqual(runner.start_calls, [mcdonald, bing, an_error])
593         self.assertEqual(runner.end_calls, [mcdonald, bing, an_error])
594
595     def test_debug(self):
596         test = self.SampleExecTestCase("test_sample_method")
597         test.debug()
598
599     def test_count_test_cases(self):
600         """TODO run the child process and count responses to determine the count."""
601
602     def test_join_dir(self):
603         sibling = subunit.join_dir(__file__, 'foo')
604         expected = '%s/foo' % (os.path.split(__file__)[0],)
605         self.assertEqual(sibling, expected)
606
607
608 class DoExecTestCase(subunit.ExecTestCase):
609
610     def test_working_script(self):
611         """sample-two-script.py"""
612
613
614 class TestIsolatedTestCase(unittest.TestCase):
615
616     class SampleIsolatedTestCase(subunit.IsolatedTestCase):
617
618         SETUP = False
619         TEARDOWN = False
620         TEST = False
621
622         def setUp(self):
623             TestIsolatedTestCase.SampleIsolatedTestCase.SETUP = True
624
625         def tearDown(self):
626             TestIsolatedTestCase.SampleIsolatedTestCase.TEARDOWN = True
627
628         def test_sets_global_state(self):
629             TestIsolatedTestCase.SampleIsolatedTestCase.TEST = True
630
631
632     def test_construct(self):
633         test = self.SampleIsolatedTestCase("test_sets_global_state")
634
635     def test_run(self):
636         result = unittest.TestResult()
637         test = self.SampleIsolatedTestCase("test_sets_global_state")
638         test.run(result)
639         self.assertEqual(result.testsRun, 1)
640         self.assertEqual(self.SampleIsolatedTestCase.SETUP, False)
641         self.assertEqual(self.SampleIsolatedTestCase.TEARDOWN, False)
642         self.assertEqual(self.SampleIsolatedTestCase.TEST, False)
643
644     def test_debug(self):
645         pass
646         #test = self.SampleExecTestCase("test_sample_method")
647         #test.debug()
648
649
650 class TestIsolatedTestSuite(unittest.TestCase):
651
652     class SampleTestToIsolate(unittest.TestCase):
653
654         SETUP = False
655         TEARDOWN = False
656         TEST = False
657
658         def setUp(self):
659             TestIsolatedTestSuite.SampleTestToIsolate.SETUP = True
660
661         def tearDown(self):
662             TestIsolatedTestSuite.SampleTestToIsolate.TEARDOWN = True
663
664         def test_sets_global_state(self):
665             TestIsolatedTestSuite.SampleTestToIsolate.TEST = True
666
667
668     def test_construct(self):
669         suite = subunit.IsolatedTestSuite()
670
671     def test_run(self):
672         result = unittest.TestResult()
673         suite = subunit.IsolatedTestSuite()
674         sub_suite = unittest.TestSuite()
675         sub_suite.addTest(self.SampleTestToIsolate("test_sets_global_state"))
676         sub_suite.addTest(self.SampleTestToIsolate("test_sets_global_state"))
677         suite.addTest(sub_suite)
678         suite.addTest(self.SampleTestToIsolate("test_sets_global_state"))
679         suite.run(result)
680         self.assertEqual(result.testsRun, 3)
681         self.assertEqual(self.SampleTestToIsolate.SETUP, False)
682         self.assertEqual(self.SampleTestToIsolate.TEARDOWN, False)
683         self.assertEqual(self.SampleTestToIsolate.TEST, False)
684
685
686 class TestTestProtocolClient(unittest.TestCase):
687
688     def setUp(self):
689         self.io = StringIO()
690         self.protocol = subunit.TestProtocolClient(self.io)
691         self.test = TestTestProtocolClient("test_start_test")
692
693
694     def test_start_test(self):
695         """Test startTest on a TestProtocolClient."""
696         self.protocol.startTest(self.test)
697         self.assertEqual(self.io.getvalue(), "test: %s\n" % self.test.id())
698
699     def test_stop_test(self):
700         """Test stopTest on a TestProtocolClient."""
701         self.protocol.stopTest(self.test)
702         self.assertEqual(self.io.getvalue(), "")
703
704     def test_add_success(self):
705         """Test addSuccess on a TestProtocolClient."""
706         self.protocol.addSuccess(self.test)
707         self.assertEqual(
708             self.io.getvalue(), "successful: %s\n" % self.test.id())
709
710     def test_add_failure(self):
711         """Test addFailure on a TestProtocolClient."""
712         self.protocol.addFailure(self.test, subunit.RemoteError("boo"))
713         self.assertEqual(
714             self.io.getvalue(),
715             'failure: %s [\nRemoteException: boo\n]\n' % self.test.id())
716
717     def test_add_error(self):
718         """Test stopTest on a TestProtocolClient."""
719         self.protocol.addError(self.test, subunit.RemoteError("phwoar"))
720         self.assertEqual(
721             self.io.getvalue(),
722             'error: %s [\n'
723             "RemoteException: phwoar\n"
724             "]\n" % self.test.id())
725
726
727 def test_suite():
728     loader = subunit.tests.TestUtil.TestLoader()
729     result = loader.loadTestsFromName(__name__)
730     return result