Skip to content

Instantly share code, notes, and snippets.

@zaim
Created April 12, 2011 13:12
Show Gist options
  • Save zaim/915477 to your computer and use it in GitHub Desktop.
Save zaim/915477 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import optparse
import os
import sys
import unittest
test_path = os.path.abspath(os.path.dirname(__file__))
app_path = os.path.abspath(os.path.join(test_path, '..'))
sdk_path = os.environ.get('APP_ENGINE_SDK_PATH')
USAGE = """%prog [testname ...]
Run unit tests for this App Engine App.
testname A list of "dotted name" tests to run"""
def fix_path():
if not sdk_path:
print 'Environment variable "APP_ENGINE_SDK_PATH" not set'
return False
sys.path.insert(0, sdk_path)
try:
import dev_appserver
dev_appserver.fix_sys_path()
except ImportError:
print 'Failed to import App Engine, are you sure "%s" is the SDK path?' % sdk_path
return False
sys.path.insert(0, app_path)
# try to import special `boot.py` module located in `app_path`,
# define a function `fixpath` in that module and do further
# sys.path customizations there (e.g. add other 3rd party libraries
# or zip imports, etc)
try:
import boot
boot.fixpath()
except ImportError:
pass
return True
def load_suite(names=None):
if not names:
names = []
for name in os.listdir(test_path):
if name.startswith('test_') and name.endswith('.py'):
names.append(name[0:-3])
return unittest.defaultTestLoader.loadTestsFromNames(names)
def main():
if not fix_path():
return
parser = optparse.OptionParser(usage=USAGE)
parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False)
options, args = parser.parse_args()
verbose = int(options.verbose) + 1
suite = load_suite(args)
if suite:
unittest.TextTestRunner(verbosity=verbose).run(suite)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment