· 8 years ago · May 16, 2018, 12:50 PM
1# Unit testing for AppEngine
2import os
3import sys
4import unittest
5
6# Path to AppEngine library (platform specific)
7SDK_PATH = os.path.realpath('/usr/local/google_appengine/')
8
9EXTRA_PATHS = [
10 SDK_PATH,
11 os.path.join(SDK_PATH, 'lib', 'antlr3'),
12 os.path.join(SDK_PATH, 'lib', 'django'),
13 os.path.join(SDK_PATH, 'lib', 'webob'),
14 os.path.join(SDK_PATH, 'lib', 'yaml', 'lib'),
15]
16
17sys.path = sys.path[0:1] + EXTRA_PATHS + sys.path[1:]
18
19#import the stubs, i.e. the fake datastore, user and mail service and urlfetch
20from google.appengine.api import apiproxy_stub_map
21from google.appengine.api import datastore_file_stub
22from google.appengine.api import mail_stub
23from google.appengine.api import urlfetch_stub
24from google.appengine.api import user_service_stub
25from google.appengine.api import users
26from google.appengine.api.memcache import memcache_stub
27
28# Import Werkzeug WSGI test client and response wrapper
29from werkzeug import Client, BaseResponse
30
31
32class GAETestCase(unittest.TestCase):
33 """ Base class that sets up the environment
34
35 """
36 def setUp(self):
37 # Start with a fresh api proxy.
38 apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
39
40 # Use a fresh stub datastore.
41 # From this point on in the tests, all calls to the Data Store, such as get and put,
42 # will be to the temporary, in-memory datastore stub.
43 stub = datastore_file_stub.DatastoreFileStub(u'testapp',
44 '/dev/null', '/dev/null')
45 apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)
46
47 # Use a fresh stub UserService.
48 apiproxy_stub_map.apiproxy.RegisterStub('user', user_service_stub.UserServiceStub())
49 os.environ['AUTH_DOMAIN'] = 'gmail.com'
50 os.environ['USER_EMAIL'] = 'testuser@example.com' # set to '' for no logged in user
51 os.environ['SERVER_NAME'] = 'example.com'
52 os.environ['SERVER_PORT'] = '9999'
53 os.environ['SERVER_SOFTWARE'] = 'Dev'
54 os.environ['APPLICATION_ID'] = 'testapp'
55 os.environ['CURRENT_VERSION_ID'] = '1'
56
57 # Use a fresh urlfetch stub.
58 apiproxy_stub_map.apiproxy.RegisterStub('urlfetch', urlfetch_stub.URLFetchServiceStub())
59
60 # Use a fresh mail stub.
61 apiproxy_stub_map.apiproxy.RegisterStub('mail', mail_stub.MailServiceStub())
62
63 # Use a fresh mail stub.
64 apiproxy_stub_map.apiproxy.RegisterStub('memcache', memcache_stub.MemcacheServiceStub())
65
66 # Import and create WSGI application
67 from main import Application
68 from utils import local
69 self.app = Application()
70 self.local = local
71
72
73class TestWSGI(GAETestCase):
74 """ Tests of the WSGI framework
75
76 """
77 def test_wsgi_app(self):
78 c = Client(self.app, BaseResponse)
79 resp = c.get('/')
80 self.assertEquals(resp.status_code, 200)
81
82 def test_test_user(self):
83 user = users.get_current_user()
84 self.assertEquals(user, users.User('testuser@example.com'))
85
86
87if __name__ == "__main__":
88 unittest.main()