下面是我的测试文件:
from flask import Flask
from flask.ext.testing import TestCase
class TestInitViews(TestCase):
render_templates = False
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
return app
def test_root_route(self):
self.client.get('/')
self.assert_template_used('index.html')下面是完整的堆栈跟踪:
$ nosetests tests/api/client/test_init_views.py
F
======================================================================
FAIL: test_root_route (tests.api.client.test_init_views.TestInitViews)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/dmonsewicz/dev/autoresponders/tests/api/client/test_init_views.py", line 17, in test_root_route
self.assert_template_used('index.html')
File "/Users/dmonsewicz/.virtualenvs/autoresponders-api/lib/python2.7/site-packages/flask_testing.py", line 120, in assertTemplateUsed
raise AssertionError("template %s not used" % name)
AssertionError: template index.html not used
----------------------------------------------------------------------
Ran 1 test in 0.012s
FAILED (failures=1)我是Python的新手,似乎不能理解这一点。我所要做的就是编写一个简单的测试,该测试命中/ (根路由)端点和asserts,所使用的模板实际上是index.html
尝试使用LiveServerTestCase
from flask import Flask
from flask.ext.testing import LiveServerTestCase
class TestInitViews(LiveServerTestCase):
render_templates = False
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
app.config['LIVESERVER_PORT'] = 6765
return app
def setUp(self):
self.app = self.app.test_client()
def test_root_route(self):
res = self.app.get('/')
print(res)
self.assert_template_used('index.html')我使用的是Flask-Testing版本0.4,但由于某种原因,我的导入中不存在该LiveServerTestCase
工作代码
from flask import Flask
from flask.ext.testing import TestCase
from api.client import blueprint
class TestInitViews(TestCase):
render_templates = False
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
app.register_blueprint(blueprint)
return app
def test_root_route(self):
res = self.client.get('/')
self.assert_template_used('index.html')发布于 2014-06-14 10:05:53
你必须运行pip install blinker并确保你的flask版本高于.6。
看起来您忽略了设置app.config‘’TESTING‘= True
我能够运行以下测试来验证断言是否为True:
#!/usr/bin/python
import unittest
from flask import Flask
from flask.ext.testing import TestCase
from flask import render_template
class MyTest(TestCase):
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
@app.route('/')
def hello_world():
return render_template('index.html')
return app
def test_root_route(self):
self.client.get('/')
self.assert_template_used('index.html')
if __name__ == '__main__':
unittest.main()https://stackoverflow.com/questions/24215880
复制相似问题