On May 4, 9:25 pm, Con <
conra...@gmail.com> wrote:
> Hey Duncan, how does one properly add the search path to the
> environment so that I can
> try the example below?
Try this (if your projects is in folder X then put this in test/
runtests.py). If you aren't running on Windows then GOOGLE_PATH will
need changing.
It will run any doctests in .py files in the main folder, and also any
text files with an extension .tests in the tests sub-folder.
-------------------------------------------------------------
import os, sys, glob
import unittest
import doctest
from os.path import dirname, basename, splitext, join
TESTDIR = dirname(__file__)
MAINDIR = dirname(TESTDIR)
GOOGLE_PATH = "C:/Program Files/Google/google_appengine"
EXTRA_PATHS = [
TESTDIR,
MAINDIR,
GOOGLE_PATH,
os.path.join(GOOGLE_PATH, 'lib', 'django'),
os.path.join(GOOGLE_PATH, 'lib', 'webob'),
os.path.join(GOOGLE_PATH, 'lib', 'yaml', 'lib'),
]
for directory in EXTRA_PATHS:
if not directory in sys.path:
sys.path.insert(0, directory)
from google.appengine.api import apiproxy_stub_map
from google.appengine.api import datastore_file_stub
from google.appengine.api import mail_stub
from google.appengine.api import urlfetch_stub
from google.appengine.api import user_service_stub
APP_ID = u'test_app'
AUTH_DOMAIN = '
gmail.com'
LOGGED_IN_USER = '
te...@example.com' # set to '' for no logged in
user
def setUp(self):
# Start with a fresh api proxy.
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
# Use a fresh stub datastore.
stub = datastore_file_stub.DatastoreFileStub(APP_ID, '/dev/null', '/
dev/null')
apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)
# Use a fresh stub UserService.
apiproxy_stub_map.apiproxy.RegisterStub('user',
user_service_stub.UserServiceStub())
os.environ['AUTH_DOMAIN'] = AUTH_DOMAIN
os.environ['USER_EMAIL'] = LOGGED_IN_USER
# Use a fresh urlfetch stub.
apiproxy_stub_map.apiproxy.RegisterStub(
'urlfetch', urlfetch_stub.URLFetchServiceStub())
# Use a fresh mail stub.
apiproxy_stub_map.apiproxy.RegisterStub(
'mail', mail_stub.MailServiceStub())
def initGlobs():
"""Create global variables for use in tests."""
return {}
def getModules():
"""return all modules in the parent directory"""
modules = [ splitext(basename(name))[0] for name in
glob.glob(join(MAINDIR, '*.py'))]
for mod in modules:
if not mod.startswith('_'):
yield __import__(mod)
def getDocTests():
"""Find doctest files in current directory"""
return glob.glob(join(TESTDIR, '*.tests'))
if __name__=='__main__':
suite = unittest.TestSuite()
for name in getDocTests():
suite.addTest(doctest.DocFileSuite(name,
module_relative=False))
for mod in getModules():
try:
suite.addTest(doctest.DocTestSuite(mod,
extraglobs=initGlobs(), setUp=setUp))
except ValueError, e:
print >>sys.stderr, e
runner = unittest.TextTestRunner()
runner.run(suite)
-----------------------------------------------------------------