michael@0: #!/usr/bin/env python michael@0: michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: """ michael@0: run mozbase tests from a manifest, michael@0: by default https://github.com/mozilla/mozbase/blob/master/test-manifest.ini michael@0: """ michael@0: michael@0: import copy michael@0: import imp michael@0: import manifestparser michael@0: import mozinfo michael@0: import optparse michael@0: import os michael@0: import sys michael@0: import unittest michael@0: michael@0: from moztest.results import TestResultCollection michael@0: michael@0: michael@0: here = os.path.dirname(os.path.abspath(__file__)) michael@0: michael@0: def unittests(path): michael@0: """return the unittests in a .py file""" michael@0: michael@0: path = os.path.abspath(path) michael@0: unittests = [] michael@0: assert os.path.exists(path) michael@0: directory = os.path.dirname(path) michael@0: sys.path.insert(0, directory) # insert directory into path for top-level imports michael@0: modname = os.path.splitext(os.path.basename(path))[0] michael@0: module = imp.load_source(modname, path) michael@0: sys.path.pop(0) # remove directory from global path michael@0: loader = unittest.TestLoader() michael@0: suite = loader.loadTestsFromModule(module) michael@0: for test in suite: michael@0: unittests.append(test) michael@0: return unittests michael@0: michael@0: def main(args=sys.argv[1:]): michael@0: michael@0: # parse command line options michael@0: usage = '%prog [options] manifest.ini <...>' michael@0: parser = optparse.OptionParser(usage=usage, description=__doc__) michael@0: parser.add_option('-b', "--binary", michael@0: dest="binary", help="Binary path", michael@0: metavar=None, default=None) michael@0: parser.add_option('--list', dest='list_tests', michael@0: action='store_true', default=False, michael@0: help="list paths of tests to be run") michael@0: options, args = parser.parse_args(args) michael@0: michael@0: # read the manifest michael@0: if args: michael@0: manifests = args michael@0: else: michael@0: manifests = [os.path.join(here, 'test-manifest.ini')] michael@0: missing = [] michael@0: for manifest in manifests: michael@0: # ensure manifests exist michael@0: if not os.path.exists(manifest): michael@0: missing.append(manifest) michael@0: assert not missing, 'manifest(s) not found: %s' % ', '.join(missing) michael@0: manifest = manifestparser.TestManifest(manifests=manifests) michael@0: michael@0: if options.binary: michael@0: # A specified binary should override the environment variable michael@0: os.environ['BROWSER_PATH'] = options.binary michael@0: michael@0: # gather the tests michael@0: tests = manifest.active_tests(disabled=False, **mozinfo.info) michael@0: tests = [test['path'] for test in tests] michael@0: if options.list_tests: michael@0: # print test paths michael@0: print '\n'.join(tests) michael@0: sys.exit(0) michael@0: michael@0: # create unittests michael@0: unittestlist = [] michael@0: for test in tests: michael@0: unittestlist.extend(unittests(test)) michael@0: michael@0: # run the tests michael@0: suite = unittest.TestSuite(unittestlist) michael@0: runner = unittest.TextTestRunner(verbosity=2) # default=1 does not show success of unittests michael@0: unittest_results = runner.run(suite) michael@0: results = TestResultCollection.from_unittest_results(None, unittest_results) michael@0: michael@0: # exit according to results michael@0: sys.exit(1 if results.num_failures else 0) michael@0: michael@0: if __name__ == '__main__': michael@0: main()