subunit: Update to latest upstream version.
[metze/samba/wip.git] / lib / subunit / python / subunit / tests / test_run.py
1 #
2 #  subunit: extensions to python unittest to get test results from subprocesses.
3 #  Copyright (C) 2011  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 from testtools.compat import BytesIO
18 import unittest
19
20 from testtools import PlaceHolder
21
22 import subunit
23 from subunit.run import SubunitTestRunner
24
25
26 def test_suite():
27     loader = subunit.tests.TestUtil.TestLoader()
28     result = loader.loadTestsFromName(__name__)
29     return result
30
31
32 class TimeCollectingTestResult(unittest.TestResult):
33
34     def __init__(self, *args, **kwargs):
35         super(TimeCollectingTestResult, self).__init__(*args, **kwargs)
36         self.time_called = []
37
38     def time(self, a_time):
39         self.time_called.append(a_time)
40
41
42 class TestSubunitTestRunner(unittest.TestCase):
43
44     def test_includes_timing_output(self):
45         io = BytesIO()
46         runner = SubunitTestRunner(stream=io)
47         test = PlaceHolder('name')
48         runner.run(test)
49         client = TimeCollectingTestResult()
50         io.seek(0)
51         subunit.TestProtocolServer(client).readFrom(io)
52         self.assertTrue(len(client.time_called) > 0)