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/.
8 run mozbase tests from a manifest,
9 by default https://github.com/mozilla/mozbase/blob/master/test-manifest.ini
20 from moztest
.results
import TestResultCollection
22 here
= os
.path
.dirname(os
.path
.abspath(__file__
))
25 """return the unittests in a .py file"""
27 path
= os
.path
.abspath(path
)
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
)
38 unittests
.append(test
)
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
)
55 manifests
= [os
.path
.join(here
, 'test-manifest.ini')]
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
)
65 tests
= manifest
.active_tests(disabled
=False, **mozinfo
.info
)
66 tests
= [test
['path'] for test
in tests
]
67 if options
.list_tests
:
69 print '\n'.join(tests
)
75 unittestlist
.extend(unittests(test
))
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__':