Skip to content

Instantly share code, notes, and snippets.

@jdriscoll
Created November 21, 2009 15:30
Show Gist options
  • Save jdriscoll/240166 to your computer and use it in GitHub Desktop.
Save jdriscoll/240166 to your computer and use it in GitHub Desktop.
# Unit testing for AppEngine
import os
import sys
import unittest
# Path to AppEngine library (platform specific)
SDK_PATH = os.path.realpath('/usr/local/google_appengine/')
EXTRA_PATHS = [
SDK_PATH,
os.path.join(SDK_PATH, 'lib', 'antlr3'),
os.path.join(SDK_PATH, 'lib', 'django'),
os.path.join(SDK_PATH, 'lib', 'webob'),
os.path.join(SDK_PATH, 'lib', 'yaml', 'lib'),
]
sys.path = sys.path[0:1] + EXTRA_PATHS + sys.path[1:]
#import the stubs, i.e. the fake datastore, user and mail service and urlfetch
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
from google.appengine.api import users
from google.appengine.api.memcache import memcache_stub
# Import Werkzeug WSGI test client and response wrapper
from werkzeug import Client, BaseResponse
class GAETestCase(unittest.TestCase):
""" Base class that sets up the environment
"""
def setUp(self):
# Start with a fresh api proxy.
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
# Use a fresh stub datastore.
# From this point on in the tests, all calls to the Data Store, such as get and put,
# will be to the temporary, in-memory datastore stub.
stub = datastore_file_stub.DatastoreFileStub(u'testapp',
'/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'] = 'gmail.com'
os.environ['USER_EMAIL'] = '[email protected]' # set to '' for no logged in user
os.environ['SERVER_NAME'] = 'example.com'
os.environ['SERVER_PORT'] = '9999'
os.environ['SERVER_SOFTWARE'] = 'Dev'
os.environ['APPLICATION_ID'] = 'testapp'
os.environ['CURRENT_VERSION_ID'] = '1'
# 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())
# Use a fresh mail stub.
apiproxy_stub_map.apiproxy.RegisterStub('memcache', memcache_stub.MemcacheServiceStub())
# Import and create WSGI application
from main import Application
from utils import local
self.app = Application()
self.local = local
class TestWSGI(GAETestCase):
""" Tests of the WSGI framework
"""
def test_wsgi_app(self):
c = Client(self.app, BaseResponse)
resp = c.get('/')
self.assertEquals(resp.status_code, 200)
def test_test_user(self):
user = users.get_current_user()
self.assertEquals(user, users.User('[email protected]'))
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment