2 # subunit: extensions to python unittest to get test results from subprocesses.
3 # Copyright (C) 2013 Thomi Richards <thomi.richards@canonical.com>
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.
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.
16 from argparse import ArgumentParser
18 from sys import stdout
20 from subunit.v2 import StreamResultToBytes
23 args = parse_arguments()
24 output = get_output_stream_writer()
25 generate_bytestream(args, output)
30 def parse_arguments(args=None):
31 """Parse arguments from the command line.
33 If specified, args must be a list of strings, similar to sys.argv[1:].
36 parser = ArgumentParser(
37 prog='subunit-output',
38 description="A tool to generate a subunit result byte-stream",
41 common_args = ArgumentParser(add_help=False)
42 common_args.add_argument("test_id", help="""A string that uniquely
43 identifies this test.""")
44 sub_parsers = parser.add_subparsers(dest="action")
46 parser_start = sub_parsers.add_parser("start", help="Start a test.",
47 parents=[common_args])
49 parser_pass = sub_parsers.add_parser("pass", help="Pass a test.",
50 parents=[common_args])
52 parser_fail = sub_parsers.add_parser("fail", help="Fail a test.",
53 parents=[common_args])
55 parser_skip = sub_parsers.add_parser("skip", help="Skip a test.",
56 parents=[common_args])
58 return parser.parse_args(args)
61 def translate_command_name(command_name):
62 """Turn the friendly command names we show users on the command line into
63 something subunit understands.
67 'start': 'inprogress',
69 }.get(command_name, command_name)
72 def get_output_stream_writer():
73 return StreamResultToBytes(stdout)
76 def generate_bytestream(args, output_writer):
77 output_writer.startTestRun()
80 test_status=translate_command_name(args.action),
81 timestamp=create_timestamp()
83 output_writer.stopTestRun()
86 _ZERO = datetime.timedelta(0)
89 class UTC(datetime.tzinfo):
91 def utcoffset(self, dt):
102 def create_timestamp():
103 return datetime.datetime.now(utc)