01c0b0e9e6b35911b06db3e3a534769a1cbe41de
[ira/wip.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
27
28 class SubunitTestRunner(object):
29     def __init__(self, stream=sys.stdout):
30         self.stream = stream
31
32     def run(self, test):
33         "Run the given test case or test suite."
34         result = TestProtocolClient(self.stream)
35         test(result)
36         return result
37
38
39 if __name__ == '__main__':
40     import optparse
41     from unittest import TestProgram
42     parser = optparse.OptionParser(__doc__)
43     args = parser.parse_args()[1]
44     stream = get_default_formatter()
45     runner = SubunitTestRunner(stream)
46     program = TestProgram(module=None, argv=[sys.argv[0]] + args,
47                           testRunner=runner)