pygame is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againpygame / run_tests.py
- Tag
- release_1_8_1rc1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | #!/usr/bin/env python
"""
Test runner for pygame unittests:
By default, runs all test/xxxx_test.py files in a single process.
Option to run tests in subprocesses using subprocess and async_sub. Will poll
tests for return code and if tests don't return after TIME_OUT, will kill
process with os.kill.
os.kill is defined on win32 platform using subprocess.Popen to call either
pskill or taskkill if on the system $PATH. If not, the script will raise
SystemExit.
taskkill is shipped with windows from XP on.
pskill is available from SysInternals website
Dependencies:
async_sub.py:
Requires win32 extensions when run on windows:
Maybe able to get away with win32file.pyd, win32pipe.pyd zipped to
about 35kbytes and ship with that.
"""
#################################### IMPORTS ###################################
import sys, os, re, unittest, subprocess, time, optparse
import pygame.threads
# async_sub imported if needed when run in subprocess mode
main_dir = os.path.split(os.path.abspath(sys.argv[0]))[0]
test_subdir = os.path.join(main_dir, 'test')
fake_test_subdir = os.path.join(test_subdir, 'run_tests__tests')
sys.path.insert(0, test_subdir)
# sys.path.append( os.path.join(os.path.dirname(__file__), "async_libs.zip") )
import test_utils
################################### CONSTANTS ##################################
# Defaults:
# See optparse options below for more options
#
# If an xxxx_test.py takes longer than TIME_OUT seconds it will be killed
# This is only the default, can be over-ridden on command line
TIME_OUT = 30
# Any tests in IGNORE will not be ran
IGNORE = (
"scrap_test",
)
# Subprocess has less of a need to worry about interference between tests
SUBPROCESS_IGNORE = (
"scrap_test",
)
################################################################################
COMPLETE_FAILURE_TEMPLATE = """
======================================================================
ERROR: all_tests_for (%s.AllTestCases)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test\%s.py", line 1, in all_tests_for
subprocess completely failed with return code of %s
cmd: %s
return (abbrv):
%s
""" # Leave that last empty line else build page regex won't match
RAN_TESTS_DIV = (70 * "-") + "\nRan"
DOTS = re.compile("^([FE.]+)$", re.MULTILINE)
TEST_MODULE_RE = re.compile('^(.+_test)\.py$')
################################################################################
# Set the command line options
#
USEAGE = """
Runs all the test/xxxx_test.py tests.
"""
opt_parser = optparse.OptionParser(USEAGE)
opt_parser.add_option(
"-v", "--verbose", action = 'store_true',
help = "be verbose in output (only single process mode)" )
opt_parser.add_option (
"-i", "--incomplete", action = 'store_true',
help = "fail incomplete tests (only single process mode)" )
opt_parser.add_option (
"-s", "--subprocess", action = 'store_true',
help = "run test suites in subprocesses (default: same process)" )
opt_parser.add_option (
"-m", "--multi_thread", metavar = 'THREADS', type = 'int',
help = "run subprocessed tests in x THREADS" )
opt_parser.add_option (
"-t", "--time_out", metavar = 'SECONDS', type = 'int', default = TIME_OUT,
help = "kill stalled subprocessed tests after SECONDS" )
opt_parser.add_option (
"-f", "--fake", metavar = "DIR",
help = "run fake tests in %s%s$DIR" % (fake_test_subdir, os.path.sep) )
opt_parser.add_option (
"-p", "--python", metavar = "PYTHON", default = sys.executable,
help = "path to python excutable to run subproccesed tests\n"
"default (sys.executable): %s" % sys.executable)
# can be used for testing ret_code resilience
options, args = opt_parser.parse_args()
################################################################################
# Change to working directory and compile a list of test modules
# If options.fake, then compile list of fake xxxx_test.py from run_tests__tests
# this is used for testing subprocess output against single process mode
if options.fake:
test_subdir = os.path.join(fake_test_subdir, options.fake )
sys.path.append(test_subdir)
os.chdir(main_dir)
test_modules = []
for f in os.listdir(test_subdir):
for match in TEST_MODULE_RE.findall(f):
test_modules.append(match)
################################################################################
# Run all the tests in one process
# unittest.TextTestRunner().run(unittest.TestSuite())
#
if not options.subprocess:
suite = unittest.TestSuite()
runner = unittest.TextTestRunner()
for module in [m for m in test_modules if m not in IGNORE]:
print 'loading ' + module
__import__( module )
test = unittest.defaultTestLoader.loadTestsFromName( module )
suite.addTest( test )
test_utils.fail_incomplete_tests = options.incomplete
if options.verbose:
runner.verbosity = 2
runner.run( suite )
sys.exit()
###########################
# SYS.EXIT() FLOW CONTROL #
###########################
################################################################################
# Runs an individual xxxx_test.py test suite in a subprocess
#
import async_sub
def run_test(cmd):
module = os.path.basename(cmd).split('.')[0]
print 'loading %s' % module
ret_code, response = async_sub.proc_in_time_or_kill (
cmd, time_out=options.time_out
)
return cmd, module, ret_code, response
################################################################################
# Run all the tests in subprocesses
#
test_cmd = ('%s %s/' % (options.python, test_subdir)) + '%s.py'
# test_cmd += flags and options to pass on
test_cmds = [ test_cmd % m for m in test_modules if
m not in SUBPROCESS_IGNORE ]
t = time.time()
if options.multi_thread:
test_results = pygame.threads.tmap (
run_test, test_cmds,
stop_on_error = False,
num_workers = options.multi_thread
)
else:
test_results = map(run_test, test_cmds)
t = time.time() - t
################################################################################
# Combine subprocessed TextTestRunner() results to mimick single run
# Puts complete failures in a form the build page will pick up
all_dots = ''
failures = []
complete_failures = 0
for cmd, module, ret_code, ret in test_results:
if ret_code and RAN_TESTS_DIV not in ret:
ret = ''.join(ret.splitlines(1)[:5])
failures.append (
COMPLETE_FAILURE_TEMPLATE % (module, module, ret_code, cmd, ret)
)
complete_failures += 1
continue
dots = DOTS.search(ret)
if not dots: continue # in case of empty xxxx_test.py
else: dots = dots.group(1)
all_dots += dots
if 'E' in dots or 'F' in dots:
failure = ret[len(dots):].split(RAN_TESTS_DIV)[0]
failures.append (
failure.replace( "(__main__.", "(%s." % module)
)
total_fails, total_errors = all_dots.count('F'), all_dots.count('E')
total_tests = len(all_dots)
print all_dots
if failures: print ''.join(failures).lstrip('\n')[:-1]
print "%s %s tests in %.3fs\n" % (RAN_TESTS_DIV, total_tests, t)
if not failures:
print 'OK'
else:
print 'FAILED (%s)' % ', '.join (
(total_fails and ["failures=%s" % total_fails] or []) +
(total_errors and ["errors=%s" % total_errors] or [])
)
################################################################################
|