runtktests.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """
  2. Use this module to get and run all tk tests.
  3. tkinter tests should live in a package inside the directory where this file
  4. lives, like test_tkinter.
  5. Extensions also should live in packages following the same rule as above.
  6. """
  7. import os
  8. import importlib
  9. import test.support
  10. this_dir_path = os.path.abspath(os.path.dirname(__file__))
  11. def is_package(path):
  12. for name in os.listdir(path):
  13. if name in ('__init__.py', '__init__.pyc'):
  14. return True
  15. return False
  16. def get_tests_modules(basepath=this_dir_path, gui=True, packages=None):
  17. """This will import and yield modules whose names start with test_
  18. and are inside packages found in the path starting at basepath.
  19. If packages is specified it should contain package names that
  20. want their tests collected.
  21. """
  22. py_ext = '.py'
  23. for dirpath, dirnames, filenames in os.walk(basepath):
  24. for dirname in list(dirnames):
  25. if dirname[0] == '.':
  26. dirnames.remove(dirname)
  27. if is_package(dirpath) and filenames:
  28. pkg_name = dirpath[len(basepath) + len(os.sep):].replace('/', '.')
  29. if packages and pkg_name not in packages:
  30. continue
  31. filenames = filter(
  32. lambda x: x.startswith('test_') and x.endswith(py_ext),
  33. filenames)
  34. for name in filenames:
  35. try:
  36. yield importlib.import_module(
  37. ".%s.%s" % (pkg_name, name[:-len(py_ext)]),
  38. "tkinter.test")
  39. except test.support.ResourceDenied:
  40. if gui:
  41. raise
  42. def get_tests(text=True, gui=True, packages=None):
  43. """Yield all the tests in the modules found by get_tests_modules.
  44. If nogui is True, only tests that do not require a GUI will be
  45. returned."""
  46. attrs = []
  47. if text:
  48. attrs.append('tests_nogui')
  49. if gui:
  50. attrs.append('tests_gui')
  51. for module in get_tests_modules(gui=gui, packages=packages):
  52. for attr in attrs:
  53. for test in getattr(module, attr, ()):
  54. yield test
  55. if __name__ == "__main__":
  56. test.support.run_unittest(*get_tests())