__main__.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #
  2. # Python unit test runner
  3. #
  4. # Author : Sander Mathijs van Veen <smvv@kompiler.org>
  5. # License: GPL version 3, see also the file `LICENSE'.
  6. #
  7. import sys
  8. import os
  9. import unittest
  10. from testrunner import TextTestRunner
  11. if len(sys.argv) < 2:
  12. print 'Usage: %s tests/test_COMPONENT.py' % sys.argv[0]
  13. sys.exit(1)
  14. # TODO parse verbosity option '-v', given in argv.
  15. verbosity = 0
  16. suites = []
  17. # Dynamic load the requested module containing the test cases.
  18. for testfile in sys.argv[1:]:
  19. module_dir, module_file = os.path.split(testfile)
  20. module_name, module_ext = os.path.splitext(module_file)
  21. module_obj = __import__(module_dir.replace('/', '.'),
  22. fromlist=[module_name])
  23. module_obj.__file__ = testfile
  24. globals()[module_name] = module_obj
  25. # Start the test runner and display the results to stdout.
  26. container = module_obj.__dict__[module_name]
  27. class_parts = module_name[5:].split('_')
  28. class_name = 'Test' + ''.join([p.capitalize() for p in class_parts])
  29. testcase = container.__dict__[class_name]
  30. suites += [unittest.TestLoader().loadTestsFromTestCase(testcase)]
  31. # Create the text runner and execute the tests.
  32. TextTestRunner(verbosity=verbosity).run(unittest.TestSuite(suites))