2021-07-10 14:34:17+00:00

Unit testing Google App Engine applications locally is difficult because the code relies on GAE's proprietary runtime services (like the NDB Datastore, Memcache, and TaskQueue APIs). If you run test cases without initializing these APIs, the library calls will throw null pointer or runtime environment errors. To solve this, Google Cloud provides the testbed library, which mocks GAE services locally in memory, allowing you to test database access code without a live connection.

By implementing a custom test base class that configures testbed stubs, we can write fast and isolated unit tests.


1. Building the Testbed Base Class

We write a base test case class that initializes the testbed container, registers Datastore and Memcache stubs before each test, and tears them down after execution:

# test_base.py
import unittest
from google.appengine.ext import testbed

class GAETestCase(unittest.TestCase):
    def setUp(self):
        # Create and activate the testbed environment
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        
        # Initialize in-memory stubs
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.testbed.init_taskqueue_stub()
        
    def tearDown(self):
        # Deactivate the testbed to clean up environmental side effects
        self.testbed.deactivate()

# Example test case using our GAE Base class
from google.cloud import ndb

class UserTest(GAETestCase):
    def test_user_creation(self):
        client = ndb.Client()
        with client.context():
            # Test database operations on mock in-memory store
            user = UserEntity(username="test_dev")
            user.put()
            
            queried = UserEntity.query().get()
            self.assertEqual(queried.username, "test_dev")

2. Speeding Up Tests

Using these local memory stubs avoids the need for external network calls during unit testing. This enables large test suites containing hundreds of test files to run in under ten seconds, integrating cleanly with local watch loops.

Deterministic Unit Testing with GAE Testbed & Cloud Emulator Suites

Isolate NDB Datastore, Memcache, and Task Queue stubs per test case so unit test suites execute in milliseconds without external cloud dependencies.


About the Author — Hire Freelance Programmer Aldian Fazrihady

Aldian Fazrihady is a Top-Rated Senior Freelance Programmer, Freelance Full Stack Developer, and AI Application Engineer with 15+ years of production software engineering experience and Google Cloud Professional Cloud Architect & Data Engineer certifications. He specializes in building custom LLM & autonomous AI agents, RAG pipelines, Next.js/React PWAs, Python (Django/FastAPI), Go microservices, and serverless cloud infrastructure on Google Cloud Run and AWS.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD via Polar) | View Live AI Portfolio | Portfolio Directory | Send a Direct Message

Related Engineering Articles

Freelance Programmer Specializations & Category Hubs