I am using the Google App Engine testbed framework to write test cases with mock objects. This is documented here. I’ve got my datastore tests working nicely using the mock database (Testbed.init_datastore_v3_stub), and this lets my test cases run over a fast, fresh database which is re-initialised for each test case. Now I want to test functionality that depends on the current user.
There is another testbed service called Testbed.init_user_stub, which I can activate to get the “fake” user service. Unfortunately, there doesn’t seem to be any documentation for this one. I am activating and using it like this:
import unittest
from google.appengine.ext import testbed
from google.appengine.api import users
class MyTest(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_user_stub()
def testUser(self):
u = users.get_current_user()
self.assertNotEqual(u, None)
The problem is that I haven’t found any way to tell the “fake” user service to authenticate a “fake” user. So running that test, I (predictably) get
AssertionError: None == None
meaning the fake user service is telling my app that the current user is not logged in. How can I tell the fake user service to pretend that a user is logged in? Ideally, I’d like to be able to specify the fake user’s nickname, email, user_id and whether or not they are an admin. It seems like this would be quite a common thing to want (since you need to test how the app behaves when a) nobody is logged in, b) a user is logged in, and c) an admin is logged in), but googling “init_user_stub” returns almost nothing.
Note: If you want to test the above program, you need to add this to the top:
import sys
sys.path.append('/PATH/TO/APPENGINE/SDK')
import dev_appserver
dev_appserver.fix_sys_path()
and this to the bottom:
if __name__ == '__main__':
unittest.main()
Well I don’t think there is an official way to do it, but I have been reading the source code and I found a “hack” way to do it that is working well so far. (Normally I’d be worried about using undocumented behaviour, but it’s a test suite so it only matters if it works on the dev server.)
The dev server figures out the currently logged-in user based on three environment variables:
You can use
os.environto set these as you would any other environment variable, and they take immediate effect (obviously this won’t work on the production server). But you can use them with testbed’s user_stub and they will be reset when you deactivate the testbed (which you should do ontearDown, so you get a fresh environment for each test case).Since setting environment variables is a bit unwieldy, I wrote some wrapper functions to package them up: