如何在瓶资源中测试会话 [英] How to test session in flask resource

查看:202
本文介绍了如何在瓶资源中测试会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个资源。这个响应取决于会话中的一个参数(记录)
为了测试这个资源,我写了这些测试:

pre $ import app
import unittest
$ b $ class Test(unittest.TestCase):
def setUp(self):
self.app = app.app.test_client ()

def test_without_session(self):
resp = self.app.get('/')
self.assertEqual('without session',resp.data)

def test_with_session(self):
with self.app as c:
with c.session_transaction()as sess:
sess ['logged'] = True
resp = c.get('/')
self.assertEqual('with session',resp.data)


if __name__ =='__main__':
unittest.main()

我的app.py是这样的:

 从烧瓶导入烧瓶,会话


app =烧瓶(__ name__)


@ app.route('/')
def home() :
如果在会话中记录了:
返回'with session'
返回'without session'


if __name__ =='__main__':
app.run(debug = True)

当我运行测试时, :

 错误:test_pippo_with_session(__main __。Test)
------------- -------------------------------------------------- -------
Traceback(最近一次调用最后一次):
在test_pippo_with_session
文件test_pippo.py,第17行,用c.session_transaction()作为sess:
文件/usr/lib/python2.7/contextlib.py,第17行,在__enter__
返回self.gen.next()
文件/ home / tommaso / repos / prova- flask / local / lib / python2.7 / site-packages / flask / testing.py,第74行,在session_transaction中
raise RuntimeError('Session backend does not open a session。 '
RuntimeError:会话后端没有打开会话。检查配置

我还没有在Google上找到任何解决方案。


如果你没有设置自定义 app.session_interface ,那么你忘记设置一个密钥

  def setUp(self):
app.config ['SECRET_KEY'] ='sekrit!'
self.app = app.app.test_client()


$ b

这只是为测试设置了一个模拟密钥,但是对于你的应用程序来说,你需要生成生产密钥,请参阅快速入门文档中的会话部分,以获取有关如何生成好密钥。



没有密钥,默认的无法创建会话。


I'd like to test a resource. The response of that depends on a parameter in session (logged) To test this resource, I've wrote those tests:

import app
import unittest

class Test(unittest.TestCase):
    def setUp(self):
        self.app = app.app.test_client()

    def test_without_session(self):
        resp = self.app.get('/')
        self.assertEqual('without session', resp.data)

    def test_with_session(self):
        with self.app as c:
            with c.session_transaction() as sess:
                sess['logged'] = True
            resp = c.get('/')
        self.assertEqual('with session', resp.data)


if __name__ == '__main__':
    unittest.main()

My app.py is this:

from flask import Flask, session


app = Flask(__name__)


@app.route('/')
def home():
    if 'logged' in session:
        return 'with session'
    return 'without session'


if __name__ == '__main__':
    app.run(debug=True)

When i run the tests I have this error:

ERROR: test_pippo_with_session (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_pippo.py", line 17, in test_pippo_with_session
    with c.session_transaction() as sess:
  File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/home/tommaso/repos/prova-flask/local/lib/python2.7/site-packages/flask/testing.py", line 74, in session_transaction
    raise RuntimeError('Session backend did not open a session. '
RuntimeError: Session backend did not open a session. Check the configuration

I haven't found any solution on google.

解决方案

If you did not set a custom app.session_interface, then you forgot to set a secret key:

def setUp(self):
    app.config['SECRET_KEY'] = 'sekrit!'
    self.app = app.app.test_client()

This just sets a mock secret key for the tests, but for your application to work you'll need to generate a production secret key, see the sessions section in the Quickstart documentation for tips on how to produce a good secret key.

Without a secret key, the default session implementation fails to create a session.

这篇关于如何在瓶资源中测试会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆