Remove unnecessary python path updates for bundled subunit/testtools.
[nivanova/samba-autobuild/.git] / lib / 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 subunit.test_results import AutoTimingTestResultDecorator
27 from testtools.run import (
28     BUFFEROUTPUT,
29     CATCHBREAK,
30     FAILFAST,
31     TestProgram,
32     USAGE_AS_MAIN,
33     )
34
35
36 class SubunitTestRunner(object):
37     def __init__(self, verbosity=None, failfast=None, buffer=None, stream=None):
38         """Create a TestToolsTestRunner.
39
40         :param verbosity: Ignored.
41         :param failfast: Stop running tests at the first failure.
42         :param buffer: Ignored.
43         """
44         self.failfast = failfast
45         self.stream = stream or sys.stdout
46
47     def run(self, test):
48         "Run the given test case or test suite."
49         result = TestProtocolClient(self.stream)
50         result = AutoTimingTestResultDecorator(result)
51         if self.failfast is not None:
52             result.failfast = self.failfast
53         test(result)
54         return result
55
56
57 class SubunitTestProgram(TestProgram):
58
59     USAGE = USAGE_AS_MAIN
60
61     def usageExit(self, msg=None):
62         if msg:
63             print (msg)
64         usage = {'progName': self.progName, 'catchbreak': '', 'failfast': '',
65                  'buffer': ''}
66         if self.failfast != False:
67             usage['failfast'] = FAILFAST
68         if self.catchbreak != False:
69             usage['catchbreak'] = CATCHBREAK
70         if self.buffer != False:
71             usage['buffer'] = BUFFEROUTPUT
72         usage_text = self.USAGE % usage
73         usage_lines = usage_text.split('\n')
74         usage_lines.insert(2, "Run a test suite with a subunit reporter.")
75         usage_lines.insert(3, "")
76         print('\n'.join(usage_lines))
77         sys.exit(2)
78
79
80 if __name__ == '__main__':
81     stream = get_default_formatter()
82     runner = SubunitTestRunner
83     SubunitTestProgram(module=None, argv=sys.argv, testRunner=runner,
84         stdout=sys.stdout)