如何在测试烧瓶应用程序时发出请求时传递修改后的或自定义的会话?
How to pass a modified or custom session while making request in testing flask applications?
我采用了手册中的代码
http://flask.pocoo.org/docs/0.10/testing/#accessing-and-modifying-sessions
with app.test_client() as c:
with c.session_transaction() as sess:
sess['a_key'] = 'a value'
# once this is reached the session was stored
下面介绍了我使用这种方法的项目的核心。
结构
- /testing_with_flask
- /test_project
- /模板
- index.html
- __init__.py
- /requirements.py
- /run.py
- /test.py
每个文件的内容
requirements.txt
Flask
run.py
from test_project import create_app
app = create_app()
app.run(host='0.0.0.0', port=8080, debug=True)
test.py
from test_project import create_app
from unittest import TestCase as Base
class TestCase(Base):
@classmethod
def setUpClass(cls):
cls.app = create_app()
cls.app.config['TESTING'] = True
cls.client = cls.app.test_client()
cls._ctx = cls.app.test_request_context()
cls._ctx.push()
class TestModel(TestCase):
def test_adding_comments_logged_user(self):
with self.app.test_client() as c:
with c.session_transaction() as sess:
sess['user_id'] = 1
self.app.preprocess_request()
response = self.client.get('/')
assert 'Yahooo!!!' in response.data
if __name__ == "__main__":
import unittest
unittest.main()
__init__.py
# Main init file.
from flask import Flask, render_template, g, session
DEBUG = False
TESTING = False
SECRET_KEY = "jd&%G#43WG~dn6"
SITE_TITLE = "TEST"
def index():
return render_template('index.html', user=g.user)
def load_user():
if 'user_id' not in session.keys():
g.user = 'None'
else:
g.user = 'Yahooo!!!'
def create_app():
app = Flask(__name__)
app.config.from_object(__name__)
app.before_request(load_user)
app.add_url_rule('/', 'index', index)
return app
index.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>I Need Help</title>
</head>
<body>
{{ user }}
</body>
</html>
很遗憾,我无法获取变量g.user的值'Yahooo!!!'。我无法获得函数 load_user 运行 test.py 语句的 'else' 分支 'if' .
解决方案基于类似问题中提供的代码:
How to test session in flask resource
代替行
with self.app.test_client() as c:
在'test.py'我应该写
with self.client as c:
不过,我还是不明白这些行的区别。
我采用了手册中的代码
http://flask.pocoo.org/docs/0.10/testing/#accessing-and-modifying-sessions
with app.test_client() as c:
with c.session_transaction() as sess:
sess['a_key'] = 'a value'
# once this is reached the session was stored
下面介绍了我使用这种方法的项目的核心。
结构
- /testing_with_flask
- /test_project
- /模板
- index.html
- __init__.py
- /模板
- /requirements.py
- /run.py
- /test.py
- /test_project
每个文件的内容
requirements.txt
Flask
run.py
from test_project import create_app
app = create_app()
app.run(host='0.0.0.0', port=8080, debug=True)
test.py
from test_project import create_app
from unittest import TestCase as Base
class TestCase(Base):
@classmethod
def setUpClass(cls):
cls.app = create_app()
cls.app.config['TESTING'] = True
cls.client = cls.app.test_client()
cls._ctx = cls.app.test_request_context()
cls._ctx.push()
class TestModel(TestCase):
def test_adding_comments_logged_user(self):
with self.app.test_client() as c:
with c.session_transaction() as sess:
sess['user_id'] = 1
self.app.preprocess_request()
response = self.client.get('/')
assert 'Yahooo!!!' in response.data
if __name__ == "__main__":
import unittest
unittest.main()
__init__.py
# Main init file.
from flask import Flask, render_template, g, session
DEBUG = False
TESTING = False
SECRET_KEY = "jd&%G#43WG~dn6"
SITE_TITLE = "TEST"
def index():
return render_template('index.html', user=g.user)
def load_user():
if 'user_id' not in session.keys():
g.user = 'None'
else:
g.user = 'Yahooo!!!'
def create_app():
app = Flask(__name__)
app.config.from_object(__name__)
app.before_request(load_user)
app.add_url_rule('/', 'index', index)
return app
index.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>I Need Help</title>
</head>
<body>
{{ user }}
</body>
</html>
很遗憾,我无法获取变量g.user的值'Yahooo!!!'。我无法获得函数 load_user 运行 test.py 语句的 'else' 分支 'if' .
解决方案基于类似问题中提供的代码:
How to test session in flask resource
代替行
with self.app.test_client() as c:
在'test.py'我应该写
with self.client as c:
不过,我还是不明白这些行的区别。