【发布时间】:2017-12-18 01:47:22
【问题描述】:
我认为 Flask 想让我实例化应用程序,但我不知道如何,收到错误 AttributeError: 'NoneType' object has no attribute 'app'
追溯:
C:\Users\Mlamba\Envs\vir\Scripts\python.exe D:/code/web-projects/Bucketlist-Python-Flask-project/tests.py
E
======================================================================
ERROR: test_index_view (__main__.ViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:/code/web-projects/Bucketlist-Python-Flask-project/tests.py", line 11, in test_index_view
response = make_response(render_template("index.html"))
File "C:\Users\Mlamba\Envs\vir\lib\site-packages\flask\templating.py", line 132, in render_template
ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
Run.py 文件:
from app import app
if __name__ == '__main__':
app.run()
init.py 文件:
from flask import Flask
# Load the views
from app import views
# Initialize the app
app = Flask(__name__, instance_relative_config=True)
# Load the config file
app.config.from_object('config')
测试文件:
import unittest
from flask import render_template, make_response
class ViewTests(unittest.TestCase):
def test_index_view(self):
"""
Test that index page is accessible without login
"""
response = make_response(render_template("index.html"))
self.assertEquals(response.status_code, 200)
if __name__ == '__main__':
unittest.main()
目录结构:
|-- README.md
|-- __pycache__
| `-- config.cpython-36.pyc
|-- app
| |-- __init__.py
| |-- __pycache__
| | |-- __init__.cpython-36.pyc
| | `-- views.cpython-36.pyc
| |-- models.py
| |-- static
| |-- templates
| | |-- index.html
| | `-- layout.html
| `-- views.py
|-- config.py
|-- requirements.txt
|-- run.py
`-- tests.py
【问题讨论】:
-
你的测试没有做任何与文档字符串相关的事情,即检查索引页面是否可以在没有登录的情况下访问。
-
但是,对于您的实际问题,请显示完整的回溯。
-
添加了对问题的追溯
-
@A.Mlamba 我已经编辑了我的答案,所以你有一个例子。希望这有帮助:) p.s.我看到您是新人,所以我会告诉您:如果某个答案对您有帮助
upvote it和最佳答案accept it,请单击答案旁边的 ✓
标签: python unit-testing flask tdd