So, being completely unconvinced of my own ability to produce bug free code, the first thing that came up when working with AppEngine was: how do I unit test my app?
I found a post in one of the Google groups (linked at the bottom) that discussed it but was either somewhat wrong or just out of date.
So the following worked for me:
The following was done with the standard Python 2.5 install for OS X, along with the AppEngine SDK for OS X.
It turns out that in addition to the imports I was using for my code I needed:
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
I also had to adjust the python path so I could get my unit tests to run outside of the dev_appserver instance.
export PYTHONPATH=/usr/local/google_appengine/:/usr/local/google_appengine/lib/django/:/usr/local/google_appengine/lib/webob/:/usr/local/google_appengine/lib/yaml/lib/
Keep in mind that the export above would be needed to be explicitly added to any startup scripts you’re using in order for it to be persistent.
Then the, hopefully, obvious import:
import unittest
Then there’s the step of setting up the environment for each test to run my setUp method follows:
def setUp(self): # Ensure we're in UTC. os.environ['TZ'] = 'UTC' time.tzset() # 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()) self.current_user = users.get_current_user()
The setup method was borrowed from this groups post