【问题标题】:Nosegae Unit Tests with gae-session on a Python Google App Engine Project在 Python Google App Engine 项目上使用 gae-session 进行 Nosegae 单元测试
【发布时间】:2011-03-18 06:44:23
【问题描述】:

当我使用单元测试时,我无法让gae-sessions 工作。我不确定这是 cookie 问题还是内部 gae-sessions 问题(它使用 memcache/数据存储来实现持久性)。通过浏览器访问时,它在实际应用上运行良好。

我在 Mac OS 10.6.6、Google App Engine SDK 1.4.2、nosetests 版本 1.0.0、NoseGAE-0.1.7 和 gae-sessions v1.06 上使用 Python 2.5。

这是我的代码:


网络应用实施:

from google.appengine.ext import webapp
from gaesessions import get_current_session

class cookietest(webapp.RequestHandler):
    def get(self):
        s = get_current_session()
        if not 'message' in s:
            self.response.out.write('No session value')
            s['message'] = 'This value is stored in a session'
        else:
            self.response.out.write("Message found: %s" % s['message'])

application = webapp.WSGIApplication([(r'/cookietest', cookietest)])

单元测试实现:

def test_cookietest(self):
    from gaesessions import SessionMiddleware
    app_with_sessions = SessionMiddleware(application, cookie_key=os.urandom(64)) # random key
    self.app = TestApp(app_with_sessions)

    resp = self.app.get('/cookietest')
    cookie = resp.headers['Set-Cookie']
    assert cookie
    resp = self.app.get('/cookietest', headers={'Cookie': cookie})
    print resp
    assert 'This value is stored in a session' in resp

相关输出为:

FAIL: test_cookietest (tests.test.TestUser)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Asad/.../test.py", line 33, in test_cookietest
    assert 'This value is stored in a session' in resp
AssertionError: 
-------------------- >> begin captured stdout << ---------------------
Response: 200 OK
Cache-Control: no-cache
Content-Type: text/html; charset=utf-8
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Set-Cookie:  DgU00="DQjpTdhQsKYYGhvNr/CLm+v835QV5V1jTX9T4wMULhI=1301034363_89b1c1007a6159e85828ab9a7673759cgAJ9cQB9cQFVB21lc3NhZ2VxAlUhVGhpcyB2YWx1ZSBpcyBzdG9yZWQgaW4gYSBzZXNzaW9ucQNzhnEELg=="; expires=Fri, 25-Mar-2011 06:26:03 GMT; Path=/; HttpOnly
No session value

--------------------- >> end captured stdout << ----------------------
-------------------- >> begin captured logging << --------------------
root: WARNING: Could not read datastore data from /var/folders/pt/ptPaW5O7E-eLIrfFhhczbU+++TI/-Tmp-/nosegae.datastore
root: INFO: zipimporter('/Library/Python/2.5/site-packages/NoseGAE-0.1.7-py2.5.egg', '')
root: INFO: zipimporter('/Library/Python/2.5/site-packages/WebTest-1.2.3-py2.5.egg', '')
root: INFO: zipimporter('/Library/Python/2.5/site-packages/WebOb-1.0-py2.5.egg', '')
root: INFO: zipimporter('/Library/Python/2.5/site-packages/mock-0.7.0b4-py2.5.egg', '')
root: DEBUG: Could not import "strop": Disallowed C-extension or built-in module
--------------------- >> end captured logging << ---------------------

----------------------------------------------------------------------
Ran 10 tests in 0.284s

FAILED (failures=1)

【问题讨论】:

    标签: python google-app-engine cookies webtest nosetests


    【解决方案1】:

    我认为您必须在此测试中手动保存会话。

       if not 'message' in s:
            self.response.out.write('No session value')
            s['message'] = 'This value is stored in a session'
            s.save()
    

    更新:

    gae-sessionsos.environ['HTTP_COOKIE'] 变量中检索会话 cookie。这个值需要在self.app.get请求之前设置,而不是在headers参数中传入cookie。

    【讨论】:

    • Gae-sessions 从os.environ['HTTP_COOKIE'] 变量中检索cookie。我认为传入Cookie 标头并不等同于设置此值。
    • 谢谢!那行得通。我只是在我的 self.app.get() 请求之前设置了 os.environ['HTTP_COOKIE'] 并且它工作得很好。要编辑答案或写另一个答案以便我接受吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    相关资源
    最近更新 更多