我刚刚开始对我的flask应用程序进行单元测试,即使它运行良好,它在测试时也无法返回200状态代码。
这是单元测试代码:
# dev-environment/test_basic.py
import unittest
from application import application, db
TEST_DB = "test.db"
class BasicTests(unittest.TestCase):
def setUp(self):
application.config['TESTING'] = True
application.config['DEBUG'] = False
application.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + TEST_DB
self.application = application.test_client()
db.drop_all()
db.create_all()
self.assertEqual(application.debug, False)
def tearDown(self):
pass
def test_main_page(self):
response = self.application.get("/welcome", follow_redirects=True)
self.assertEqual(response.status_code, 200)
if __name__ == "__main__":
unittest.main()
这是我在application.py中的“ / welcome”视图:
# dev-environment/application.py
@application.route("/welcome")
def welcome():
return render_template("welcome.html")
运行单元测试文件时,我得到以下堆栈跟踪:
======================================================================
FAIL: test_main_page (__main__.BasicTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_basic.py", line 22, in test_main_page
self.assertEqual(response.status_code, 200)
AssertionError: 404 != 200
----------------------------------------------------------------------
Ran 1 test in 0.475s
FAILED (failures=1)