dotest.py revision a85d7eefef64b72eec99a91524dc3942d9683705
1#!/usr/bin/env python 2 3""" 4A simple testing framework for lldb using python's unit testing framework. 5 6Tests for lldb are written as python scripts which take advantage of the script 7bridging provided by LLDB.framework to interact with lldb core. 8 9A specific naming pattern is followed by the .py script to be recognized as 10a module which implements a test scenario, namely, Test*.py. 11 12To specify the directories where "Test*.py" python test scripts are located, 13you need to pass in a list of directory names. By default, the current 14working directory is searched if nothing is specified on the command line. 15""" 16 17import os 18import sys 19import unittest 20 21# 22# Global variables: 23# 24 25# The test suite. 26suite = unittest.TestSuite() 27 28# Default verbosity is 0. 29verbose = 0 30 31# By default, search from the current working directory. 32testdirs = [ os.getcwd() ] 33 34 35def usage(): 36 print """ 37Usage: dotest.py [option] [args] 38where options: 39-h : print this help message and exit (also --help) 40-v : do verbose mode of unittest framework 41 42and: 43args : specify a list of directory names to search for python Test*.py scripts 44 if empty, search from the curret working directory, instead 45""" 46 47 48def setupSysPath(): 49 """Add LLDB.framework/Resources/Python to the search paths for modules.""" 50 51 # Get the directory containing the current script. 52 testPath = sys.path[0] 53 if not testPath.endswith('test'): 54 print "This script expects to reside in lldb's test directory." 55 sys.exit(-1) 56 57 base = os.path.abspath(os.path.join(testPath, os.pardir)) 58 dbgPath = os.path.join(base, 'build', 'Debug', 'LLDB.framework', 59 'Resources', 'Python') 60 relPath = os.path.join(base, 'build', 'Release', 'LLDB.framework', 61 'Resources', 'Python') 62 63 lldbPath = None 64 if os.path.isfile(os.path.join(dbgPath, 'lldb.py')): 65 lldbPath = dbgPath 66 elif os.path.isfile(os.path.join(relPath, 'lldb.py')): 67 lldbPath = relPath 68 69 if not lldbPath: 70 print 'This script requires lldb.py to be in either ' + dbgPath, 71 print ' or' + relPath 72 sys.exit(-1) 73 74 sys.path.append(lldbPath) 75 76 77def initTestdirs(): 78 """Initialize the list of directories containing our unittest scripts. 79 80 '-h/--help as the first option prints out usage info and exit the program. 81 """ 82 83 global verbose 84 global testdirs 85 86 if len(sys.argv) == 1: 87 pass 88 elif sys.argv[1].find('-h') != -1: 89 # Print usage info and exit. 90 usage() 91 sys.exit(0) 92 else: 93 # Process possible verbose flag. 94 index = 1 95 if sys.argv[1].find('-v') != -1: 96 verbose = 2 97 index += 1 98 99 # Gather all the dirs passed on the command line. 100 if len(sys.argv) > index: 101 testdirs = map(os.path.abspath, sys.argv[index:]) 102 103 104def visit(prefix, dir, names): 105 """Visitor function for os.path.walk(path, visit, arg).""" 106 107 global suite 108 109 for name in names: 110 if os.path.isdir(os.path.join(dir, name)): 111 continue 112 113 if '.py' == os.path.splitext(name)[1] and name.startswith(prefix): 114 # We found a pattern match for our test case. Add it to the suite. 115 if not sys.path.count(dir): 116 sys.path.append(dir) 117 base = os.path.splitext(name)[0] 118 suite.addTests(unittest.defaultTestLoader.loadTestsFromName(base)) 119 120 121# 122# Start the actions by first setting up the module search path for lldb, 123# followed by initializing the test directories, and then walking the directory 124# trees, while collecting the tests into our test suite. 125# 126setupSysPath() 127initTestdirs() 128for testdir in testdirs: 129 os.path.walk(testdir, visit, 'Test') 130 131# Now that we have loaded all the test cases, run the whole test suite. 132unittest.TextTestRunner(verbosity=verbose).run(suite) 133