lib.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #Licensed to the Apache Software Foundation (ASF) under one
  2. #or more contributor license agreements. See the NOTICE file
  3. #distributed with this work for additional information
  4. #regarding copyright ownership. The ASF licenses this file
  5. #to you under the Apache License, Version 2.0 (the
  6. #"License"); you may not use this file except in compliance
  7. #with the License. You may obtain a copy of the License at
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #Unless required by applicable law or agreed to in writing, software
  10. #distributed under the License is distributed on an "AS IS" BASIS,
  11. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. #See the License for the specific language governing permissions and
  13. #limitations under the License.
  14. import unittest, re, sys
  15. class BaseTestSuite():
  16. def __init__(self, name, excludes):
  17. self.name = name
  18. self.excludes = excludes
  19. pass
  20. def runTests(self):
  21. # Create a runner
  22. self.runner = unittest.TextTestRunner()
  23. # Get all the test-case classes
  24. # From module import *
  25. mod = __import__(self.name, fromlist=['*'])
  26. modItemsList = dir(mod)
  27. allsuites = []
  28. # Create all the test suites
  29. for modItem in modItemsList:
  30. if re.search(r"^test_", modItem):
  31. # Yes this is a test class
  32. if modItem not in self.excludes:
  33. test_class = getattr(mod, modItem)
  34. allsuites.append(unittest.makeSuite(test_class))
  35. # Create a master suite to be run.
  36. alltests = unittest.TestSuite(tuple(allsuites))
  37. # Run the master test suite.
  38. runner = self.runner.run(alltests)
  39. if(runner.wasSuccessful()): return 0
  40. printLine( "%s test(s) failed." % runner.failures.__len__())
  41. printLine( "%s test(s) threw errors." % runner.errors.__len__())
  42. return runner.failures.__len__() + runner.errors.__len__()
  43. def cleanUp(self):
  44. # suite tearDown
  45. pass
  46. def printLine(str):
  47. print >>sys.stderr, str
  48. def printSeparator():
  49. str = ""
  50. for i in range(0,79):
  51. str = str + "*"
  52. print >>sys.stderr, "\n", str, "\n"