• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import getopt
18import os
19import sys
20import tf_runner
21from unittest import loader
22import unittest
23
24class TradefedProgram(unittest.TestProgram):
25    """ Main Runner Class that should be used to run the tests. This runner ensure that the
26    reporting is compatible with Tradefed.
27    """
28
29    def __init__(self, module='__main__', defaultTest=None,
30                 argv=None, testRunner=None,
31                 testLoader=loader.defaultTestLoader, exit=True,
32                 verbosity=1, failfast=None, catchbreak=None, buffer=None, serial=None):
33      self.serial = None
34      self.extra_options = []
35      super(TradefedProgram, self).__init__()
36
37    def parseArgs(self, argv):
38        if len(argv) > 1 and argv[1].lower() == 'discover':
39            self._do_discovery(argv[2:])
40            return
41
42        long_opts = ['help', 'verbose', 'quiet', 'failfast', 'catch', 'buffer',
43                     'serial=', 'extra_options=']
44        try:
45            options, args = getopt.getopt(argv[1:], 'hHvqfcbs:e:', long_opts)
46            for opt, value in options:
47                if opt in ('-h','-H','--help'):
48                    self.usageExit()
49                if opt in ('-q','--quiet'):
50                    self.verbosity = 0
51                if opt in ('-v','--verbose'):
52                    self.verbosity = 2
53                if opt in ('-f','--failfast'):
54                    if self.failfast is None:
55                        self.failfast = True
56                    # Should this raise an exception if -f is not valid?
57                if opt in ('-c','--catch'):
58                    if self.catchbreak is None and installHandler is not None:
59                        self.catchbreak = True
60                    # Should this raise an exception if -c is not valid?
61                if opt in ('-b','--buffer'):
62                    if self.buffer is None:
63                        self.buffer = True
64                    # Should this raise an exception if -b is not valid?
65                if opt in ('-s', '--serial'):
66                    if self.serial is None:
67                        self.serial = value
68                if opt in ('-e', '--extra_options'):
69                    self.extra_options.append(value)
70            if len(args) == 0 and self.defaultTest is None:
71                # createTests will load tests from self.module
72                self.testNames = None
73            elif len(args) > 0:
74                self.testNames = args
75                if __name__ == '__main__':
76                    # to support python -m unittest ...
77                    self.module = None
78            else:
79                self.testNames = (self.defaultTest,)
80            self.createTests()
81        except getopt.error, msg:
82            self.usageExit(msg)
83
84    def runTests(self):
85        if self.testRunner is None:
86            self.testRunner = tf_runner.TfTextTestRunner(verbosity=self.verbosity,
87                                                         failfast=self.failfast,
88                                                         buffer=self.buffer,
89                                                         resultclass=tf_runner.TextTestResult,
90                                                         serial=self.serial,
91                                                         extra_options=self.extra_options)
92        super(TradefedProgram, self).runTests()
93
94main = TradefedProgram
95
96def main_run():
97    TradefedProgram(module=None)
98