《Flask Web开发》第七章,学到最后程序能够成功运行,但是进行单元测试却报错。
相关文件app__init__.py代码:
from flask import Flask, render_template from flask_bootstrap import Bootstrap from flask_mail import Mail from flask_moment import Moment from flask_sqlalchemy import SQLAlchemy from config import config bootstrap = Bootstrap() mail = Mail() moment = Moment() db = SQLAlchemy() def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) config[config_name].init_app(app) bootstrap.init_app(app) mail.init_app(app) moment.init_app(app) db.init_app(app) from .main import main as main_blueprint app.register_blueprint(main_blueprint) #注册蓝本 return app
teststest_basics.py文件代码:
import unittest from flask import current_app from app import create_app, db class BasicsTestCase(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() def tearDown(self): db.session.remove() db.drop_all() self.app_context.pop() def test_app_exists(self): self.assertFalse(current_app is None) def test_app_is_testing(self): self.assertTrue(current_app.config['TESTING'])
运行python manage.py runserver 没有问题,但是进行单元测试就失败了。网上查了一下,查不到太多的错误解析,求解???
看报错,就可以看出来了,config.py 的 config没有键值testing,
请检查下config.py文件,可能没有更新到test 版本。