Commit cd939a50 authored by Denis Bilenko's avatar Denis Bilenko

testrunner.py: support passing args to test modules

parent 1aac3b2c
...@@ -179,8 +179,6 @@ def get_tempnam(): ...@@ -179,8 +179,6 @@ def get_tempnam():
def run_tests(options, args): def run_tests(options, args):
if len(args) != 1:
sys.exit('--record requires exactly one test module to run')
arg = args[0] arg = args[0]
module_name = arg module_name = arg
if module_name.endswith('.py'): if module_name.endswith('.py'):
...@@ -196,22 +194,19 @@ def run_tests(options, args): ...@@ -196,22 +194,19 @@ def run_tests(options, args):
unittest.TextTestRunner = _runner unittest.TextTestRunner = _runner
import test_support import test_support
test_support.BasicTestRunner = _runner test_support.BasicTestRunner = _runner
sys.argv = args
globals()['__file__'] = arg
if os.path.exists(arg): if os.path.exists(arg):
sys.argv = args execfile(arg, globals())
saved_globals = {'__file__': __file__}
try:
globals()['__file__'] = arg
# QQQ this makes tests reported as if they are from __main__ and screws up warnings location
execfile(arg, globals())
finally:
globals().update(saved_globals)
else: else:
test = defaultTestLoader.loadTestsFromName(arg) test = defaultTestLoader.loadTestsFromName(arg)
result = _runner().run(test) result = _runner().run(test)
sys.exit(not result.wasSuccessful()) sys.exit(not result.wasSuccessful())
def run_subprocess(arg, options): def run_subprocess(args, options):
from threading import Timer from threading import Timer
from mysubprocess import Popen, PIPE, STDOUT from mysubprocess import Popen, PIPE, STDOUT
...@@ -220,7 +215,7 @@ def run_subprocess(arg, options): ...@@ -220,7 +215,7 @@ def run_subprocess(arg, options):
'--verbosity', options.verbosity] '--verbosity', options.verbosity]
if options.db: if options.db:
popen_args += ['--db', options.db] popen_args += ['--db', options.db]
popen_args += [arg] popen_args += args
popen_args = [str(x) for x in popen_args] popen_args = [str(x) for x in popen_args]
if options.capture: if options.capture:
popen = Popen(popen_args, stdout=PIPE, stderr=STDOUT, shell=False) popen = Popen(popen_args, stdout=PIPE, stderr=STDOUT, shell=False)
...@@ -231,7 +226,7 @@ def run_subprocess(arg, options): ...@@ -231,7 +226,7 @@ def run_subprocess(arg, options):
def killer(): def killer():
retcode.append('TIMEOUT') retcode.append('TIMEOUT')
print >> sys.stderr, 'Killing %s (%s) because of timeout' % (popen.pid, arg) print >> sys.stderr, 'Killing %s (%s) because of timeout' % (popen.pid, args)
popen.kill() popen.kill()
timeout = Timer(options.timeout, killer) timeout = Timer(options.timeout, killer)
...@@ -256,17 +251,17 @@ def run_subprocess(arg, options): ...@@ -256,17 +251,17 @@ def run_subprocess(arg, options):
finally: finally:
timeout.cancel() timeout.cancel()
# QQQ compensating for run_tests' screw up # QQQ compensating for run_tests' screw up
module_name = arg module_name = args[0]
if module_name.endswith('.py'): if module_name.endswith('.py'):
module_name = module_name[:-3] module_name = module_name[:-3]
output = output.replace(' (__main__.', ' (' + module_name + '.') output = output.replace(' (__main__.', ' (' + module_name + '.')
return retcode[0], output, output_printed return retcode[0], output, output_printed
def spawn_subprocess(arg, options, base_params): def spawn_subprocess(args, options, base_params):
success = False success = False
if options.db: if options.db:
module_name = arg module_name = args[0]
if module_name.endswith('.py'): if module_name.endswith('.py'):
module_name = module_name[:-3] module_name = module_name[:-3]
from datetime import datetime from datetime import datetime
...@@ -275,7 +270,7 @@ def spawn_subprocess(arg, options, base_params): ...@@ -275,7 +270,7 @@ def spawn_subprocess(arg, options, base_params):
'test': module_name}) 'test': module_name})
row_id = store_record(options.db, 'test', params) row_id = store_record(options.db, 'test', params)
params['id'] = row_id params['id'] = row_id
retcode, output, output_printed = run_subprocess(arg, options) retcode, output, output_printed = run_subprocess(args, options)
if len(output) > OUTPUT_LIMIT: if len(output) > OUTPUT_LIMIT:
output = output[:OUTPUT_LIMIT] + '<AbridgedOutputWarning>' output = output[:OUTPUT_LIMIT] + '<AbridgedOutputWarning>'
if retcode: if retcode:
...@@ -284,15 +279,15 @@ def spawn_subprocess(arg, options, base_params): ...@@ -284,15 +279,15 @@ def spawn_subprocess(arg, options, base_params):
else: else:
if not output_printed and options.verbosity >= -1: if not output_printed and options.verbosity >= -1:
sys.stdout.write(output) sys.stdout.write(output)
print '%s failed with code %s' % (arg, retcode) print '%s failed with code %s' % (' '.join(args), retcode)
elif retcode == 0: elif retcode == 0:
if not output_printed and options.verbosity >= 1: if not output_printed and options.verbosity >= 1:
sys.stdout.write(output) sys.stdout.write(output)
if options.verbosity >= 0: if options.verbosity >= 0:
print '%s passed' % arg print '%s passed' % ' '.join(args)
success = True success = True
else: else:
print '%s timed out' % arg print '%s timed out' % ' '.join(args)
if options.db: if options.db:
params['output'] = output params['output'] = output
params['retcode'] = retcode params['retcode'] = retcode
...@@ -311,7 +306,13 @@ def spawn_subprocesses(options, args): ...@@ -311,7 +306,13 @@ def spawn_subprocesses(options, args):
if not args: if not args:
args = glob.glob('test_*.py') args = glob.glob('test_*.py')
args.remove('test_support.py') args.remove('test_support.py')
real_args = []
for arg in args: for arg in args:
if os.path.exists(arg):
real_args.append([arg])
else:
real_args[-1].append(arg)
for arg in real_args:
try: try:
success = spawn_subprocess(arg, options, params) and success success = spawn_subprocess(arg, options, params) and success
except Exception: except Exception:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment