Skip to content

Instantly share code, notes, and snippets.

@tombatron
Created June 1, 2012 10:58
Show Gist options
  • Save tombatron/2851183 to your computer and use it in GitHub Desktop.
Save tombatron/2851183 to your computer and use it in GitHub Desktop.
Base tests for Google App Engine Projects (Python)
import os
import unittest
import xmlrpclib
from google.appengine.datastore import datastore_stub_util
from google.appengine.ext import testbed
from webtest.app import TestApp
import main
class TestingHighReplicationConsistencyPolicy(datastore_stub_util.BaseHighReplicationConsistencyPolicy):
def _ShouldApply(self, txn, meta_data):
return True
class BaseTest(unittest.TestCase):
def setUp(self):
self.app = TestApp(main.app)
self.testbed = testbed.Testbed()
self.testbed.activate()
self.data_store_policy = datastore_stub_util.BaseHighReplicationConsistencyPolicy()
self.testbed.init_datastore_v3_stub(consistency_policy=TestingHighReplicationConsistencyPolicy())
self.testbed.init_memcache_stub()
self.testbed.init_user_stub()
self.login('test@example.com')
def tearDown(self):
self.logout()
self.testbed.deactivate()
def logout(self):
os.environ.pop('USER_EMAIL', None)
os.environ.pop('USER_IS_ADMIN', None)
def login(self, email, admin=False):
self.logout()
os.environ['USER_EMAIL'] = email
if admin:
os.environ['USER_IS_ADMIN'] = '1'
class BaseXmlRpcTest(BaseTest):
def make_xml_rpc_request(self, url, method_name, *args):
xmlrpc_request_body = xmlrpclib.dumps(args, methodname=method_name)
http_response = self.app.post(url, xmlrpc_request_body)
processed_response = xmlrpclib.loads(http_response.body)[0][0]
return processed_response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment