我想测试资源。响应取决于会话中的参数(已记录) 为了测试这个资源,我写了这些测试:
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()
我的app.py是这样的:
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)
当我运行测试时,我有这个错误:
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
我还没有在谷歌找到任何解决方案。