test.py 3.2 KB

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