【发布时间】:2015-12-14 21:37:29
【问题描述】:
我正在尝试通过显式调用路由函数来执行一些测试。
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
myfunc()
return "Index!"
@app.route("/a")
def hello():
return "hello"
def myfunc():
with app.test_request_context('/', method='POST'):
app.hello()
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
但它失败了:
Traceback (most recent call last):
File "python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "test.py", line 9, in index
myfunc()
File "test.py", line 18, in myfunc
app.hello()
AttributeError: 'Flask' object has no attribute 'hello'
这样可以测试路由功能吗?
是的例子有点难看,但我需要弄清楚它有什么问题。
【问题讨论】:
标签: python python-2.7 flask