Bug 932076 - Add check for MediaExtractor creation failure. r=doublec
[gecko.git] / testing / mozbase / test.py
blobc0f62b1beb09e2a3592762a0f66517d963de3374
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 imp
13 import manifestparser
14 import mozinfo
15 import optparse
16 import os
17 import sys
18 import unittest
20 from moztest.results import TestResultCollection
22 here = os.path.dirname(os.path.abspath(__file__))
24 def unittests(path):
25 """return the unittests in a .py file"""
27 path = os.path.abspath(path)
28 unittests = []
29 assert os.path.exists(path)
30 directory = os.path.dirname(path)
31 sys.path.insert(0, directory) # insert directory into path for top-level imports
32 modname = os.path.splitext(os.path.basename(path))[0]
33 module = imp.load_source(modname, path)
34 sys.path.pop(0) # remove directory from global path
35 loader = unittest.TestLoader()
36 suite = loader.loadTestsFromModule(module)
37 for test in suite:
38 unittests.append(test)
39 return unittests
41 def main(args=sys.argv[1:]):
43 # parse command line options
44 usage = '%prog [options] manifest.ini <manifest.ini> <...>'
45 parser = optparse.OptionParser(usage=usage, description=__doc__)
46 parser.add_option('--list', dest='list_tests',
47 action='store_true', default=False,
48 help="list paths of tests to be run")
49 options, args = parser.parse_args(args)
51 # read the manifest
52 if args:
53 manifests = args
54 else:
55 manifests = [os.path.join(here, 'test-manifest.ini')]
56 missing = []
57 for manifest in manifests:
58 # ensure manifests exist
59 if not os.path.exists(manifest):
60 missing.append(manifest)
61 assert not missing, 'manifest(s) not found: %s' % ', '.join(missing)
62 manifest = manifestparser.TestManifest(manifests=manifests)
64 # gather the tests
65 tests = manifest.active_tests(disabled=False, **mozinfo.info)
66 tests = [test['path'] for test in tests]
67 if options.list_tests:
68 # print test paths
69 print '\n'.join(tests)
70 sys.exit(0)
72 # create unittests
73 unittestlist = []
74 for test in tests:
75 unittestlist.extend(unittests(test))
77 # run the tests
78 suite = unittest.TestSuite(unittestlist)
79 runner = unittest.TextTestRunner(verbosity=2) # default=1 does not show success of unittests
80 unittest_results = runner.run(suite)
81 results = TestResultCollection.from_unittest_results(None, unittest_results)
83 # exit according to results
84 sys.exit(1 if results.num_failures else 0)
86 if __name__ == '__main__':
87 main()