Running legacy Google App Engine applications locally typically requires launching the full GAE dev server (dev_appserver.py). As developers migrate applications to modern python environments, running the legacy dev server becomes hard to maintain. Developers need to run tests locally using lightweight standard test runners like pytest or serve applications using WSGI servers like Gunicorn. Mocking GAE APIs using stubs is essential to achieve this offline execution model.
By writing fallback classes that intercept NDB and Memcache calls, we can run App Engine applications locally without cloud dependencies.
1. Structuring Mock Memcache Stubs
When running outside the GAE sandbox, native memcache imports fail. We write a fallback wrapper using standard in-memory dictionaries or Redis clients:
# mock_memcache.py
class MockMemcacheClient:
def __init__(self):
self._store = {}
def get(self, key):
return self._store.get(key)
def set(self, key, val, time=0):
self._store[key] = val
return True
def delete(self, key):
self._store.pop(key, None)
return True
# Application bootloader fallback
try:
from google.appengine.api import memcache
except ImportError:
# Load fallback stub
memcache = MockMemcacheClient()
2. Enabling Fast Local Iterations
By loading these mock stubs automatically when local environment variables are detected, developers can execute python scripts and run test cases instantly without cloud dependencies.