testing/mozbase/test.py

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rwxr-xr-x

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 #!/usr/bin/env python
     3 # This Source Code Form is subject to the terms of the Mozilla Public
     4 # License, v. 2.0. If a copy of the MPL was not distributed with this
     5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
     7 """
     8 run mozbase tests from a manifest,
     9 by default https://github.com/mozilla/mozbase/blob/master/test-manifest.ini
    10 """
    12 import copy
    13 import imp
    14 import manifestparser
    15 import mozinfo
    16 import optparse
    17 import os
    18 import sys
    19 import unittest
    21 from moztest.results import TestResultCollection
    24 here = os.path.dirname(os.path.abspath(__file__))
    26 def unittests(path):
    27     """return the unittests in a .py file"""
    29     path = os.path.abspath(path)
    30     unittests = []
    31     assert os.path.exists(path)
    32     directory = os.path.dirname(path)
    33     sys.path.insert(0, directory) # insert directory into path for top-level imports
    34     modname = os.path.splitext(os.path.basename(path))[0]
    35     module = imp.load_source(modname, path)
    36     sys.path.pop(0) # remove directory from global path
    37     loader = unittest.TestLoader()
    38     suite = loader.loadTestsFromModule(module)
    39     for test in suite:
    40         unittests.append(test)
    41     return unittests
    43 def main(args=sys.argv[1:]):
    45     # parse command line options
    46     usage = '%prog [options] manifest.ini <manifest.ini> <...>'
    47     parser = optparse.OptionParser(usage=usage, description=__doc__)
    48     parser.add_option('-b', "--binary",
    49                   dest="binary", help="Binary path",
    50                   metavar=None, default=None)
    51     parser.add_option('--list', dest='list_tests',
    52                       action='store_true', default=False,
    53                       help="list paths of tests to be run")
    54     options, args = parser.parse_args(args)
    56     # read the manifest
    57     if args:
    58         manifests = args
    59     else:
    60         manifests = [os.path.join(here, 'test-manifest.ini')]
    61     missing = []
    62     for manifest in manifests:
    63         # ensure manifests exist
    64         if not os.path.exists(manifest):
    65             missing.append(manifest)
    66     assert not missing, 'manifest(s) not found: %s' % ', '.join(missing)
    67     manifest = manifestparser.TestManifest(manifests=manifests)
    69     if options.binary:
    70         # A specified binary should override the environment variable
    71         os.environ['BROWSER_PATH'] = options.binary
    73     # gather the tests
    74     tests = manifest.active_tests(disabled=False, **mozinfo.info)
    75     tests = [test['path'] for test in tests]
    76     if options.list_tests:
    77         # print test paths
    78         print '\n'.join(tests)
    79         sys.exit(0)
    81     # create unittests
    82     unittestlist = []
    83     for test in tests:
    84         unittestlist.extend(unittests(test))
    86     # run the tests
    87     suite = unittest.TestSuite(unittestlist)
    88     runner = unittest.TextTestRunner(verbosity=2) # default=1 does not show success of unittests
    89     unittest_results = runner.run(suite)
    90     results = TestResultCollection.from_unittest_results(None, unittest_results)
    92     # exit according to results
    93     sys.exit(1 if results.num_failures else 0)
    95 if __name__ == '__main__':
    96     main()

mercurial