无法获取会话变量在Flask单元测试中设置 [英] Can't Get Session Variables Set Up In Flask Unit Test
问题描述
这是我迄今为止所尝试的。我写了一个单独的数字增值微型项目来隔离这个问题。
错误
Traceback(最近一次调用最后一次):
文件/home/myusername/PycharmProjects/FlaskTestingStuff/MyFlaskTest.py,第12行,在testNumberIncrease
self.assertTrue(res不是None)
文件/usr/lib/python3.4/ contextlib.py,第66行,在__exit__
next(self.gen)
文件/usr/local/lib/python3.4/dist-packages/flask/testing.py,第94行,在session_transaction中
self.cookie_jar.extract_wsgi(c.request.environ,headers)
文件/usr/local/lib/python3.4/dist-packages/flask/ctx.py,行386,在__exit__
self.auto_pop(exc_value)
文件/usr/local/lib/python3.4/dist-packages/flask/ctx.py,第374行,在auto_pop
self.pop(exc)
文件/usr/local/lib/python3.4/dist-packages/flask/ctx.py,行341,弹出
self.app.do_teardown_request( exc)
文件/usr/local/lib/python3.4/dist-packages/flask/app.py,第1710行,在do_teardown_req uest
bp = _request_ctx_stack.top.request.blueprint
AttributeError:'NoneType'对象没有任何属性'request'
下面是我的号码递增迷你项目烧瓶应用程序的视图。
from flask import请求
$ b app = Flask(__ name__)
app.secret_key =somedumbkey
@ app.route(/ increasenum,methods =如果session和会话中的last_num为
,那么会增加num_number():
。 num)$ 1
session [last_num] = num
return num
else:
session [last_num] =0
return0
if __name__ =='__main__':
app.run(debug = True)
$ b $
$ b
$ b
$ b $ p $ import $ unit
从flaskapp导入应用程序
从烧瓶导入会话
class FlaskTestCase(unittest.TestCase):
def testNumberIncrease(self):
with app.test_client()as client:
with client.session_transaction()as sess:
sess [last_num] =8
res = client.get(/ increasenum)
self.assertTrue(res不是None)
如果__name__ =='__main__':
unittest.main()
有没有人关于如何正确设置模拟会话变量这些类型的单元测试的想法? 解决方案
它应该是:
进口单位测试$ b $ from flaskapp进口应用程序$ b $ from flask进口进程session
class FlaskTestCase(unittest.TestCase):
def testNumberIncrease(self):
with app.test_client()as client:
with client.session_transaction()as sess:
#修改会话在这个上下文块。
sess [last_num] =8
#在此上下文块中测试请求。
res = client.get(/ increasenum)
self.assertTrue(res不是None)
if __name__ =='__main__':
unittest。 main()
I am having a difficult time setting test session variables for unit testing some Flask views. There are "email" and "display name" session variables that are normally set via code that handles Google Oauth2 login stuff. My goal is to let the unit testing tools set those session variables instead. That way, my Flask endpoints can be tested independently of the whole oauth2 business.
Here's what I've tried so far. I wrote a separate "number incrementer" mini-project to isolate the issue. Here's the stack trace from running the unit test.
Error
Traceback (most recent call last):
File "/home/myusername/PycharmProjects/FlaskTestingStuff/MyFlaskTest.py", line 12, in testNumberIncrease
self.assertTrue(res is not None)
File "/usr/lib/python3.4/contextlib.py", line 66, in __exit__
next(self.gen)
File "/usr/local/lib/python3.4/dist-packages/flask/testing.py", line 94, in session_transaction
self.cookie_jar.extract_wsgi(c.request.environ, headers)
File "/usr/local/lib/python3.4/dist-packages/flask/ctx.py", line 386, in __exit__
self.auto_pop(exc_value)
File "/usr/local/lib/python3.4/dist-packages/flask/ctx.py", line 374, in auto_pop
self.pop(exc)
File "/usr/local/lib/python3.4/dist-packages/flask/ctx.py", line 341, in pop
self.app.do_teardown_request(exc)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1710, in do_teardown_request
bp = _request_ctx_stack.top.request.blueprint
AttributeError: 'NoneType' object has no attribute 'request'
Here's the view for my number incrementing "mini project" flask app.
from flask import Flask, session, request
app = Flask(__name__)
app.secret_key = "somedumbkey"
@app.route("/increasenum", methods=["GET"])
def increase_num():
if session and "last_num" in session:
num = session["last_num"]
num = str(int(num) + 1)
session["last_num"] = num
return num
else:
session["last_num"] = "0"
return "0"
if __name__ == '__main__':
app.run(debug=True)
Lastly, here is the unit test that's giving me a hard time.
import unittest
from flaskapp import app
from flask import session
class FlaskTestCase(unittest.TestCase):
def testNumberIncrease(self):
with app.test_client() as client:
with client.session_transaction() as sess:
sess["last_num"] = "8"
res = client.get("/increasenum")
self.assertTrue(res is not None)
if __name__ == '__main__':
unittest.main()
Does anybody have any ideas on how to properly set up "mock session variables" properly for these kinds of unit tests?
It should be:
import unittest
from flaskapp import app
from flask import session
class FlaskTestCase(unittest.TestCase):
def testNumberIncrease(self):
with app.test_client() as client:
with client.session_transaction() as sess:
# Modify the session in this context block.
sess["last_num"] = "8"
# Test the request in this context block.
res = client.get("/increasenum")
self.assertTrue(res is not None)
if __name__ == '__main__':
unittest.main()
这篇关于无法获取会话变量在Flask单元测试中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!