1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/test.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,96 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +# This Source Code Form is subject to the terms of the Mozilla Public 1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.9 + 1.10 +""" 1.11 +run mozbase tests from a manifest, 1.12 +by default https://github.com/mozilla/mozbase/blob/master/test-manifest.ini 1.13 +""" 1.14 + 1.15 +import copy 1.16 +import imp 1.17 +import manifestparser 1.18 +import mozinfo 1.19 +import optparse 1.20 +import os 1.21 +import sys 1.22 +import unittest 1.23 + 1.24 +from moztest.results import TestResultCollection 1.25 + 1.26 + 1.27 +here = os.path.dirname(os.path.abspath(__file__)) 1.28 + 1.29 +def unittests(path): 1.30 + """return the unittests in a .py file""" 1.31 + 1.32 + path = os.path.abspath(path) 1.33 + unittests = [] 1.34 + assert os.path.exists(path) 1.35 + directory = os.path.dirname(path) 1.36 + sys.path.insert(0, directory) # insert directory into path for top-level imports 1.37 + modname = os.path.splitext(os.path.basename(path))[0] 1.38 + module = imp.load_source(modname, path) 1.39 + sys.path.pop(0) # remove directory from global path 1.40 + loader = unittest.TestLoader() 1.41 + suite = loader.loadTestsFromModule(module) 1.42 + for test in suite: 1.43 + unittests.append(test) 1.44 + return unittests 1.45 + 1.46 +def main(args=sys.argv[1:]): 1.47 + 1.48 + # parse command line options 1.49 + usage = '%prog [options] manifest.ini <manifest.ini> <...>' 1.50 + parser = optparse.OptionParser(usage=usage, description=__doc__) 1.51 + parser.add_option('-b', "--binary", 1.52 + dest="binary", help="Binary path", 1.53 + metavar=None, default=None) 1.54 + parser.add_option('--list', dest='list_tests', 1.55 + action='store_true', default=False, 1.56 + help="list paths of tests to be run") 1.57 + options, args = parser.parse_args(args) 1.58 + 1.59 + # read the manifest 1.60 + if args: 1.61 + manifests = args 1.62 + else: 1.63 + manifests = [os.path.join(here, 'test-manifest.ini')] 1.64 + missing = [] 1.65 + for manifest in manifests: 1.66 + # ensure manifests exist 1.67 + if not os.path.exists(manifest): 1.68 + missing.append(manifest) 1.69 + assert not missing, 'manifest(s) not found: %s' % ', '.join(missing) 1.70 + manifest = manifestparser.TestManifest(manifests=manifests) 1.71 + 1.72 + if options.binary: 1.73 + # A specified binary should override the environment variable 1.74 + os.environ['BROWSER_PATH'] = options.binary 1.75 + 1.76 + # gather the tests 1.77 + tests = manifest.active_tests(disabled=False, **mozinfo.info) 1.78 + tests = [test['path'] for test in tests] 1.79 + if options.list_tests: 1.80 + # print test paths 1.81 + print '\n'.join(tests) 1.82 + sys.exit(0) 1.83 + 1.84 + # create unittests 1.85 + unittestlist = [] 1.86 + for test in tests: 1.87 + unittestlist.extend(unittests(test)) 1.88 + 1.89 + # run the tests 1.90 + suite = unittest.TestSuite(unittestlist) 1.91 + runner = unittest.TextTestRunner(verbosity=2) # default=1 does not show success of unittests 1.92 + unittest_results = runner.run(suite) 1.93 + results = TestResultCollection.from_unittest_results(None, unittest_results) 1.94 + 1.95 + # exit according to results 1.96 + sys.exit(1 if results.num_failures else 0) 1.97 + 1.98 +if __name__ == '__main__': 1.99 + main()