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