简体   繁体   中英

How do I mock users with GAE and Nosetest?

How do I call call setup_env() when nose instantiates testbed on it's own?

I'm trying to develop a App Engine app with TDD and I need to figure out how to mock users.

I start my tests with:
nosetests -v --with-gae

I had several people suggest using nosetests because it will make things easier. However, all the examples for how to mock things seem to explicitly use testbed.setup_env()
https://cloud.google.com/appengine/docs/python/tools/localunittesting
How do you mock the User service in App Engine?

I don't understand how I can set environmental values using nosetests.

This is what I have now:

import sys, os, subprocess, time, unittest, shlex     
sys.path.append("/usr/local/google_appengine")   
sys.path.append('/usr/local/google_appengine/lib/')     
sys.path.append("/usr/local/google_appengine/lib/yaml/lib")      
sys.path.append("/usr/local/google_appengine/lib/webapp2-2.5.2")      
sys.path.append("/usr/local/google_appengine/lib/django-1.5")      
sys.path.append("/usr/local/google_appengine/lib/cherrypy")      
sys.path.append("/usr/local/google_appengine/lib/concurrent")      
sys.path.append("/usr/local/google_appengine/lib/docker")      
sys.path.append("/usr/local/google_appengine/lib/requests")      
sys.path.append("/usr/local/google_appengine/lib/websocket")      
sys.path.append("/usr/local/google_appengine/lib/fancy_urllib")      
sys.path.append("/usr/local/google_appengine/lib/antlr3")      

os.environ['APPLICATION_ID'] = 'workout'   

from selenium import webdriver      
from selenium.webdriver.common.keys import Keys  

from google.appengine.api import memcache, apiproxy_stub, apiproxy_stub_map     
from google.appengine.ext import db      
from google.appengine.ext import testbed      
from google.appengine.datastore import datastore_stub_util       
from google.appengine.tools.devappserver2 import devappserver2      

class NewVisitorTest(unittest.TestCase):      
    # enable the datastore stub  
    nosegae_datastore_v3 = True  
    nosegae_datastore_v3_kwargs = {  
        'datastore_file': '/tmp/nosegae.sqlite3',  
        'use_sqlite': True  
    }  

    def setUp(self):      
        # Start the dev server    
       cmd = "/usr/local/bin/dev_appserver.py /Users/Bryan/work/GoogleAppEngine/workout_log/app.yaml --port 8080 --storage_path /tmp/datastore --clear_datastore --skip_sdk_update_check"    
       self.dev_appserver = subprocess.Popen(shlex.split(cmd),     
                                              stdout=subprocess.PIPE)    
       time.sleep(2) # Important, let dev_appserver start up    

       self.datastore_stub = apiproxy_stub_map.apiproxy.GetStub('datastore_v3')    

       self.browser = webdriver.Firefox()      
       self.browser.implicitly_wait(3)      

    def tearDown(self):      
        self.browser.quit()        
        self.dev_appserver.terminate()  

I think I figured this out myself. I added the statement below to the test setUp():

testself.testbed.setup_env(user_is_admin='1')

FWIW, you can configure the users stub the same way you configure the datastore stub.

Here is an example from the repo

Here are the supported configuration keys

class NewVisitorTest(unittest.TestCase):
    # enable the users stub
    nosegae_user = True
    nosegae_user_kwargs = {
        'USER_EMAIL': 'nosegae@example.org',
        'USER_IS_ADMIN': 1  # User should be considered an admin
    }
    # enable the datastore stub
    nosegae_datastore_v3 = True  
    nosegae_datastore_v3_kwargs = {  
        'datastore_file': '/tmp/nosegae.sqlite3',  
        'use_sqlite': True  
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM