简体   繁体   English

如何使用GAE和Nosetest模拟用户?

[英]How do I mock users with GAE and Nosetest?

How do I call call setup_env() when nose instantiates testbed on it's own? 当鼻子实例化自己的测试平台时,如何调用setup_env()?

I'm trying to develop a App Engine app with TDD and I need to figure out how to mock users. 我正在尝试使用TDD开发App Engine应用程序,我需要弄清楚如何模拟用户。

I start my tests with: 我开始测试:
nosetests -v --with-gae nosetests -v --with-gae

I had several people suggest using nosetests because it will make things easier. 我有几个人建议使用nosetests,因为它会让事情变得更容易。 However, all the examples for how to mock things seem to explicitly use testbed.setup_env() 但是,有关如何模拟事物的所有示例似乎都明确使用了testbed.setup_env()
https://cloud.google.com/appengine/docs/python/tools/localunittesting https://cloud.google.com/appengine/docs/python/tools/localunittesting
How do you mock the User service in App Engine? 你如何模拟App Engine中的用户服务?

I don't understand how I can set environmental values using nosetests. 我不明白如何使用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(): 我将下面的语句添加到测试setUp()中:

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

FWIW, you can configure the users stub the same way you configure the datastore stub. FWIW,您可以像配置数据存储区存根一样配置用户存根。

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  
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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