alltests.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/sh
  2. ''''exec python -u "$0" "$@" #'''
  3. # $Id: alltests.py 8124 2017-06-23 02:29:16Z goodger $
  4. # Author: David Goodger <goodger@python.org>
  5. # Copyright: This module has been placed in the public domain.
  6. __doc__ = \
  7. """
  8. All modules named 'test_*.py' in the current directory, and recursively in
  9. subdirectories (packages) called 'test_*', are loaded and test suites within
  10. are run.
  11. """
  12. import time
  13. # Start point for actual elapsed time, including imports
  14. # and setup outside of unittest.
  15. start = time.time()
  16. import sys
  17. import os
  18. import platform
  19. import DocutilsTestSupport # must be imported before docutils
  20. import docutils
  21. class Tee:
  22. """Write to a file and a stream (default: stdout) simultaneously."""
  23. def __init__(self, filename, stream=sys.__stdout__):
  24. self.file = open(filename, 'w')
  25. self.stream = stream
  26. self.encoding = getattr(stream, 'encoding', None)
  27. def write(self, string):
  28. try:
  29. self.stream.write(string)
  30. self.file.write(string)
  31. except UnicodeEncodeError: # Py3k writing to "ascii" stream/file
  32. string = string.encode('raw_unicode_escape').decode('ascii')
  33. self.stream.write(string)
  34. self.file.write(string)
  35. def flush(self):
  36. self.stream.flush()
  37. self.file.flush()
  38. def pformat(suite):
  39. step = 4
  40. suitestr = repr(suite).replace('=[<', '=[\n<').replace(', ', ',\n')
  41. indent = 0
  42. output = []
  43. for line in suitestr.splitlines():
  44. output.append(' ' * indent + line)
  45. if line[-1:] == '[':
  46. indent += step
  47. else:
  48. if line [-5:] == ']>]>,':
  49. indent -= step * 2
  50. elif line[-3:] == ']>,':
  51. indent -= step
  52. return '\n'.join(output)
  53. def suite():
  54. path, script = os.path.split(sys.argv[0])
  55. suite = package_unittest.loadTestModules(DocutilsTestSupport.testroot,
  56. 'test_', packages=1)
  57. sys.stdout.flush()
  58. return suite
  59. # must redirect stderr *before* first import of unittest
  60. sys.stdout = sys.stderr = Tee('alltests.out')
  61. import package_unittest
  62. if __name__ == '__main__':
  63. suite = suite()
  64. print ('Testing Docutils %s with Python %s on %s at %s'
  65. % (docutils.__version__, sys.version.split()[0],
  66. time.strftime('%Y-%m-%d'), time.strftime('%H:%M:%S')))
  67. print ('OS: %s %s %s (%s, %s)'
  68. % (platform.system(), platform.release(), platform.version(),
  69. sys.platform, platform.platform()))
  70. print 'Working directory: %s' % os.getcwd()
  71. print 'Docutils package: %s' % os.path.dirname(docutils.__file__)
  72. sys.stdout.flush()
  73. result = package_unittest.main(suite)
  74. #if package_unittest.verbosity > 1:
  75. # print >>sys.stderr, pformat(suite) # check the test suite
  76. finish = time.time()
  77. print 'Elapsed time: %.3f seconds' % (finish - start)
  78. sys.exit(not result.wasSuccessful())