comparison tests/run.py @ 610:300b917d23c5

testrunner: use optparse
author Dan Villiom Podlaski Christiansen <danchr@gmail.com>
date Fri, 14 May 2010 17:39:26 +0200
parents 930bb6df19a0
children 58f397523604
comparison
equal deleted inserted replaced
609:aafbf0d40dc2 610:300b917d23c5
1 import optparse
1 import os 2 import os
2 import sys 3 import sys
3 import unittest 4 import unittest
4 5
5 import test_util 6 import test_util
34 def comprehensive(mod): 35 def comprehensive(mod):
35 dir = os.path.basename(os.path.dirname(mod.__file__)) 36 dir = os.path.basename(os.path.dirname(mod.__file__))
36 return dir == 'comprehensive' 37 return dir == 'comprehensive'
37 38
38 if __name__ == '__main__': 39 if __name__ == '__main__':
40 description = ("This script runs the hgsubversion tests. If no tests are "
41 "specified, all known tests are implied.")
42 parser = optparse.OptionParser(usage="%prog [options] [TESTS ...]",
43 description=description)
44 parser.add_option("-A", "--all",
45 dest="comprehensive", action="store_true", default=False,
46 help="include slow, but comprehensive tests")
47 parser.add_option("-v", "--verbose",
48 dest="verbose", action="store_true", default=False,
49 help="enable verbose output")
39 50
40 kwargs = {'descriptions': 2} 51 (options, args) = parser.parse_args()
41 if '-v' in sys.argv: 52
42 kwargs['descriptions'] = 3 53 if options.verbose:
43 kwargs['verbosity'] = 2 54 testargs = { 'descriptions': 3, 'verbosity': 2 }
55 else:
56 testargs = {'descriptions': 2}
44 57
45 # silence output when running outside nose 58 # silence output when running outside nose
46 sys.stdout = os.tmpfile() 59 sys.stdout = os.tmpfile()
47 60
48 all = globals() 61 all = globals()
49 all = dict((k, v) for (k, v) in all.iteritems() if k.startswith('test_')) 62 all = dict((k, v) for (k, v) in all.iteritems() if k.startswith('test_'))
50 del all['test_util'] 63 del all['test_util']
51 64
52 args = [i for i in sys.argv[1:] if i.startswith('test')]
53 args = [i.split('.py')[0].replace('-', '_') for i in args] 65 args = [i.split('.py')[0].replace('-', '_') for i in args]
54 66
55 if not args: 67 if not args:
56 check = lambda x: '-A' in sys.argv or not comprehensive(x) 68 check = lambda x: options.comprehensive or not comprehensive(x)
57 mods = [m for (n, m) in sorted(all.iteritems()) if check(m)] 69 mods = [m for (n, m) in sorted(all.iteritems()) if check(m)]
58 suite = [m.suite() for m in mods] 70 suite = [m.suite() for m in mods]
59 else: 71 else:
60 suite = [] 72 suite = []
61 for arg in args: 73 for arg in args:
62 if arg not in all: 74 if arg == 'test_util':
75 continue
76 elif arg not in all:
63 print >> sys.stderr, 'test module %s not available' % arg 77 print >> sys.stderr, 'test module %s not available' % arg
64 else: 78 else:
65 suite.append(all[arg].suite()) 79 suite.append(all[arg].suite())
66 80
67 runner = unittest.TextTestRunner(**kwargs) 81 runner = unittest.TextTestRunner(**testargs)
68 result = runner.run(unittest.TestSuite(suite)) 82 result = runner.run(unittest.TestSuite(suite))
69 if not result.wasSuccessful(): 83 if not result.wasSuccessful():
70 sys.exit(1) 84 sys.exit(1)