python - How to test session in flask resource -


i'd test resource. response of depends on parameter in session (logged) test resource, i've wrote 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):         self.app c:             c.session_transaction() sess:                 sess['logged'] = true             resp = c.get('/')         self.assertequal('with session', resp.data)   if __name__ == '__main__':     unittest.main() 

my app.py 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 run tests have error:

error: test_pippo_with_session (__main__.test) ---------------------------------------------------------------------- traceback (most recent call last):   file "test_pippo.py", line 17, in test_pippo_with_session     c.session_transaction() 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 session. ' runtimeerror: session backend did not open session. check configuration 

i haven't found solution on google.

if did not set custom app.session_interface, forgot set secret key:

def setup(self):     app.config['secret_key'] = 'sekrit!'     self.app = app.app.test_client() 

this sets mock secret key tests, application work you'll need generate production secret key, see sessions section in quickstart documentation tips on how produce secret key.

without secret key, default session implementation fails create session.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -