Merge Stewart Smith's branch to add timestamps to shell functions.
[third_party/subunit] / python / subunit / run.py
1 #!/usr/bin/python
2 #
3 # Simple subunit testrunner for python
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
5 #   
6 #  Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
7 #  license at the users choice. A copy of both licenses are available in the
8 #  project source as Apache-2.0 and BSD. You may not use this file except in
9 #  compliance with one of these two licences.
10 #  
11 #  Unless required by applicable law or agreed to in writing, software
12 #  distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
13 #  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14 #  license you chose for the specific language governing permissions and
15 #  limitations under that license.
16 #
17
18 """Run a unittest testcase reporting results as Subunit.
19
20   $ python -m subunit.run mylib.tests.test_suite
21 """
22
23 import sys
24
25 from subunit import TestProtocolClient, get_default_formatter
26 from testtools.run import (
27     BUFFEROUTPUT,
28     CATCHBREAK,
29     FAILFAST,
30     TestProgram,
31     USAGE_AS_MAIN,
32     )
33
34
35 class SubunitTestRunner(object):
36     def __init__(self, stream=sys.stdout):
37         self.stream = stream
38
39     def run(self, test):
40         "Run the given test case or test suite."
41         result = TestProtocolClient(self.stream)
42         test(result)
43         return result
44
45
46 class SubunitTestProgram(TestProgram):
47
48     USAGE = USAGE_AS_MAIN
49
50     def usageExit(self, msg=None):
51         if msg:
52             print (msg)
53         usage = {'progName': self.progName, 'catchbreak': '', 'failfast': '',
54                  'buffer': ''}
55         if self.failfast != False:
56             usage['failfast'] = FAILFAST
57         if self.catchbreak != False:
58             usage['catchbreak'] = CATCHBREAK
59         if self.buffer != False:
60             usage['buffer'] = BUFFEROUTPUT
61         usage_text = self.USAGE % usage
62         usage_lines = usage_text.split('\n')
63         usage_lines.insert(2, "Run a test suite with a subunit reporter.")
64         usage_lines.insert(3, "")
65         print('\n'.join(usage_lines))
66         sys.exit(2)
67
68
69 if __name__ == '__main__':
70     stream = get_default_formatter()
71     runner = SubunitTestRunner(stream)
72     SubunitTestProgram(module=None, argv=sys.argv, testRunner=runner,
73         stdout=sys.stdout)