Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5959175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:38:53+00:00 2026-05-22T18:38:53+00:00

I am using the Google App Engine testbed framework to write test cases with

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:38:54+00:00Added an answer on May 22, 2026 at 6:38 pm

    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:

    • USER_EMAIL: The user’s email address, and the user’s nickname.
    • USER_ID: The user’s unique Google ID (string).
    • USER_IS_ADMIN: “0” if the user is non-admin, “1” if the user is an admin.

    You can use os.environ to 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 on tearDown, 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:

    import os
    
    def setCurrentUser(email, user_id, is_admin=False):
        os.environ['USER_EMAIL'] = email or ''
        os.environ['USER_ID'] = user_id or ''
        os.environ['USER_IS_ADMIN'] = '1' if is_admin else '0'
    
    def logoutCurrentUser():
        setCurrentUser(None, None)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Google App Engine and Django templates. I have a table that I
As the title suggests, I'm using Google App Engine and Django. I have quite
I've been using Pydev/Eclipse to develop Google App Engine (GAE) applications but I've been
I have a simple Google App Engine app, that I wrote using ordinary strings.
Using Google App Engine and Django. Whenever I have a db.ReferenceProperty() inside a model
Using Google App Engine SDK and Python, I'm facing an issue : I'm unable
Using Google app engine, is it possible to initialize a globally accessible singleton on
When using Google app engine is there any benefit to use a CDN if
I'm using Google App Engine, Jquery and Django. I want POST data to be
I want to develop some web apps using Google app engine. I had deployed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.